Skip to content

trait_sel: MetaSized always holds temporarily #144016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
_ => return false,
};

// FIXME(sized_hierarchy): this temporarily reverts the `sized_hierarchy` feature
// while a proper fix for `tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs`
// is pending a proper fix
if !tcx.features().sized_hierarchy() && matches!(sizedness, SizedTraitKind::MetaSized) {
return true;
}

if trait_pred.self_ty().has_trivial_sizedness(tcx, sizedness) {
debug!("fast path -- trivial sizedness");
return true;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/const-size_of_val-align_of_val-extern-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ extern "C" {
}

const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) };
//~^ ERROR the size for values of type `Opaque` cannot be known
//~^ ERROR `extern type` does not have known layout
const _ALIGN: usize = unsafe { align_of_val(&4 as *const i32 as *const Opaque) };
//~^ ERROR the size for values of type `Opaque` cannot be known
//~^ ERROR `extern type` does not have known layout

fn main() {}
38 changes: 7 additions & 31 deletions tests/ui/consts/const-size_of_val-align_of_val-extern-type.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
error[E0277]: the size for values of type `Opaque` cannot be known
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:10:43
error[E0080]: `extern type` does not have known layout
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:10:31
|
LL | const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) };
| ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MetaSized` is not implemented for `Opaque`
| |
| required by a bound introduced by this call
|
= note: the trait bound `Opaque: MetaSized` is not satisfied
note: required by a bound in `std::intrinsics::size_of_val`
--> $SRC_DIR/core/src/intrinsics/mod.rs:LL:COL
help: consider borrowing here
|
LL | const _SIZE: usize = unsafe { size_of_val(&(&4 as *const i32 as *const Opaque)) };
| ++ +
LL | const _SIZE: usize = unsafe { size_of_val(&mut (&4 as *const i32 as *const Opaque)) };
| ++++++ +
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_SIZE` failed here

error[E0277]: the size for values of type `Opaque` cannot be known
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:45
error[E0080]: `extern type` does not have known layout
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:32
|
LL | const _ALIGN: usize = unsafe { align_of_val(&4 as *const i32 as *const Opaque) };
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MetaSized` is not implemented for `Opaque`
| |
| required by a bound introduced by this call
|
= note: the trait bound `Opaque: MetaSized` is not satisfied
note: required by a bound in `std::intrinsics::align_of_val`
--> $SRC_DIR/core/src/intrinsics/mod.rs:LL:COL
help: consider borrowing here
|
LL | const _ALIGN: usize = unsafe { align_of_val(&(&4 as *const i32 as *const Opaque)) };
| ++ +
LL | const _ALIGN: usize = unsafe { align_of_val(&mut (&4 as *const i32 as *const Opaque)) };
| ++++++ +
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_ALIGN` failed here

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0080`.
4 changes: 1 addition & 3 deletions tests/ui/extern/extern-types-size_of_val.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ check-fail
//@ check-pass
#![feature(extern_types)]

use std::mem::{align_of_val, size_of_val};
Expand All @@ -11,7 +11,5 @@ fn main() {
let x: &A = unsafe { &*(1usize as *const A) };

size_of_val(x);
//~^ ERROR the size for values of type `A` cannot be known
align_of_val(x);
//~^ ERROR the size for values of type `A` cannot be known
}
39 changes: 0 additions & 39 deletions tests/ui/extern/extern-types-size_of_val.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/extern/extern-types-unsized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ fn main() {

assert_sized::<Bar<A>>();
//~^ ERROR the size for values of type
//~| ERROR the size for values of type

assert_sized::<Bar<Bar<A>>>();
//~^ ERROR the size for values of type
//~| ERROR the size for values of type
}
30 changes: 2 additions & 28 deletions tests/ui/extern/extern-types-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,8 @@ help: consider relaxing the implicit `Sized` restriction
LL | fn assert_sized<T: ?Sized>() {}
| ++++++++

error[E0277]: the size for values of type `A` cannot be known
--> $DIR/extern-types-unsized.rs:28:20
|
LL | assert_sized::<Bar<A>>();
| ^^^^^^ doesn't have a known size
|
= help: the trait `MetaSized` is not implemented for `A`
note: required by a bound in `Bar`
--> $DIR/extern-types-unsized.rs:14:12
|
LL | struct Bar<T: ?Sized> {
| ^ required by this bound in `Bar`

error[E0277]: the size for values of type `A` cannot be known at compilation time
--> $DIR/extern-types-unsized.rs:32:20
--> $DIR/extern-types-unsized.rs:31:20
|
LL | assert_sized::<Bar<Bar<A>>>();
| ^^^^^^^^^^^ doesn't have a size known at compile-time
Expand All @@ -94,19 +81,6 @@ help: consider relaxing the implicit `Sized` restriction
LL | fn assert_sized<T: ?Sized>() {}
| ++++++++

error[E0277]: the size for values of type `A` cannot be known
--> $DIR/extern-types-unsized.rs:32:20
|
LL | assert_sized::<Bar<Bar<A>>>();
| ^^^^^^^^^^^ doesn't have a known size
|
= help: the trait `MetaSized` is not implemented for `A`
note: required by a bound in `Bar`
--> $DIR/extern-types-unsized.rs:14:12
|
LL | struct Bar<T: ?Sized> {
| ^ required by this bound in `Bar`

error: aborting due to 6 previous errors
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0277`.
4 changes: 0 additions & 4 deletions tests/ui/extern/unsized-extern-derefmove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ extern "C" {
}

unsafe fn make_device() -> Box<Device> {
//~^ ERROR the size for values of type `Device` cannot be known
Box::from_raw(0 as *mut _)
//~^ ERROR the size for values of type `Device` cannot be known
//~| ERROR the size for values of type `Device` cannot be known
}

fn main() {
let d: Device = unsafe { *make_device() };
//~^ ERROR the size for values of type `Device` cannot be known
//~| ERROR the size for values of type `Device` cannot be known
}
52 changes: 2 additions & 50 deletions tests/ui/extern/unsized-extern-derefmove.stderr
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
error[E0277]: the size for values of type `Device` cannot be known
--> $DIR/unsized-extern-derefmove.rs:9:28
|
LL | unsafe fn make_device() -> Box<Device> {
| ^^^^^^^^^^^ doesn't have a known size
|
= help: the trait `MetaSized` is not implemented for `Device`
note: required by a bound in `Box`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL

error[E0277]: the size for values of type `Device` cannot be known
--> $DIR/unsized-extern-derefmove.rs:11:19
|
LL | Box::from_raw(0 as *mut _)
| ------------- ^^^^^^^^^^^ the trait `MetaSized` is not implemented for `Device`
| |
| required by a bound introduced by this call
|
= note: the trait bound `Device: MetaSized` is not satisfied
note: required by a bound in `Box::<T>::from_raw`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
help: consider borrowing here
|
LL | Box::from_raw(&(0 as *mut _))
| ++ +
LL | Box::from_raw(&mut (0 as *mut _))
| ++++++ +

error[E0277]: the size for values of type `Device` cannot be known
--> $DIR/unsized-extern-derefmove.rs:11:5
|
LL | Box::from_raw(0 as *mut _)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a known size
|
= help: the trait `MetaSized` is not implemented for `Device`
note: required by a bound in `Box`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL

error[E0277]: the size for values of type `Device` cannot be known at compilation time
--> $DIR/unsized-extern-derefmove.rs:17:9
--> $DIR/unsized-extern-derefmove.rs:14:9
|
LL | let d: Device = unsafe { *make_device() };
| ^ doesn't have a size known at compile-time
Expand All @@ -49,16 +11,6 @@ help: consider borrowing here
LL | let d: &Device = unsafe { *make_device() };
| +

error[E0277]: the size for values of type `Device` cannot be known
--> $DIR/unsized-extern-derefmove.rs:17:31
|
LL | let d: Device = unsafe { *make_device() };
| ^^^^^^^^^^^^^ doesn't have a known size
|
= help: the trait `MetaSized` is not implemented for `Device`
note: required by a bound in `Box`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL

error: aborting due to 5 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/layout/unconstrained-param-ice-137308.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ impl<C: ?Sized> A for u8 { //~ ERROR: the type parameter `C` is not constrained
#[rustc_layout(debug)]
struct S([u8; <u8 as A>::B]);
//~^ ERROR: the type has an unknown layout
//~| ERROR: type annotations needed
11 changes: 2 additions & 9 deletions tests/ui/layout/unconstrained-param-ice-137308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ error[E0207]: the type parameter `C` is not constrained by the impl trait, self
LL | impl<C: ?Sized> A for u8 {
| ^ unconstrained type parameter

error[E0282]: type annotations needed
--> $DIR/unconstrained-param-ice-137308.rs:18:16
|
LL | struct S([u8; <u8 as A>::B]);
| ^^ cannot infer type for type parameter `C`

error: the type has an unknown layout
--> $DIR/unconstrained-param-ice-137308.rs:18:1
|
LL | struct S([u8; <u8 as A>::B]);
| ^^^^^^^^

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0207, E0282.
For more information about an error, try `rustc --explain E0207`.
For more information about this error, try `rustc --explain E0207`.
2 changes: 1 addition & 1 deletion tests/ui/nll/issue-50716.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ trait A {
type X: ?Sized;
}

fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) //~ ERROR
fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
where
for<'b> &'b T: A,
<&'static T as A>::X: Sized
Expand Down
18 changes: 1 addition & 17 deletions tests/ui/nll/issue-50716.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
error[E0308]: mismatched types
--> $DIR/issue-50716.rs:8:27
|
LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected trait `<<&'a T as A>::X as MetaSized>`
found trait `<<&'static T as A>::X as MetaSized>`
note: the lifetime `'a` as defined here...
--> $DIR/issue-50716.rs:8:8
|
LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
| ^^
= note: ...does not necessarily outlive the static lifetime

error: lifetime may not live long enough
--> $DIR/issue-50716.rs:13:14
|
Expand All @@ -22,6 +7,5 @@ LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
LL | let _x = *s;
| ^^ proving this value is `Sized` requires that `'a` must outlive `'static`

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/incomplete-inference-issue-143992.rs:27:28
|
LL | let _x = T::Assoc::new(());
| ------------- ^^ expected `[u32; 1]`, found `()`
| |
| arguments to this function are incorrect
|
note: associated function defined here
--> $DIR/incomplete-inference-issue-143992.rs:18:8
|
LL | fn new(r: R) -> R {
| ^^^ ----

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
29 changes: 29 additions & 0 deletions tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ compile-flags: --crate-type=lib
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[current] check-pass
//@[next] compile-flags: -Znext-solver
//@[next] check-fail

// Test that we avoid incomplete inference when normalizing. Without this,
// `Trait`'s implicit `MetaSized` supertrait requires proving `T::Assoc<_>: MetaSized`
// before checking the `new` arguments, resulting in eagerly constraining the inference
// var to `u32`. This is undesirable and would breaking code.

pub trait Trait {
type Assoc<G>: OtherTrait<G>;
}

pub trait OtherTrait<R> {
fn new(r: R) -> R {
r
}
}

pub fn function<T: Trait>()
where
T::Assoc<[u32; 1]>: Clone,
{
let _x = T::Assoc::new(());
//[next]~^ ERROR mismatched types
}
Loading
Loading