Skip to content

Commit

Permalink
Normalize when equating dyn tails in MIR borrowck
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Aug 5, 2024
1 parent 5367673 commit ca4f422
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/cast/dyn-tails-need-normalization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ check-pass

trait Trait {
type Associated;
}

impl Trait for i32 {
type Associated = i64;
}

trait Generic<T> {}

type TraitObject = dyn Generic<<i32 as Trait>::Associated>;

struct Wrap(TraitObject);

fn cast(x: *mut TraitObject) {
x as *mut Wrap;
}

fn main() {}

0 comments on commit ca4f422

Please sign in to comment.