Skip to content

Commit

Permalink
Handle ty::Opaque correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 21, 2023
1 parent aaf18c2 commit 775726f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
14 changes: 13 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,19 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
self.patterns.is_empty()
}
fn head_ty(&self) -> Ty<'tcx> {
self.patterns[0].ty()
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
// version. Otherwise we could encounter constructors for the revealed type and crash.
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
let first_ty = self.patterns[0].ty();
if is_opaque(first_ty) {
for pat in &self.patterns {
let ty = pat.ty();
if !is_opaque(ty) {
return ty;
}
}
}
first_ty
}

fn analyze_ctors(&self, pcx: &PatCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'tcx> {
Expand Down
27 changes: 17 additions & 10 deletions tests/ui/type-alias-impl-trait/issue-96572-unconstrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ fn upvar() {
fn enum_upvar() {
type T = impl Copy;
let foo: T = Some((1u32, 2u32));
let x = move || {
match foo {
None => (),
Some((a, b)) => (),
}
let x = move || match foo {
None => (),
Some((a, b)) => (),
};
}

Expand Down Expand Up @@ -63,6 +61,19 @@ mod only_pattern {
None => {}
}
}

type V = impl Copy;

fn baz(baz: Option<V>) {
match baz {
_ => {}
Some((mut x, mut y)) => {
x = 42;
y = "foo";
}
None => {}
}
}
}

mod only_pattern_rpit {
Expand All @@ -71,11 +82,7 @@ mod only_pattern_rpit {
let (mut x, mut y) = foo(false);
x = 42;
y = "foo";
if b {
panic!()
} else {
foo(true)
}
if b { panic!() } else { foo(true) }
}

fn bar(b: bool) -> Option<impl Copy> {
Expand Down

0 comments on commit 775726f

Please sign in to comment.