Skip to content

Commit fa90379

Browse files
Rollup merge of rust-lang#152548 - dianne:reject-const-block-pat-pre-expansion, r=fmease
reject inline const patterns pre-expansion Reverts the parser changes from rust-lang#149667 Fixes rust-lang#152499 Awkwardly, some cases of inline const pats can only be caught pre-expansion and some can only be caught post-expansion. rust-lang#149667 switched from only rejecting the former to only rejecting the latter.
2 parents e76c9ca + 60f802f commit fa90379

File tree

7 files changed

+91
-52
lines changed

7 files changed

+91
-52
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ impl<'a> Parser<'a> {
15241524
},
15251525
)
15261526
} else if this.check_inline_const(0) {
1527-
this.parse_const_block(lo)
1527+
this.parse_const_block(lo, false)
15281528
} else if this.may_recover() && this.is_do_catch_block() {
15291529
this.recover_do_catch()
15301530
} else if this.is_try_block() {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ impl<'a> Parser<'a> {
12701270
}
12711271

12721272
/// Parses inline const expressions.
1273-
fn parse_const_block(&mut self, span: Span) -> PResult<'a, Box<Expr>> {
1273+
fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, Box<Expr>> {
12741274
self.expect_keyword(exp!(Const))?;
12751275
let (attrs, blk) = self.parse_inner_attrs_and_block(None)?;
12761276
let anon_const = AnonConst {
@@ -1279,7 +1279,18 @@ impl<'a> Parser<'a> {
12791279
mgca_disambiguation: MgcaDisambiguation::AnonConst,
12801280
};
12811281
let blk_span = anon_const.value.span;
1282-
let kind = ExprKind::ConstBlock(anon_const);
1282+
let kind = if pat {
1283+
let guar = self
1284+
.dcx()
1285+
.struct_span_err(blk_span, "const blocks cannot be used as patterns")
1286+
.with_help(
1287+
"use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead",
1288+
)
1289+
.emit();
1290+
ExprKind::Err(guar)
1291+
} else {
1292+
ExprKind::ConstBlock(anon_const)
1293+
};
12831294
Ok(self.mk_expr_with_attrs(span.to(blk_span), kind, attrs))
12841295
}
12851296

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,8 @@ impl<'a> Parser<'a> {
785785
} else if self.eat_keyword(exp!(Box)) {
786786
self.parse_pat_box()?
787787
} else if self.check_inline_const(0) {
788-
// Parse `const pat`.
789-
// NOTE: This will always error later during AST lowering because
790-
// inline const cannot be used as patterns.
791-
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
788+
// Parse `const pat`
789+
let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;
792790

793791
if let Some(re) = self.parse_range_end() {
794792
self.parse_pat_range_begin_with(const_expr, re)?
@@ -1283,7 +1281,7 @@ impl<'a> Parser<'a> {
12831281
.then_some(self.prev_token.span);
12841282

12851283
let bound = if self.check_inline_const(0) {
1286-
self.parse_const_block(self.token.span)
1284+
self.parse_const_block(self.token.span, true)
12871285
} else if self.check_path() {
12881286
let lo = self.token.span;
12891287
let (qself, path) = if self.eat_lt() {

tests/ui/inline-const/in-pat-recovery.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,63 @@
44
fn main() {
55
match 1 {
66
const { 1 + 7 } => {}
7-
//~^ ERROR arbitrary expressions aren't allowed in patterns
7+
//~^ ERROR const blocks cannot be used as patterns
88
2 => {}
99
_ => {}
1010
}
1111

1212
match 5 {
1313
const { 1 } ..= 10 => {}
14-
//~^ ERROR arbitrary expressions aren't allowed in patterns
14+
//~^ ERROR const blocks cannot be used as patterns
1515
_ => {}
1616
}
1717

1818
match 5 {
1919
1 ..= const { 10 } => {}
20-
//~^ ERROR arbitrary expressions aren't allowed in patterns
20+
//~^ ERROR const blocks cannot be used as patterns
2121
_ => {}
2222
}
2323

2424
match 5 {
2525
const { 1 } ..= const { 10 } => {}
26-
//~^ ERROR arbitrary expressions aren't allowed in patterns
27-
//~| ERROR arbitrary expressions aren't allowed in patterns
26+
//~^ ERROR const blocks cannot be used as patterns
27+
//~| ERROR const blocks cannot be used as patterns
2828
_ => {}
2929
}
3030

3131
match 5 {
3232
const { 1 } .. 10 => {}
33-
//~^ ERROR arbitrary expressions aren't allowed in patterns
33+
//~^ ERROR const blocks cannot be used as patterns
3434
_ => {}
3535
}
3636

3737
match 5 {
3838
1 .. const { 10 } => {}
39-
//~^ ERROR arbitrary expressions aren't allowed in patterns
39+
//~^ ERROR const blocks cannot be used as patterns
4040
_ => {}
4141
}
4242

4343
match 5 {
4444
const { 1 + 2 } ..= 10 => {}
45-
//~^ ERROR arbitrary expressions aren't allowed in patterns
45+
//~^ ERROR const blocks cannot be used as patterns
4646
_ => {}
4747
}
4848

4949
match 5 {
5050
1 ..= const { 5 + 5 } => {}
51-
//~^ ERROR arbitrary expressions aren't allowed in patterns
51+
//~^ ERROR const blocks cannot be used as patterns
5252
_ => {}
5353
}
5454

5555
match 5 {
5656
const { 3 } .. => {}
57-
//~^ ERROR arbitrary expressions aren't allowed in patterns
57+
//~^ ERROR const blocks cannot be used as patterns
5858
_ => {}
5959
}
6060

6161
match 5 {
6262
..= const { 7 } => {}
63-
//~^ ERROR arbitrary expressions aren't allowed in patterns
63+
//~^ ERROR const blocks cannot be used as patterns
6464
_ => {}
6565
}
6666
}

tests/ui/inline-const/in-pat-recovery.stderr

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
error: arbitrary expressions aren't allowed in patterns
2-
--> $DIR/in-pat-recovery.rs:6:9
1+
error: const blocks cannot be used as patterns
2+
--> $DIR/in-pat-recovery.rs:6:15
33
|
44
LL | const { 1 + 7 } => {}
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^
66
|
77
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
88

9-
error: arbitrary expressions aren't allowed in patterns
10-
--> $DIR/in-pat-recovery.rs:13:9
9+
error: const blocks cannot be used as patterns
10+
--> $DIR/in-pat-recovery.rs:13:15
1111
|
1212
LL | const { 1 } ..= 10 => {}
13-
| ^^^^^^^^^^^
13+
| ^^^^^
1414
|
1515
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
1616

17-
error: arbitrary expressions aren't allowed in patterns
18-
--> $DIR/in-pat-recovery.rs:19:15
17+
error: const blocks cannot be used as patterns
18+
--> $DIR/in-pat-recovery.rs:19:21
1919
|
2020
LL | 1 ..= const { 10 } => {}
21-
| ^^^^^^^^^^^^
21+
| ^^^^^^
2222
|
2323
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
2424

25-
error: arbitrary expressions aren't allowed in patterns
26-
--> $DIR/in-pat-recovery.rs:25:9
25+
error: const blocks cannot be used as patterns
26+
--> $DIR/in-pat-recovery.rs:25:15
2727
|
2828
LL | const { 1 } ..= const { 10 } => {}
29-
| ^^^^^^^^^^^
29+
| ^^^^^
3030
|
3131
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
3232

33-
error: arbitrary expressions aren't allowed in patterns
34-
--> $DIR/in-pat-recovery.rs:25:25
33+
error: const blocks cannot be used as patterns
34+
--> $DIR/in-pat-recovery.rs:25:31
3535
|
3636
LL | const { 1 } ..= const { 10 } => {}
37-
| ^^^^^^^^^^^^
37+
| ^^^^^^
3838
|
3939
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
4040

41-
error: arbitrary expressions aren't allowed in patterns
42-
--> $DIR/in-pat-recovery.rs:32:9
41+
error: const blocks cannot be used as patterns
42+
--> $DIR/in-pat-recovery.rs:32:15
4343
|
4444
LL | const { 1 } .. 10 => {}
45-
| ^^^^^^^^^^^
45+
| ^^^^^
4646
|
4747
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
4848

49-
error: arbitrary expressions aren't allowed in patterns
50-
--> $DIR/in-pat-recovery.rs:38:14
49+
error: const blocks cannot be used as patterns
50+
--> $DIR/in-pat-recovery.rs:38:20
5151
|
5252
LL | 1 .. const { 10 } => {}
53-
| ^^^^^^^^^^^^
53+
| ^^^^^^
5454
|
5555
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
5656

57-
error: arbitrary expressions aren't allowed in patterns
58-
--> $DIR/in-pat-recovery.rs:44:9
57+
error: const blocks cannot be used as patterns
58+
--> $DIR/in-pat-recovery.rs:44:15
5959
|
6060
LL | const { 1 + 2 } ..= 10 => {}
61-
| ^^^^^^^^^^^^^^^
61+
| ^^^^^^^^^
6262
|
6363
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
6464

65-
error: arbitrary expressions aren't allowed in patterns
66-
--> $DIR/in-pat-recovery.rs:50:15
65+
error: const blocks cannot be used as patterns
66+
--> $DIR/in-pat-recovery.rs:50:21
6767
|
6868
LL | 1 ..= const { 5 + 5 } => {}
69-
| ^^^^^^^^^^^^^^^
69+
| ^^^^^^^^^
7070
|
7171
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
7272

73-
error: arbitrary expressions aren't allowed in patterns
74-
--> $DIR/in-pat-recovery.rs:56:9
73+
error: const blocks cannot be used as patterns
74+
--> $DIR/in-pat-recovery.rs:56:15
7575
|
7676
LL | const { 3 } .. => {}
77-
| ^^^^^^^^^^^
77+
| ^^^^^
7878
|
7979
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
8080

81-
error: arbitrary expressions aren't allowed in patterns
82-
--> $DIR/in-pat-recovery.rs:62:13
81+
error: const blocks cannot be used as patterns
82+
--> $DIR/in-pat-recovery.rs:62:19
8383
|
8484
LL | ..= const { 7 } => {}
85-
| ^^^^^^^^^^^
85+
| ^^^^^
8686
|
8787
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
8888

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/152499>: reject inline const
2+
//! patterns pre-expansion when possible.
3+
4+
macro_rules! analyze { ($p:pat) => {}; }
5+
analyze!(const { 0 });
6+
//~^ ERROR: const blocks cannot be used as patterns
7+
8+
#[cfg(false)]
9+
fn scope() { let const { 0 }; }
10+
//~^ ERROR: const blocks cannot be used as patterns
11+
12+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: const blocks cannot be used as patterns
2+
--> $DIR/reject-const-block-pat-pre-expansion.rs:9:24
3+
|
4+
LL | fn scope() { let const { 0 }; }
5+
| ^^^^^
6+
|
7+
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
8+
9+
error: const blocks cannot be used as patterns
10+
--> $DIR/reject-const-block-pat-pre-expansion.rs:5:16
11+
|
12+
LL | analyze!(const { 0 });
13+
| ^^^^^
14+
|
15+
= help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)