Skip to content

Commit 3fa2e71

Browse files
committed
Handle ty::Opaque correctly
1 parent d5070e3 commit 3fa2e71

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,22 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
883883
self.patterns.is_empty()
884884
}
885885
fn head_ty(&self) -> Option<Ty<'tcx>> {
886-
self.patterns.get(0).map(|p| p.ty())
886+
if self.patterns.len() == 0 {
887+
return None;
888+
}
889+
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
890+
// version. Otherwise we could encounter constructors for the revealed type and crash.
891+
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
892+
let first_ty = self.patterns[0].ty();
893+
if is_opaque(first_ty) {
894+
for pat in &self.patterns {
895+
let ty = pat.ty();
896+
if !is_opaque(ty) {
897+
return Some(ty);
898+
}
899+
}
900+
}
901+
Some(first_ty)
887902
}
888903

889904
fn analyze_ctors(&self, pcx: &PatCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'tcx> {

tests/ui/type-alias-impl-trait/issue-96572-unconstrained.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ fn upvar() {
2626
fn enum_upvar() {
2727
type T = impl Copy;
2828
let foo: T = Some((1u32, 2u32));
29-
let x = move || {
30-
match foo {
31-
None => (),
32-
Some((a, b)) => (),
33-
}
29+
let x = move || match foo {
30+
None => (),
31+
Some((a, b)) => (),
3432
};
3533
}
3634

@@ -63,6 +61,19 @@ mod only_pattern {
6361
None => {}
6462
}
6563
}
64+
65+
type V = impl Copy;
66+
67+
fn baz(baz: Option<V>) {
68+
match baz {
69+
_ => {}
70+
Some((mut x, mut y)) => {
71+
x = 42;
72+
y = "foo";
73+
}
74+
None => {}
75+
}
76+
}
6677
}
6778

6879
mod only_pattern_rpit {
@@ -71,11 +82,7 @@ mod only_pattern_rpit {
7182
let (mut x, mut y) = foo(false);
7283
x = 42;
7384
y = "foo";
74-
if b {
75-
panic!()
76-
} else {
77-
foo(true)
78-
}
85+
if b { panic!() } else { foo(true) }
7986
}
8087

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

0 commit comments

Comments
 (0)