Skip to content

Commit 9ed9025

Browse files
authored
Rollup merge of #90028 - tmiasko:structural-match-closure, r=spastorino
Reject closures in patterns Fixes #90013.
2 parents cbebdd8 + c97cf7f commit 9ed9025

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
130130
traits::NonStructuralMatchTy::Opaque => {
131131
"opaque types cannot be used in patterns".to_string()
132132
}
133+
traits::NonStructuralMatchTy::Closure => {
134+
"closures cannot be used in patterns".to_string()
135+
}
133136
traits::NonStructuralMatchTy::Generator => {
134137
"generators cannot be used in patterns".to_string()
135138
}

compiler/rustc_trait_selection/src/traits/structural_match.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum NonStructuralMatchTy<'tcx> {
1717
Dynamic,
1818
Foreign,
1919
Opaque,
20+
Closure,
2021
Generator,
2122
Projection,
2223
}
@@ -154,6 +155,9 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
154155
ty::Projection(..) => {
155156
return ControlFlow::Break(NonStructuralMatchTy::Projection);
156157
}
158+
ty::Closure(..) => {
159+
return ControlFlow::Break(NonStructuralMatchTy::Closure);
160+
}
157161
ty::Generator(..) | ty::GeneratorWitness(..) => {
158162
return ControlFlow::Break(NonStructuralMatchTy::Generator);
159163
}
@@ -197,7 +201,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
197201
// First check all contained types and then tell the caller to continue searching.
198202
return ty.super_visit_with(self);
199203
}
200-
ty::Closure(..) | ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => {
204+
ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => {
201205
bug!("unexpected type during structural-match checking: {:?}", ty);
202206
}
203207
ty::Error(_) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for issue 90013.
2+
// check-pass
3+
#![allow(incomplete_features)]
4+
#![feature(inline_const)]
5+
6+
fn main() {
7+
const { || {} };
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2021
2+
#![allow(incomplete_features)]
3+
#![allow(unreachable_code)]
4+
#![feature(const_async_blocks)]
5+
#![feature(inline_const)]
6+
7+
fn main() {
8+
match loop {} {
9+
const { || {} } => {}, //~ ERROR cannot be used in patterns
10+
}
11+
match loop {} {
12+
const { async {} } => {}, //~ ERROR cannot be used in patterns
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: `[closure@$DIR/non-structural-match-types.rs:9:17: 9:22]` cannot be used in patterns
2+
--> $DIR/non-structural-match-types.rs:9:9
3+
|
4+
LL | const { || {} } => {},
5+
| ^^^^^^^^^^^^^^^
6+
7+
error: `impl Future` cannot be used in patterns
8+
--> $DIR/non-structural-match-types.rs:12:9
9+
|
10+
LL | const { async {} } => {},
11+
| ^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)