Skip to content

Commit 58f9efd

Browse files
committed
Auto merge of rust-lang#91311 - matthiaskrgr:rollup-ju9xizl, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - rust-lang#90896 (Stabilize some `MaybeUninit` behavior as const) - rust-lang#91254 (Only check for errors in predicate when skipping impl assembly) - rust-lang#91303 (Miri: fix alignment check in array initialization) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 27d5935 + 7134ae0 commit 58f9efd

File tree

12 files changed

+74
-41
lines changed

12 files changed

+74
-41
lines changed

compiler/rustc_const_eval/src/interpret/step.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
242242
let elem_size = first.layout.size;
243243
let first_ptr = first.ptr;
244244
let rest_ptr = first_ptr.offset(elem_size, self)?;
245+
// For the alignment of `rest_ptr`, we crucially do *not* use `first.align` as
246+
// that place might be more aligned than its type mandates (a `u8` array could
247+
// be 4-aligned if it sits at the right spot in a struct). Instead we use
248+
// `first.layout.align`, i.e., the alignment given by the type.
245249
self.memory.copy_repeatedly(
246250
first_ptr,
247251
first.align,
248252
rest_ptr,
249-
first.align,
253+
first.layout.align.abi,
250254
elem_size,
251255
length - 1,
252256
/*nonoverlapping:*/ true,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
536536
// This helps us avoid overflow: see issue #72839
537537
// Since compilation is already guaranteed to fail, this is just
538538
// to try to show the 'nicest' possible errors to the user.
539-
if obligation.references_error() {
539+
// We don't check for errors in the `ParamEnv` - in practice,
540+
// it seems to cause us to be overly aggressive in deciding
541+
// to give up searching for candidates, leading to spurious errors.
542+
if obligation.predicate.references_error() {
540543
return;
541544
}
542545

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ extern "rust-intrinsic" {
853853
/// This will statically either panic, or do nothing.
854854
///
855855
/// This intrinsic does not have a stable counterpart.
856-
#[rustc_const_unstable(feature = "const_assert_type", issue = "none")]
856+
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
857857
pub fn assert_inhabited<T>();
858858

859859
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit

library/core/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
#![feature(const_align_of_val)]
102102
#![feature(const_alloc_layout)]
103103
#![feature(const_arguments_as_str)]
104-
#![feature(const_assert_type)]
105104
#![feature(const_bigint_helper_methods)]
106105
#![feature(const_caller_location)]
107106
#![feature(const_cell_into_inner)]
@@ -117,7 +116,7 @@
117116
#![feature(const_intrinsic_copy)]
118117
#![feature(const_intrinsic_forget)]
119118
#![feature(const_likely)]
120-
#![feature(const_maybe_uninit_as_ptr)]
119+
#![feature(const_maybe_uninit_as_mut_ptr)]
121120
#![feature(const_maybe_uninit_assume_init)]
122121
#![feature(const_num_from_num)]
123122
#![feature(const_ops)]

library/core/src/mem/maybe_uninit.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<T> MaybeUninit<T> {
528528
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
529529
/// until they are, it is advisable to avoid them.)
530530
#[stable(feature = "maybe_uninit", since = "1.36.0")]
531-
#[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
531+
#[rustc_const_stable(feature = "const_maybe_uninit_as_ptr", since = "1.59.0")]
532532
#[inline(always)]
533533
pub const fn as_ptr(&self) -> *const T {
534534
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
@@ -567,7 +567,7 @@ impl<T> MaybeUninit<T> {
567567
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
568568
/// until they are, it is advisable to avoid them.)
569569
#[stable(feature = "maybe_uninit", since = "1.36.0")]
570-
#[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
570+
#[rustc_const_unstable(feature = "const_maybe_uninit_as_mut_ptr", issue = "75251")]
571571
#[inline(always)]
572572
pub const fn as_mut_ptr(&mut self) -> *mut T {
573573
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
@@ -620,7 +620,7 @@ impl<T> MaybeUninit<T> {
620620
/// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
621621
/// ```
622622
#[stable(feature = "maybe_uninit", since = "1.36.0")]
623-
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
623+
#[rustc_const_stable(feature = "const_maybe_uninit_assume_init", since = "1.59.0")]
624624
#[inline(always)]
625625
#[rustc_diagnostic_item = "assume_init"]
626626
#[track_caller]
@@ -788,7 +788,8 @@ impl<T> MaybeUninit<T> {
788788
/// }
789789
/// ```
790790
#[stable(feature = "maybe_uninit_ref", since = "1.55.0")]
791-
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
791+
#[rustc_const_stable(feature = "const_maybe_uninit_assume_init", since = "1.59.0")]
792+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_raw_ptr_deref))]
792793
#[inline(always)]
793794
pub const unsafe fn assume_init_ref(&self) -> &T {
794795
// SAFETY: the caller must guarantee that `self` is initialized.
@@ -968,7 +969,7 @@ impl<T> MaybeUninit<T> {
968969
///
969970
/// [`assume_init_ref`]: MaybeUninit::assume_init_ref
970971
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
971-
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
972+
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
972973
#[inline(always)]
973974
pub const unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] {
974975
// SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(const_assume)]
1111
#![feature(const_cell_into_inner)]
1212
#![feature(const_convert)]
13+
#![feature(const_maybe_uninit_as_mut_ptr)]
1314
#![feature(const_maybe_uninit_assume_init)]
1415
#![feature(const_ptr_read)]
1516
#![feature(const_ptr_write)]

library/core/tests/mem.rs

+32
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,35 @@ fn uninit_const_assume_init_read() {
269269
const FOO: u32 = unsafe { MaybeUninit::new(42).assume_init_read() };
270270
assert_eq!(FOO, 42);
271271
}
272+
273+
#[test]
274+
fn const_maybe_uninit() {
275+
use std::ptr;
276+
277+
#[derive(Debug, PartialEq)]
278+
struct Foo {
279+
x: u8,
280+
y: u8,
281+
}
282+
283+
const FIELD_BY_FIELD: Foo = unsafe {
284+
let mut val = MaybeUninit::uninit();
285+
init_y(&mut val); // order shouldn't matter
286+
init_x(&mut val);
287+
val.assume_init()
288+
};
289+
290+
const fn init_x(foo: &mut MaybeUninit<Foo>) {
291+
unsafe {
292+
*ptr::addr_of_mut!((*foo.as_mut_ptr()).x) = 1;
293+
}
294+
}
295+
296+
const fn init_y(foo: &mut MaybeUninit<Foo>) {
297+
unsafe {
298+
*ptr::addr_of_mut!((*foo.as_mut_ptr()).y) = 2;
299+
}
300+
}
301+
302+
assert_eq!(FIELD_BY_FIELD, Foo { x: 1, y: 2 });
303+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: generic parameters may not be used in const operations
2-
--> $DIR/issue-72787.rs:12:17
2+
--> $DIR/issue-72787.rs:11:17
33
|
44
LL | Condition<{ LHS <= RHS }>: True
55
| ^^^ cannot perform const operation using `LHS`
@@ -8,7 +8,7 @@ LL | Condition<{ LHS <= RHS }>: True
88
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
11-
--> $DIR/issue-72787.rs:12:24
11+
--> $DIR/issue-72787.rs:11:24
1212
|
1313
LL | Condition<{ LHS <= RHS }>: True
1414
| ^^^ cannot perform const operation using `RHS`
@@ -17,7 +17,7 @@ LL | Condition<{ LHS <= RHS }>: True
1717
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: generic parameters may not be used in const operations
20-
--> $DIR/issue-72787.rs:26:25
20+
--> $DIR/issue-72787.rs:25:25
2121
|
2222
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
2323
| ^ cannot perform const operation using `I`
@@ -26,7 +26,7 @@ LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
2626
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
2727

2828
error: generic parameters may not be used in const operations
29-
--> $DIR/issue-72787.rs:26:36
29+
--> $DIR/issue-72787.rs:25:36
3030
|
3131
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
3232
| ^ cannot perform const operation using `J`
@@ -35,29 +35,21 @@ LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
3535
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
3636

3737
error[E0283]: type annotations needed
38-
--> $DIR/issue-72787.rs:10:38
39-
|
40-
LL | impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
41-
| ^^^^ cannot infer type for struct `IsLessOrEqual<LHS, RHS>`
42-
|
43-
= note: cannot satisfy `IsLessOrEqual<LHS, RHS>: True`
44-
45-
error[E0283]: type annotations needed
46-
--> $DIR/issue-72787.rs:22:26
38+
--> $DIR/issue-72787.rs:21:26
4739
|
4840
LL | IsLessOrEqual<I, 8>: True,
4941
| ^^^^ cannot infer type for struct `IsLessOrEqual<I, 8_u32>`
5042
|
5143
= note: cannot satisfy `IsLessOrEqual<I, 8_u32>: True`
5244

5345
error[E0283]: type annotations needed
54-
--> $DIR/issue-72787.rs:22:26
46+
--> $DIR/issue-72787.rs:21:26
5547
|
5648
LL | IsLessOrEqual<I, 8>: True,
5749
| ^^^^ cannot infer type for struct `IsLessOrEqual<I, 8_u32>`
5850
|
5951
= note: cannot satisfy `IsLessOrEqual<I, 8_u32>: True`
6052

61-
error: aborting due to 7 previous errors
53+
error: aborting due to 6 previous errors
6254

6355
For more information about this error, try `rustc --explain E0283`.

src/test/ui/const-generics/generic_const_exprs/issue-72787.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub struct Condition<const CONDITION: bool>;
88
pub trait True {}
99

1010
impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
11-
//[min]~^ ERROR type annotations needed
1211
Condition<{ LHS <= RHS }>: True
1312
//[min]~^ Error generic parameters may not be used in const operations
1413
//[min]~| Error generic parameters may not be used in const operations

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ struct Multiply<N, M> {
1010
}
1111
impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
1212
//~^ ERROR cannot find type `VAL` in this scope
13-
//~| ERROR type annotations needed
13+
//~| ERROR not all trait items implemented, missing: `VAL`

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
1717
| |
1818
| help: you might be missing a type parameter: `, VAL`
1919

20-
error[E0283]: type annotations needed
21-
--> $DIR/issue-77919.rs:11:12
20+
error[E0046]: not all trait items implemented, missing: `VAL`
21+
--> $DIR/issue-77919.rs:11:1
2222
|
23+
LL | const VAL: T;
24+
| ------------- `VAL` from trait
25+
...
2326
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
24-
| ^^^^^^^^^^^^^^ cannot infer type for struct `Multiply<N, M>`
25-
|
26-
= note: cannot satisfy `Multiply<N, M>: TypeVal<usize>`
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
2728

2829
error: aborting due to 3 previous errors
2930

30-
Some errors have detailed explanations: E0283, E0412.
31-
For more information about an error, try `rustc --explain E0283`.
31+
Some errors have detailed explanations: E0046, E0412.
32+
For more information about an error, try `rustc --explain E0046`.

src/tools/clippy/tests/ui/crashes/ice-6252.stderr

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
2121
| |
2222
| help: you might be missing a type parameter: `, VAL`
2323

24-
error[E0283]: type annotations needed
25-
--> $DIR/ice-6252.rs:10:12
24+
error[E0046]: not all trait items implemented, missing: `VAL`
25+
--> $DIR/ice-6252.rs:10:1
2626
|
27+
LL | const VAL: T;
28+
| ------------- `VAL` from trait
29+
...
2730
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
28-
| ^^^^^^^^^^^^^^ cannot infer type for struct `Multiply<N, M>`
29-
|
30-
= note: cannot satisfy `Multiply<N, M>: TypeVal<usize>`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
3132

3233
error: aborting due to 3 previous errors
3334

34-
Some errors have detailed explanations: E0283, E0412.
35-
For more information about an error, try `rustc --explain E0283`.
35+
Some errors have detailed explanations: E0046, E0412.
36+
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)