Skip to content

Commit 78f0c7f

Browse files
committed
check_match: unify some lowering code and fix some ICEs
1 parent 71450c7 commit 78f0c7f

8 files changed

+100
-19
lines changed

src/librustc_mir_build/hair/pattern/_match.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -582,15 +582,12 @@ crate struct MatchCheckCtxt<'a, 'tcx> {
582582
}
583583

584584
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
585-
crate fn create_and_enter<F, R>(
585+
crate fn create_and_enter<R>(
586586
tcx: TyCtxt<'tcx>,
587587
param_env: ty::ParamEnv<'tcx>,
588588
module: DefId,
589-
f: F,
590-
) -> R
591-
where
592-
F: for<'b> FnOnce(MatchCheckCtxt<'b, 'tcx>) -> R,
593-
{
589+
f: impl for<'b> FnOnce(MatchCheckCtxt<'b, 'tcx>) -> R,
590+
) -> R {
594591
let pattern_arena = TypedArena::default();
595592

596593
f(MatchCheckCtxt { tcx, param_env, module, pattern_arena: &pattern_arena })

src/librustc_mir_build/hair/pattern/check_match.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
121121
check_for_bindings_named_same_as_variants(self, pat);
122122
}
123123

124+
fn lower_pattern<'p>(
125+
&self,
126+
cx: &mut MatchCheckCtxt<'p, 'tcx>,
127+
pat: &'tcx hir::Pat<'tcx>,
128+
have_errors: &mut bool,
129+
) -> (&'p super::Pat<'tcx>, Ty<'tcx>) {
130+
let mut patcx = PatCtxt::new(self.tcx, self.param_env, self.tables);
131+
patcx.include_lint_checks();
132+
let pattern = patcx.lower_pattern(pat);
133+
let pattern_ty = pattern.ty;
134+
let pattern: &_ = cx.pattern_arena.alloc(expand_pattern(cx, pattern));
135+
if !patcx.errors.is_empty() {
136+
*have_errors = true;
137+
patcx.report_inlining_errors(pat.span);
138+
}
139+
(pattern, pattern_ty)
140+
}
141+
124142
fn check_match(
125143
&mut self,
126144
scrut: &hir::Expr<'_>,
@@ -139,14 +157,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
139157
let inlined_arms: Vec<_> = arms
140158
.iter()
141159
.map(|arm| {
142-
let mut patcx = PatCtxt::new(self.tcx, self.param_env, self.tables);
143-
patcx.include_lint_checks();
144-
let pattern = patcx.lower_pattern(&arm.pat);
145-
let pattern: &_ = cx.pattern_arena.alloc(expand_pattern(cx, pattern));
146-
if !patcx.errors.is_empty() {
147-
patcx.report_inlining_errors(arm.pat.span);
148-
have_errors = true;
149-
}
160+
let (pattern, _) = self.lower_pattern(cx, &arm.pat, &mut have_errors);
150161
(pattern, &*arm.pat, arm.guard.is_some())
151162
})
152163
.collect();
@@ -171,11 +182,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
171182
fn check_irrefutable(&self, pat: &'tcx Pat<'tcx>, origin: &str, sp: Option<Span>) {
172183
let module = self.tcx.hir().get_module_parent(pat.hir_id);
173184
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |ref mut cx| {
174-
let mut patcx = PatCtxt::new(self.tcx, self.param_env, self.tables);
175-
patcx.include_lint_checks();
176-
let pattern = patcx.lower_pattern(pat);
177-
let pattern_ty = pattern.ty;
178-
let pattern = cx.pattern_arena.alloc(expand_pattern(cx, pattern));
185+
let (pattern, pattern_ty) = self.lower_pattern(cx, pat, &mut false);
179186
let pats: Matrix<'_, '_> = vec![PatStack::from_pattern(pattern)].into_iter().collect();
180187

181188
let witnesses = match check_not_useful(cx, pattern_ty, &pats, pat.hir_id) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub enum EFoo {
2+
A,
3+
}
4+
5+
pub trait Foo {
6+
const X: EFoo;
7+
}
8+
9+
struct Abc;
10+
11+
impl Foo for Abc {
12+
const X: EFoo = EFoo::A;
13+
}
14+
15+
struct Def;
16+
impl Foo for Def {
17+
const X: EFoo = EFoo::A;
18+
}
19+
20+
pub fn test<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
21+
//~^ ERROR associated consts cannot be referenced in patterns
22+
let A::X = arg;
23+
//~^ ERROR associated consts cannot be referenced in patterns
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0158]: associated consts cannot be referenced in patterns
2+
--> $DIR/issue-68393-let-pat-assoc-constant.rs:20:40
3+
|
4+
LL | pub fn test<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
5+
| ^^^^
6+
7+
error[E0158]: associated consts cannot be referenced in patterns
8+
--> $DIR/issue-68393-let-pat-assoc-constant.rs:22:9
9+
|
10+
LL | let A::X = arg;
11+
| ^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0158`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let x = 255u8;
3+
let 0u8..=x = 0;
4+
//~^ ERROR runtime values cannot be referenced in patterns
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0080]: runtime values cannot be referenced in patterns
2+
--> $DIR/issue-68394-let-pat-runtime-value.rs:3:15
3+
|
4+
LL | let 0u8..=x = 0;
5+
| ^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let 1234567890123456789012345678901234567890e-340: f64 = 0.0;
3+
//~^ ERROR could not evaluate float literal (see issue #31407)
4+
5+
fn param(1234567890123456789012345678901234567890e-340: f64) {}
6+
//~^ ERROR could not evaluate float literal (see issue #31407)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: could not evaluate float literal (see issue #31407)
2+
--> $DIR/issue-68396-let-float-bug.rs:2:9
3+
|
4+
LL | let 1234567890123456789012345678901234567890e-340: f64 = 0.0;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0080]: could not evaluate float literal (see issue #31407)
8+
--> $DIR/issue-68396-let-float-bug.rs:5:14
9+
|
10+
LL | fn param(1234567890123456789012345678901234567890e-340: f64) {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)