Skip to content

Commit 40d933a

Browse files
authored
Rollup merge of #108705 - clubby789:refutable-let-closure-borrow, r=cjgillot
Prevent ICE with broken borrow in closure r? `@Nilstrieb` Fixes #108683 This solution isn't ideal, I'm hoping to find a way to continue compilation without ICEing.
2 parents 9a767b6 + 2d5ca0e commit 40d933a

File tree

5 files changed

+154
-13
lines changed

5 files changed

+154
-13
lines changed

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ rustc_queries! {
10161016
desc { "converting literal to mir constant" }
10171017
}
10181018

1019-
query check_match(key: LocalDefId) {
1019+
query check_match(key: LocalDefId) -> Result<(), rustc_errors::ErrorGuaranteed> {
10201020
desc { |tcx| "match-checking `{}`", tcx.def_path_str(key) }
10211021
cache_on_disk_if { true }
10221022
}

compiler/rustc_mir_build/src/build/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ fn mir_build(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
4242
// Ensure unsafeck and abstract const building is ran before we steal the THIR.
4343
tcx.ensure_with_value().thir_check_unsafety(def);
4444
tcx.ensure_with_value().thir_abstract_const(def);
45-
tcx.ensure_with_value().check_match(def);
45+
if let Err(e) = tcx.check_match(def) {
46+
return construct_error(tcx, def, e);
47+
}
4648

4749
let body = match tcx.thir_body(def) {
4850
Err(error_reported) => construct_error(tcx, def, error_reported),

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use rustc_session::Session;
2626
use rustc_span::hygiene::DesugaringKind;
2727
use rustc_span::Span;
2828

29-
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) {
30-
let Ok((thir, expr)) = tcx.thir_body(def_id) else { return };
29+
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
30+
let (thir, expr) = tcx.thir_body(def_id)?;
3131
let thir = thir.borrow();
3232
let pattern_arena = TypedArena::default();
3333
let mut visitor = MatchVisitor {
@@ -37,13 +37,16 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) {
3737
lint_level: tcx.hir().local_def_id_to_hir_id(def_id),
3838
let_source: LetSource::None,
3939
pattern_arena: &pattern_arena,
40+
error: Ok(()),
4041
};
4142
visitor.visit_expr(&thir[expr]);
43+
4244
for param in thir.params.iter() {
4345
if let Some(box ref pattern) = param.pat {
4446
visitor.check_irrefutable(pattern, "function argument", None);
4547
}
4648
}
49+
visitor.error
4750
}
4851

4952
fn create_e0004(
@@ -77,6 +80,7 @@ struct MatchVisitor<'a, 'p, 'tcx> {
7780
lint_level: HirId,
7881
let_source: LetSource,
7982
pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
83+
error: Result<(), ErrorGuaranteed>,
8084
}
8185

8286
impl<'a, 'tcx> Visitor<'a, 'tcx> for MatchVisitor<'a, '_, 'tcx> {
@@ -276,9 +280,9 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
276280
let [pat_field] = &subpatterns[..] else { bug!() };
277281
self.check_irrefutable(&pat_field.pattern, "`for` loop binding", None);
278282
} else {
279-
non_exhaustive_match(
283+
self.error = Err(non_exhaustive_match(
280284
&cx, self.thir, scrut_ty, scrut.span, witnesses, arms, expr_span,
281-
);
285+
));
282286
}
283287
}
284288
}
@@ -406,7 +410,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
406410
}
407411

408412
#[instrument(level = "trace", skip(self))]
409-
fn check_irrefutable(&self, pat: &Pat<'tcx>, origin: &str, sp: Option<Span>) {
413+
fn check_irrefutable(&mut self, pat: &Pat<'tcx>, origin: &str, sp: Option<Span>) {
410414
let mut cx = self.new_cx(self.lint_level, false);
411415

412416
let pattern = self.lower_pattern(&mut cx, pat);
@@ -475,7 +479,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
475479
AdtDefinedHere { adt_def_span, ty, variants }
476480
};
477481

478-
self.tcx.sess.emit_err(PatternNotCovered {
482+
self.error = Err(self.tcx.sess.emit_err(PatternNotCovered {
479483
span: pat.span,
480484
origin,
481485
uncovered: Uncovered::new(pat.span, &cx, witnesses),
@@ -486,7 +490,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
486490
let_suggestion,
487491
misc_suggestion,
488492
adt_defined_here,
489-
});
493+
}));
490494
}
491495
}
492496

@@ -628,7 +632,7 @@ fn non_exhaustive_match<'p, 'tcx>(
628632
witnesses: Vec<DeconstructedPat<'p, 'tcx>>,
629633
arms: &[ArmId],
630634
expr_span: Span,
631-
) {
635+
) -> ErrorGuaranteed {
632636
let is_empty_match = arms.is_empty();
633637
let non_empty_enum = match scrut_ty.kind() {
634638
ty::Adt(def, _) => def.is_enum() && !def.variants().is_empty(),
@@ -640,13 +644,12 @@ fn non_exhaustive_match<'p, 'tcx>(
640644
let pattern;
641645
let patterns_len;
642646
if is_empty_match && !non_empty_enum {
643-
cx.tcx.sess.emit_err(NonExhaustivePatternsTypeNotEmpty {
647+
return cx.tcx.sess.emit_err(NonExhaustivePatternsTypeNotEmpty {
644648
cx,
645649
expr_span,
646650
span: sp,
647651
ty: scrut_ty,
648652
});
649-
return;
650653
} else {
651654
// FIXME: migration of this diagnostic will require list support
652655
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
@@ -797,7 +800,7 @@ fn non_exhaustive_match<'p, 'tcx>(
797800
} else {
798801
err.help(msg);
799802
}
800-
err.emit();
803+
err.emit()
801804
}
802805

803806
pub(crate) fn joined_uncovered_patterns<'p, 'tcx>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// regression test for #108683
2+
// edition:2021
3+
4+
enum Refutable {
5+
A,
6+
B,
7+
}
8+
9+
fn example(v1: u32, v2: [u32; 4], v3: Refutable) {
10+
const PAT: u32 = 0;
11+
let v4 = &v2[..];
12+
|| {
13+
let 0 = v1; //~ ERROR refutable pattern in local binding
14+
let (0 | 1) = v1; //~ ERROR refutable pattern in local binding
15+
let 1.. = v1; //~ ERROR refutable pattern in local binding
16+
let [0, 0, 0, 0] = v2; //~ ERROR refutable pattern in local binding
17+
let [0] = v4; //~ ERROR refutable pattern in local binding
18+
let Refutable::A = v3; //~ ERROR refutable pattern in local binding
19+
let PAT = v1; //~ ERROR refutable pattern in local binding
20+
};
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
error[E0005]: refutable pattern in local binding
2+
--> $DIR/bad-pattern.rs:13:13
3+
|
4+
LL | let 0 = v1;
5+
| ^ pattern `1_u32..=u32::MAX` not covered
6+
|
7+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
8+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
9+
= note: the matched value is of type `u32`
10+
help: you might want to use `if let` to ignore the variant that isn't matched
11+
|
12+
LL | if let 0 = v1 { todo!() };
13+
| ++ +++++++++++
14+
help: alternatively, you could prepend the pattern with an underscore to define a new named variable; identifiers cannot begin with digits
15+
|
16+
LL | let _0 = v1;
17+
| +
18+
19+
error[E0005]: refutable pattern in local binding
20+
--> $DIR/bad-pattern.rs:14:14
21+
|
22+
LL | let (0 | 1) = v1;
23+
| ^^^^^ pattern `2_u32..=u32::MAX` not covered
24+
|
25+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
26+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
27+
= note: the matched value is of type `u32`
28+
help: you might want to use `if let` to ignore the variant that isn't matched
29+
|
30+
LL | if let (0 | 1) = v1 { todo!() };
31+
| ++ +++++++++++
32+
33+
error[E0005]: refutable pattern in local binding
34+
--> $DIR/bad-pattern.rs:15:13
35+
|
36+
LL | let 1.. = v1;
37+
| ^^^ pattern `0_u32` not covered
38+
|
39+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
40+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
41+
= note: the matched value is of type `u32`
42+
help: you might want to use `if let` to ignore the variant that isn't matched
43+
|
44+
LL | if let 1.. = v1 { todo!() };
45+
| ++ +++++++++++
46+
47+
error[E0005]: refutable pattern in local binding
48+
--> $DIR/bad-pattern.rs:16:13
49+
|
50+
LL | let [0, 0, 0, 0] = v2;
51+
| ^^^^^^^^^^^^ pattern `[1_u32..=u32::MAX, _, _, _]` not covered
52+
|
53+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
54+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
55+
= note: the matched value is of type `[u32; 4]`
56+
help: you might want to use `if let` to ignore the variant that isn't matched
57+
|
58+
LL | if let [0, 0, 0, 0] = v2 { todo!() };
59+
| ++ +++++++++++
60+
61+
error[E0005]: refutable pattern in local binding
62+
--> $DIR/bad-pattern.rs:17:13
63+
|
64+
LL | let [0] = v4;
65+
| ^^^ patterns `&[]` and `&[_, _, ..]` not covered
66+
|
67+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
68+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
69+
= note: the matched value is of type `&[u32]`
70+
help: you might want to use `if let` to ignore the variants that aren't matched
71+
|
72+
LL | if let [0] = v4 { todo!() };
73+
| ++ +++++++++++
74+
75+
error[E0005]: refutable pattern in local binding
76+
--> $DIR/bad-pattern.rs:18:13
77+
|
78+
LL | let Refutable::A = v3;
79+
| ^^^^^^^^^^^^ pattern `Refutable::B` not covered
80+
|
81+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
82+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
83+
note: `Refutable` defined here
84+
--> $DIR/bad-pattern.rs:4:6
85+
|
86+
LL | enum Refutable {
87+
| ^^^^^^^^^
88+
LL | A,
89+
LL | B,
90+
| - not covered
91+
= note: the matched value is of type `Refutable`
92+
help: you might want to use `if let` to ignore the variant that isn't matched
93+
|
94+
LL | if let Refutable::A = v3 { todo!() };
95+
| ++ +++++++++++
96+
97+
error[E0005]: refutable pattern in local binding
98+
--> $DIR/bad-pattern.rs:19:13
99+
|
100+
LL | let PAT = v1;
101+
| ^^^
102+
| |
103+
| pattern `1_u32..=u32::MAX` not covered
104+
| missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
105+
| help: introduce a variable instead: `PAT_var`
106+
|
107+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
108+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
109+
= note: the matched value is of type `u32`
110+
111+
error: aborting due to 7 previous errors
112+
113+
For more information about this error, try `rustc --explain E0005`.

0 commit comments

Comments
 (0)