Skip to content

Commit

Permalink
Normalize field types before checking validity
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 23, 2024
1 parent 6265a95 commit dbf638e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
23 changes: 14 additions & 9 deletions compiler/rustc_const_eval/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,13 +810,18 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
let adt_def = self.tcx.adt_def(def_id);
assert!(adt_def.is_union());
assert_eq!(idx, FIRST_VARIANT);
let dest = adt_def.non_enum_variant().fields[field].ty(self.tcx, args);
if fields.len() != 1 {
let dest_ty = self.tcx.normalize_erasing_regions(
self.param_env,
adt_def.non_enum_variant().fields[field].ty(self.tcx, args),
);
if fields.len() == 1 {
let src_ty = fields.raw[0].ty(self.body, self.tcx);
if !self.mir_assign_valid_types(src_ty, dest_ty) {
self.fail(location, "union field has the wrong type");
}
} else {
self.fail(location, "unions should have one initialized field");
}
if !self.mir_assign_valid_types(fields.raw[0].ty(self.body, self.tcx), dest) {
self.fail(location, "union field has the wrong type");
}
}
AggregateKind::Adt(def_id, idx, args, _, None) => {
let adt_def = self.tcx.adt_def(def_id);
Expand All @@ -826,10 +831,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
self.fail(location, "adt has the wrong number of initialized fields");
}
for (src, dest) in std::iter::zip(fields, &variant.fields) {
if !self.mir_assign_valid_types(
src.ty(self.body, self.tcx),
dest.ty(self.tcx, args),
) {
let dest_ty = self
.tcx
.normalize_erasing_regions(self.param_env, dest.ty(self.tcx, args));
if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest_ty) {
self.fail(location, "adt field has the wrong type");
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/type-alias-impl-trait/struct-assignment-validity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// compile-flags: -Zvalidate-mir
// check-pass

// Check that we don't cause cycle errors when validating pre-`Reveal::All` MIR
// that assigns opaques through normalized projections.

#![feature(impl_trait_in_assoc_type)]

struct Bar;

trait Trait {
type Assoc;
fn foo() -> Foo;
}

impl Trait for Bar {
type Assoc = impl std::fmt::Debug;
fn foo() -> Foo {
let x: <Bar as Trait>::Assoc = ();
Foo { field: () }
}
}

struct Foo {
field: <Bar as Trait>::Assoc,
}

fn main() {}

0 comments on commit dbf638e

Please sign in to comment.