Skip to content

Commit 5e0c6a6

Browse files
committed
Auto merge of #61962 - Centril:rollup-y6sg1zw, r=Centril
Rollup of 4 pull requests Successful merges: - #60667 ( Add functions for building raw slices to libcore ) - #61547 (Support `cfg` and `cfg_attr` on generic parameters) - #61861 (Update rustfmt and rls) - #61940 (Make Place::ty iterate) Failed merges: r? @ghost
2 parents e79b2a1 + bf6c505 commit 5e0c6a6

File tree

12 files changed

+286
-213
lines changed

12 files changed

+286
-213
lines changed

Cargo.lock

+104-82
Large diffs are not rendered by default.

src/libcore/ptr/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,53 @@ pub const fn null<T>() -> *const T { 0 as *const T }
230230
#[rustc_promotable]
231231
pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
232232

233+
#[repr(C)]
234+
pub(crate) union Repr<T> {
235+
pub(crate) rust: *const [T],
236+
rust_mut: *mut [T],
237+
pub(crate) raw: FatPtr<T>,
238+
}
239+
240+
#[repr(C)]
241+
pub(crate) struct FatPtr<T> {
242+
data: *const T,
243+
pub(crate) len: usize,
244+
}
245+
246+
/// Forms a slice from a pointer and a length.
247+
///
248+
/// The `len` argument is the number of **elements**, not the number of bytes.
249+
///
250+
/// # Examples
251+
///
252+
/// ```rust
253+
/// #![feature(slice_from_raw_parts)]
254+
/// use std::ptr;
255+
///
256+
/// // create a slice pointer when starting out with a pointer to the first element
257+
/// let mut x = [5, 6, 7];
258+
/// let ptr = &mut x[0] as *mut _;
259+
/// let slice = ptr::slice_from_raw_parts_mut(ptr, 3);
260+
/// assert_eq!(unsafe { &*slice }[2], 7);
261+
/// ```
262+
#[inline]
263+
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
264+
pub fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
265+
unsafe { Repr { raw: FatPtr { data, len } }.rust }
266+
}
267+
268+
/// Performs the same functionality as [`from_raw_parts`], except that a
269+
/// mutable slice is returned.
270+
///
271+
/// See the documentation of [`from_raw_parts`] for more details.
272+
///
273+
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
274+
#[inline]
275+
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
276+
pub fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
277+
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
278+
}
279+
233280
/// Swaps the values at two mutable locations of the same type, without
234281
/// deinitializing either.
235282
///

src/libcore/slice/mod.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,6 @@ pub mod memchr;
4545
mod rotate;
4646
mod sort;
4747

48-
#[repr(C)]
49-
union Repr<'a, T: 'a> {
50-
rust: &'a [T],
51-
rust_mut: &'a mut [T],
52-
raw: FatPtr<T>,
53-
}
54-
55-
#[repr(C)]
56-
struct FatPtr<T> {
57-
data: *const T,
58-
len: usize,
59-
}
60-
6148
//
6249
// Extension traits
6350
//
@@ -78,7 +65,7 @@ impl<T> [T] {
7865
#[rustc_const_unstable(feature = "const_slice_len")]
7966
pub const fn len(&self) -> usize {
8067
unsafe {
81-
Repr { rust: self }.raw.len
68+
crate::ptr::Repr { rust: self }.raw.len
8269
}
8370
}
8471

@@ -5195,7 +5182,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
51955182
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
51965183
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
51975184
"attempt to create slice covering half the address space");
5198-
Repr { raw: FatPtr { data, len } }.rust
5185+
&*ptr::slice_from_raw_parts(data, len)
51995186
}
52005187

52015188
/// Performs the same functionality as [`from_raw_parts`], except that a
@@ -5216,7 +5203,7 @@ pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]
52165203
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
52175204
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
52185205
"attempt to create slice covering half the address space");
5219-
Repr { raw: FatPtr { data, len } }.rust_mut
5206+
&mut *ptr::slice_from_raw_parts_mut(data, len)
52205207
}
52215208

52225209
/// Converts a reference to T into a slice of length 1 (without copying).

src/librustc/mir/tcx.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,25 @@ impl<'tcx> Place<'tcx> {
122122
where
123123
D: HasLocalDecls<'tcx>,
124124
{
125-
match *self {
126-
Place::Base(PlaceBase::Local(index)) =>
127-
PlaceTy::from_ty(local_decls.local_decls()[index].ty),
128-
Place::Base(PlaceBase::Static(ref data)) =>
129-
PlaceTy::from_ty(data.ty),
130-
Place::Projection(ref proj) =>
131-
proj.base.ty(local_decls, tcx).projection_ty(tcx, &proj.elem),
125+
self.iterate(|place_base, place_projections| {
126+
let mut place_ty = place_base.ty(local_decls);
127+
128+
for proj in place_projections {
129+
place_ty = place_ty.projection_ty(tcx, &proj.elem);
130+
}
131+
132+
place_ty
133+
})
134+
}
135+
}
136+
137+
impl<'tcx> PlaceBase<'tcx> {
138+
pub fn ty<D>(&self, local_decls: &D) -> PlaceTy<'tcx>
139+
where D: HasLocalDecls<'tcx>
140+
{
141+
match self {
142+
PlaceBase::Local(index) => PlaceTy::from_ty(local_decls.local_decls()[*index].ty),
143+
PlaceBase::Static(data) => PlaceTy::from_ty(data.ty),
132144
}
133145
}
134146
}

src/libsyntax/config.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ impl<'a> StripUnconfigured<'a> {
240240
items.flat_map_in_place(|item| self.configure(item));
241241
}
242242

243+
pub fn configure_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
244+
params.flat_map_in_place(|param| self.configure(param));
245+
}
246+
243247
fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
244248
match vdata {
245249
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) =>
@@ -301,22 +305,6 @@ impl<'a> StripUnconfigured<'a> {
301305
pub fn configure_fn_decl(&mut self, fn_decl: &mut ast::FnDecl) {
302306
fn_decl.inputs.flat_map_in_place(|arg| self.configure(arg));
303307
}
304-
305-
/// Denies `#[cfg]` on generic parameters until we decide what to do with it.
306-
/// See issue #51279.
307-
pub fn disallow_cfg_on_generic_param(&mut self, param: &ast::GenericParam) {
308-
for attr in param.attrs() {
309-
let offending_attr = if attr.check_name(sym::cfg) {
310-
"cfg"
311-
} else if attr.check_name(sym::cfg_attr) {
312-
"cfg_attr"
313-
} else {
314-
continue;
315-
};
316-
let msg = format!("#[{}] cannot be applied on a generic parameter", offending_attr);
317-
self.sess.span_diagnostic.span_err(attr.span, &msg);
318-
}
319-
}
320308
}
321309

322310
impl<'a> MutVisitor for StripUnconfigured<'a> {

src/libsyntax/ext/expand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1329,9 +1329,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13291329
}
13301330
}
13311331

1332-
fn visit_generic_param(&mut self, param: &mut ast::GenericParam) {
1333-
self.cfg.disallow_cfg_on_generic_param(&param);
1334-
noop_visit_generic_param(param, self)
1332+
fn visit_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
1333+
self.cfg.configure_generic_params(params);
1334+
noop_visit_generic_params(params, self);
13351335
}
13361336

13371337
fn visit_attribute(&mut self, at: &mut ast::Attribute) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// compile-flags:--cfg yes
2+
3+
fn f_lt<#[cfg(yes)] 'a: 'a, #[cfg(no)] T>() {}
4+
fn f_ty<#[cfg(no)] 'a: 'a, #[cfg(yes)] T>() {}
5+
6+
type FnGood = for<#[cfg(yes)] 'a, #[cfg(no)] T> fn(); // OK
7+
type FnBad = for<#[cfg(no)] 'a, #[cfg(yes)] T> fn();
8+
//~^ ERROR only lifetime parameters can be used in this context
9+
10+
type PolyGood = dyn for<#[cfg(yes)] 'a, #[cfg(no)] T> Copy; // OK
11+
type PolyBad = dyn for<#[cfg(no)] 'a, #[cfg(yes)] T> Copy;
12+
//~^ ERROR only lifetime parameters can be used in this context
13+
14+
struct WhereGood where for<#[cfg(yes)] 'a, #[cfg(no)] T> u8: Copy; // OK
15+
struct WhereBad where for<#[cfg(no)] 'a, #[cfg(yes)] T> u8: Copy;
16+
//~^ ERROR only lifetime parameters can be used in this context
17+
18+
fn f_lt_no<#[cfg_attr(no, unknown)] 'a>() {} // OK
19+
fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {} //~ ERROR attribute `unknown` is currently unknown
20+
fn f_ty_no<#[cfg_attr(no, unknown)] T>() {} // OK
21+
fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {} //~ ERROR attribute `unknown` is currently unknown
22+
23+
type FnNo = for<#[cfg_attr(no, unknown)] 'a> fn(); // OK
24+
type FnYes = for<#[cfg_attr(yes, unknown)] 'a> fn();
25+
//~^ ERROR attribute `unknown` is currently unknown
26+
27+
type PolyNo = dyn for<#[cfg_attr(no, unknown)] 'a> Copy; // OK
28+
type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
29+
//~^ ERROR attribute `unknown` is currently unknown
30+
31+
struct WhereNo where for<#[cfg_attr(no, unknown)] 'a> u8: Copy; // OK
32+
struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
33+
//~^ ERROR attribute `unknown` is currently unknown
34+
35+
fn main() {
36+
f_lt::<'static>();
37+
f_ty::<u8>();
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error: only lifetime parameters can be used in this context
2+
--> $DIR/cfg-generic-params.rs:7:45
3+
|
4+
LL | type FnBad = for<#[cfg(no)] 'a, #[cfg(yes)] T> fn();
5+
| ^
6+
7+
error: only lifetime parameters can be used in this context
8+
--> $DIR/cfg-generic-params.rs:11:51
9+
|
10+
LL | type PolyBad = dyn for<#[cfg(no)] 'a, #[cfg(yes)] T> Copy;
11+
| ^
12+
13+
error: only lifetime parameters can be used in this context
14+
--> $DIR/cfg-generic-params.rs:15:54
15+
|
16+
LL | struct WhereBad where for<#[cfg(no)] 'a, #[cfg(yes)] T> u8: Copy;
17+
| ^
18+
19+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
20+
--> $DIR/cfg-generic-params.rs:19:29
21+
|
22+
LL | fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {}
23+
| ^^^^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
26+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
27+
28+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
29+
--> $DIR/cfg-generic-params.rs:21:29
30+
|
31+
LL | fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {}
32+
| ^^^^^^^
33+
|
34+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
35+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
36+
37+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
38+
--> $DIR/cfg-generic-params.rs:24:34
39+
|
40+
LL | type FnYes = for<#[cfg_attr(yes, unknown)] 'a> fn();
41+
| ^^^^^^^
42+
|
43+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
44+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
45+
46+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
47+
--> $DIR/cfg-generic-params.rs:28:40
48+
|
49+
LL | type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
50+
| ^^^^^^^
51+
|
52+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
53+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
54+
55+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
56+
--> $DIR/cfg-generic-params.rs:32:43
57+
|
58+
LL | struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
59+
| ^^^^^^^
60+
|
61+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
62+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
63+
64+
error: aborting due to 8 previous errors
65+
66+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/issues/issue-51279.rs

-27
This file was deleted.

src/test/ui/issues/issue-51279.stderr

-60
This file was deleted.

src/tools/rls

Submodule rls updated from 483dcbc to 3e51965

src/tools/rustfmt

0 commit comments

Comments
 (0)