Skip to content

Commit 200034a

Browse files
committed
Account for const fn with type params calls in const expr recovery
1 parent 00aeabc commit 200034a

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

src/librustc_parse/parser/path.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,10 @@ impl<'a> Parser<'a> {
456456
}
457457
})
458458
.is_some();
459-
// This will be true when a trait object type `Foo +` has been parsed.
460-
let was_op = self.prev_token.kind == token::BinOp(token::Plus);
459+
// This will be true when a trait object type `Foo +` or a path which was a `const fn` with
460+
// type params has been parsed.
461+
let was_op =
462+
matches!(self.prev_token.kind, token::BinOp(token::Plus | token::Shr) | token::Gt);
461463
if !is_op && !was_op {
462464
// We perform these checks and early return to avoid taking a snapshot unnecessarily.
463465
return Err(err);

src/test/ui/const-generics/const-expression-missing-braces.rs

+23
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,27 @@ fn e() {
2727
fn f() {
2828
foo::<100 - BAR>(); // ok
2929
}
30+
fn g() {
31+
foo::<bar<i32>()>(); //~ ERROR expected one of
32+
}
33+
fn h() {
34+
foo::<bar::<i32>()>(); //~ ERROR expected one of
35+
}
36+
fn i() {
37+
foo::<bar::<i32>() + BAR>(); //~ ERROR expected one of
38+
}
39+
fn j() {
40+
foo::<bar::<i32>() - BAR>(); //~ ERROR expected one of
41+
}
42+
fn k() {
43+
foo::<BAR - bar::<i32>()>(); //~ ERROR expected one of
44+
}
45+
fn l() {
46+
foo::<BAR - bar::<i32>()>(); //~ ERROR expected one of
47+
}
48+
49+
const fn bar<const C: usize>() -> usize {
50+
C
51+
}
52+
3053
fn main() {}

src/test/ui/const-generics/const-expression-missing-braces.stderr

+67-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,72 @@ help: to write a `const` expression, surround it with braces for it to be unambi
3131
LL | foo::<{ BAR - BAR }>();
3232
| ^ ^
3333

34+
error: expected one of `,` or `>`, found `(`
35+
--> $DIR/const-expression-missing-braces.rs:31:19
36+
|
37+
LL | foo::<bar<i32>()>();
38+
| ^ expected one of `,` or `>`
39+
|
40+
help: to write a `const` expression, surround it with braces for it to be unambiguous
41+
|
42+
LL | foo::<{ bar<i32>() }>();
43+
| ^ ^
44+
45+
error: expected one of `,` or `>`, found `(`
46+
--> $DIR/const-expression-missing-braces.rs:34:21
47+
|
48+
LL | foo::<bar::<i32>()>();
49+
| ^ expected one of `,` or `>`
50+
|
51+
help: to write a `const` expression, surround it with braces for it to be unambiguous
52+
|
53+
LL | foo::<{ bar::<i32>() }>();
54+
| ^ ^
55+
56+
error: expected one of `,` or `>`, found `(`
57+
--> $DIR/const-expression-missing-braces.rs:37:21
58+
|
59+
LL | foo::<bar::<i32>() + BAR>();
60+
| ^ expected one of `,` or `>`
61+
|
62+
help: to write a `const` expression, surround it with braces for it to be unambiguous
63+
|
64+
LL | foo::<{ bar::<i32>() + BAR }>();
65+
| ^ ^
66+
67+
error: expected one of `,` or `>`, found `(`
68+
--> $DIR/const-expression-missing-braces.rs:40:21
69+
|
70+
LL | foo::<bar::<i32>() - BAR>();
71+
| ^ expected one of `,` or `>`
72+
|
73+
help: to write a `const` expression, surround it with braces for it to be unambiguous
74+
|
75+
LL | foo::<{ bar::<i32>() - BAR }>();
76+
| ^ ^
77+
78+
error: expected one of `,` or `>`, found `-`
79+
--> $DIR/const-expression-missing-braces.rs:43:15
80+
|
81+
LL | foo::<BAR - bar::<i32>()>();
82+
| ^ expected one of `,` or `>`
83+
|
84+
help: to write a `const` expression, surround it with braces for it to be unambiguous
85+
|
86+
LL | foo::<{ BAR - bar::<i32>() }>();
87+
| ^ ^
88+
89+
error: expected one of `,` or `>`, found `-`
90+
--> $DIR/const-expression-missing-braces.rs:46:15
91+
|
92+
LL | foo::<BAR - bar::<i32>()>();
93+
| ^ expected one of `,` or `>`
94+
|
95+
help: to write a `const` expression, surround it with braces for it to be unambiguous
96+
|
97+
LL | foo::<{ BAR - bar::<i32>() }>();
98+
| ^ ^
99+
34100
error: likely `const` expression parsed as trait bounds
35101
--> $DIR/const-expression-missing-braces.rs:13:11
36102
|
@@ -42,5 +108,5 @@ help: if you meant to write a `const` expression, surround the expression with b
42108
LL | foo::<{ BAR + BAR }>();
43109
| ^ ^
44110

45-
error: aborting due to 4 previous errors
111+
error: aborting due to 10 previous errors
46112

0 commit comments

Comments
 (0)