Skip to content

Commit 5294992

Browse files
committed
Suggest adding missing braces in const block pattern
Previously it would only suggest wrapping the code in braces in regular expressions; now it does it in patterns too.
1 parent 9775ffe commit 5294992

File tree

8 files changed

+184
-26
lines changed

8 files changed

+184
-26
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ impl<'a> Parser<'a> {
10741074
})
10751075
} else if self.eat_keyword(kw::Unsafe) {
10761076
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
1077-
} else if self.check_inline_const(0) {
1077+
} else if self.eat_keyword(kw::Const) {
10781078
self.parse_const_block(lo.to(self.token.span))
10791079
} else if self.is_do_catch_block() {
10801080
self.recover_do_catch(attrs)

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,6 @@ impl<'a> Parser<'a> {
552552
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
553553
}
554554

555-
fn check_inline_const(&self, dist: usize) -> bool {
556-
self.is_keyword_ahead(dist, &[kw::Const])
557-
&& self.look_ahead(dist + 1, |t| match t.kind {
558-
token::Interpolated(ref nt) => matches!(**nt, token::NtBlock(..)),
559-
token::OpenDelim(DelimToken::Brace) => true,
560-
_ => false,
561-
})
562-
}
563-
564555
/// Checks to see if the next token is either `+` or `+=`.
565556
/// Otherwise returns `false`.
566557
fn check_plus(&mut self) -> bool {
@@ -897,10 +888,8 @@ impl<'a> Parser<'a> {
897888
}
898889
}
899890

900-
/// Parses inline const expressions.
891+
/// Parses inline const expressions. The `const` keyword was already eaten.
901892
fn parse_const_block(&mut self, span: Span) -> PResult<'a, P<Expr>> {
902-
self.sess.gated_spans.gate(sym::inline_const, span);
903-
self.eat_keyword(kw::Const);
904893
let blk = self.parse_block()?;
905894
let anon_const = AnonConst {
906895
id: DUMMY_NODE_ID,

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ impl<'a> Parser<'a> {
337337
let pat = self.parse_pat_with_range_pat(false, None)?;
338338
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
339339
PatKind::Box(pat)
340-
} else if self.check_inline_const(0) {
341-
// Parse `const pat`
340+
} else if self.eat_keyword(kw::Const) {
341+
// Parse `const { pat }`
342342
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
343343

344344
if let Some(re) = self.parse_range_end() {

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,14 @@ impl<'a> Parser<'a> {
303303
match self.parse_stmt_without_recovery() {
304304
// If the next token is an open brace (e.g., `if a b {`), the place-
305305
// inside-a-block suggestion would be more likely wrong than right.
306+
//
307+
// But we don't want to trigger this if we just parsed a pattern,
308+
// so this only triggers if the current token is neither `=>` nor `=`.
306309
Ok(Some(_))
307-
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
308-
|| do_not_suggest_help => {}
310+
if do_not_suggest_help
311+
|| (self.token != token::FatArrow
312+
&& self.token != token::Eq
313+
&& self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))) => {}
309314
Ok(Some(stmt)) => {
310315
let stmt_own_line = self.sess.source_map().is_line_before_span_empty(sp);
311316
let stmt_span = if stmt_own_line && self.eat(&token::Semi) {
@@ -319,7 +324,7 @@ impl<'a> Parser<'a> {
319324
stmt_span,
320325
"try placing this code inside a block",
321326
format!("{{ {} }}", snippet),
322-
// Speculative; has been misleading in the past (#46836).
327+
// Speculative; has been misleading in the past (see #46836).
323328
Applicability::MaybeIncorrect,
324329
);
325330
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// run-rustfix
2+
3+
// See issue #78168.
4+
5+
#![allow(incomplete_features)]
6+
#![feature(inline_const)]
7+
8+
// FIXME(#78171): the lint has to be allowed because of a bug
9+
#[allow(dead_code)]
10+
const fn one() -> i32 {
11+
1
12+
}
13+
14+
fn foo() -> i32 {
15+
let x = 2;
16+
17+
match x {
18+
const { 2 } => {}
19+
//~^ ERROR expected `{`, found `2`
20+
//~| HELP try placing this code inside a block
21+
_ => {}
22+
}
23+
24+
match x {
25+
const { 1 + 2 * 3 / 4 } => {}
26+
//~^ ERROR expected `{`, found `1`
27+
//~| HELP try placing this code inside a block
28+
_ => {}
29+
}
30+
31+
match x {
32+
const { one() } => {}
33+
//~^ ERROR expected `{`, found `one`
34+
//~| HELP try placing this code inside a block
35+
_ => {}
36+
}
37+
38+
x
39+
}
40+
41+
fn bar() -> i32 {
42+
let x = const { 2 };
43+
//~^ ERROR expected `{`, found `2`
44+
//~| HELP try placing this code inside a block
45+
46+
x
47+
}
48+
49+
fn baz() -> i32 {
50+
let y = const { 1 + 2 * 3 / 4 };
51+
//~^ ERROR expected `{`, found `1`
52+
//~| HELP try placing this code inside a block
53+
54+
y
55+
}
56+
57+
fn main() {
58+
foo();
59+
bar();
60+
baz();
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// run-rustfix
2+
3+
// See issue #78168.
4+
5+
#![allow(incomplete_features)]
6+
#![feature(inline_const)]
7+
8+
// FIXME(#78171): the lint has to be allowed because of a bug
9+
#[allow(dead_code)]
10+
const fn one() -> i32 {
11+
1
12+
}
13+
14+
fn foo() -> i32 {
15+
let x = 2;
16+
17+
match x {
18+
const 2 => {}
19+
//~^ ERROR expected `{`, found `2`
20+
//~| HELP try placing this code inside a block
21+
_ => {}
22+
}
23+
24+
match x {
25+
const 1 + 2 * 3 / 4 => {}
26+
//~^ ERROR expected `{`, found `1`
27+
//~| HELP try placing this code inside a block
28+
_ => {}
29+
}
30+
31+
match x {
32+
const one() => {}
33+
//~^ ERROR expected `{`, found `one`
34+
//~| HELP try placing this code inside a block
35+
_ => {}
36+
}
37+
38+
x
39+
}
40+
41+
fn bar() -> i32 {
42+
let x = const 2;
43+
//~^ ERROR expected `{`, found `2`
44+
//~| HELP try placing this code inside a block
45+
46+
x
47+
}
48+
49+
fn baz() -> i32 {
50+
let y = const 1 + 2 * 3 / 4;
51+
//~^ ERROR expected `{`, found `1`
52+
//~| HELP try placing this code inside a block
53+
54+
y
55+
}
56+
57+
fn main() {
58+
foo();
59+
bar();
60+
baz();
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: expected `{`, found `2`
2+
--> $DIR/inline-const-without-block.rs:18:15
3+
|
4+
LL | const 2 => {}
5+
| ^
6+
| |
7+
| expected `{`
8+
| help: try placing this code inside a block: `{ 2 }`
9+
10+
error: expected `{`, found `1`
11+
--> $DIR/inline-const-without-block.rs:25:15
12+
|
13+
LL | const 1 + 2 * 3 / 4 => {}
14+
| ^------------
15+
| |
16+
| expected `{`
17+
| help: try placing this code inside a block: `{ 1 + 2 * 3 / 4 }`
18+
19+
error: expected `{`, found `one`
20+
--> $DIR/inline-const-without-block.rs:32:15
21+
|
22+
LL | const one() => {}
23+
| ^^^--
24+
| |
25+
| expected `{`
26+
| help: try placing this code inside a block: `{ one() }`
27+
28+
error: expected `{`, found `2`
29+
--> $DIR/inline-const-without-block.rs:42:19
30+
|
31+
LL | let x = const 2;
32+
| ^
33+
| |
34+
| expected `{`
35+
| help: try placing this code inside a block: `{ 2 }`
36+
37+
error: expected `{`, found `1`
38+
--> $DIR/inline-const-without-block.rs:50:19
39+
|
40+
LL | let y = const 1 + 2 * 3 / 4;
41+
| ^------------
42+
| |
43+
| expected `{`
44+
| help: try placing this code inside a block: `{ 1 + 2 * 3 / 4 }`
45+
46+
error: aborting due to 5 previous errors
47+
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
error: expected identifier, found keyword `const`
2-
--> $DIR/keyword-const-as-identifier.rs:4:9
1+
error: expected `{`, found `=`
2+
--> $DIR/keyword-const-as-identifier.rs:4:15
33
|
44
LL | let const = "foo";
5-
| ^^^^^ expected identifier, found keyword
6-
|
7-
help: you can escape reserved keywords to use them as identifiers
8-
|
9-
LL | let r#const = "foo";
10-
| ^^^^^^^
5+
| ^ expected `{`
116

127
error: aborting due to previous error
138

0 commit comments

Comments
 (0)