From c6f8672dd5e128766298ed0d53bb32a94188f886 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 5 Aug 2024 14:04:40 -0400 Subject: [PATCH] Normalize when equating dyn tails in MIR borrowck --- compiler/rustc_borrowck/src/type_check/mod.rs | 6 ++++++ tests/crashes/128621-2.rs | 16 ---------------- .../cast/dyn-tails-need-normalization.rs} | 4 +++- 3 files changed, 9 insertions(+), 17 deletions(-) delete mode 100644 tests/crashes/128621-2.rs rename tests/{crashes/128621.rs => ui/cast/dyn-tails-need-normalization.rs} (87%) diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index bbb5daccfd6..b13773ffe14 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -2330,10 +2330,16 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { match (cast_ty_from, cast_ty_to) { (Some(CastTy::Ptr(src)), Some(CastTy::Ptr(dst))) => { let mut normalize = |t| self.normalize(t, location); + + // N.B. `struct_tail_with_normalize` only "structurally resolves" + // the type. It is not fully normalized, so we have to normalize it + // afterwards. let src_tail = tcx.struct_tail_with_normalize(src.ty, &mut normalize, || ()); + let src_tail = normalize(src_tail); let dst_tail = tcx.struct_tail_with_normalize(dst.ty, &mut normalize, || ()); + let dst_tail = normalize(dst_tail); // This checks (lifetime part of) vtable validity for pointer casts, // which is irrelevant when there are aren't principal traits on both sides (aka only auto traits). diff --git a/tests/crashes/128621-2.rs b/tests/crashes/128621-2.rs deleted file mode 100644 index b1cdaf94984..00000000000 --- a/tests/crashes/128621-2.rs +++ /dev/null @@ -1,16 +0,0 @@ -//@ known-bug: rust-lang/rust#128621 - -#![feature(ptr_metadata)] -use std::{ops::FnMut, ptr::Pointee}; - -pub type EmplacerFn<'a, T> = dyn for<'b> FnMut(::Metadata) + 'a; - -pub struct Emplacer<'a, T>(EmplacerFn<'a, T>); - -impl<'a, T> Emplacer<'a, T> { - pub unsafe fn from_fn<'b>(emplacer_fn: &'b mut EmplacerFn<'a, T>) -> &'b mut Self { - unsafe { &mut *((emplacer_fn as *mut EmplacerFn<'a, T>) as *mut Self) } - } -} - -pub fn main() {} diff --git a/tests/crashes/128621.rs b/tests/ui/cast/dyn-tails-need-normalization.rs similarity index 87% rename from tests/crashes/128621.rs rename to tests/ui/cast/dyn-tails-need-normalization.rs index 0a02352236d..719e0e89243 100644 --- a/tests/crashes/128621.rs +++ b/tests/ui/cast/dyn-tails-need-normalization.rs @@ -1,4 +1,4 @@ -//@ known-bug: rust-lang/rust#128621 +//@ check-pass trait Trait { type Associated; @@ -17,3 +17,5 @@ struct Wrap(TraitObject); fn cast(x: *mut TraitObject) { x as *mut Wrap; } + +fn main() {}