Rollup merge of #109101 - compiler-errors:layout-err, r=michaelwoerister

Fall back to old metadata computation when type references errors

Projection is a bit too aggressive normalizing `<dyn Trait<[type error]> as Pointee>::Metadata` to `[type error]`, rather than to `DynMetadata<..>`. Side-step that by just falling back to the old structural metadata computation.

Fixes #109078
This commit is contained in:
Matthias Krüger 2023-03-14 17:40:05 +01:00 committed by GitHub
commit 1f159b4894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View File

@ -730,7 +730,11 @@ where
*/
};
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
// Projection eagerly bails out when the pointee references errors,
// fall back to structurally deducing metadata.
&& !pointee.references_error()
{
let metadata = tcx.normalize_erasing_regions(
cx.param_env(),
tcx.mk_projection(metadata_def_id, [pointee]),

View File

@ -156,7 +156,11 @@ fn layout_of_uncached<'tcx>(
let unsized_part = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
// Projection eagerly bails out when the pointee references errors,
// fall back to structurally deducing metadata.
&& !pointee.references_error()
{
let metadata_ty = tcx.normalize_erasing_regions(
param_env,
tcx.mk_projection(metadata_def_id, [pointee]),

View File

@ -0,0 +1,8 @@
trait Trait<T> {}
struct Bar(Box<dyn Trait<T>>);
//~^ ERROR cannot find type `T` in this scope
fn main() {
let x: Bar = unsafe { std::mem::transmute(()) };
}

View File

@ -0,0 +1,14 @@
error[E0412]: cannot find type `T` in this scope
--> $DIR/transmute-to-tail-with-err.rs:3:26
|
LL | struct Bar(Box<dyn Trait<T>>);
| ^ not found in this scope
|
help: you might be missing a type parameter
|
LL | struct Bar<T>(Box<dyn Trait<T>>);
| +++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0412`.