Skip to content

Commit 529fd2d

Browse files
committed
suggest adding { .. } around a const function with arguments
1 parent d53e195 commit 529fd2d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

compiler/rustc_parse/src/parser/path.rs

+9
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,18 @@ impl<'a> Parser<'a> {
624624
GenericArg::Const(self.parse_const_arg()?)
625625
} else if self.check_type() {
626626
// Parse type argument.
627+
let is_const_fn = self.look_ahead(1, |t| t.kind == token::OpenDelim(token::Paren));
628+
let mut snapshot = self.clone();
627629
match self.parse_ty() {
628630
Ok(ty) => GenericArg::Type(ty),
629631
Err(err) => {
632+
if is_const_fn {
633+
if let Ok(expr) = snapshot.parse_expr_res(Restrictions::CONST_EXPR, None) {
634+
*self = snapshot;
635+
return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
636+
}
637+
}
638+
// self.parse_fn_call_expr();
630639
// Try to recover from possible `const` arg without braces.
631640
return self.recover_const_arg(start, err).map(Some);
632641
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn foo<const N: i32>() -> i32 {
2+
N
3+
}
4+
5+
const fn bar(n: i32, m: i32) -> i32 {
6+
n
7+
}
8+
9+
const fn baz() -> i32 {
10+
1
11+
}
12+
13+
const FOO: i32 = 3;
14+
15+
fn main() {
16+
foo::<baz()>(); //~ ERROR expected type, found function `baz`
17+
//~| ERROR unresolved item provided when a constant was expected
18+
foo::<bar(bar(1, 1), bar(1, 1))>(); //~ ERROR expected type, found `1`
19+
foo::<bar(1, 1)>(); //~ ERROR expected type, found `1`
20+
foo::<bar(FOO, 2)>(); //~ ERROR expected type, found `2`
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: expected type, found `1`
2+
--> $DIR/const-generic-function.rs:18:19
3+
|
4+
LL | foo::<bar(bar(1, 1), bar(1, 1))>();
5+
| ^ expected type
6+
|
7+
help: expressions must be enclosed in braces to be used as const generic arguments
8+
|
9+
LL | foo::<{ bar(bar(1, 1), bar(1, 1)) }>();
10+
| + +
11+
12+
error: expected type, found `1`
13+
--> $DIR/const-generic-function.rs:19:15
14+
|
15+
LL | foo::<bar(1, 1)>();
16+
| ^ expected type
17+
|
18+
help: expressions must be enclosed in braces to be used as const generic arguments
19+
|
20+
LL | foo::<{ bar(1, 1) }>();
21+
| + +
22+
23+
error: expected type, found `2`
24+
--> $DIR/const-generic-function.rs:20:20
25+
|
26+
LL | foo::<bar(FOO, 2)>();
27+
| ^ expected type
28+
|
29+
help: expressions must be enclosed in braces to be used as const generic arguments
30+
|
31+
LL | foo::<{ bar(FOO, 2) }>();
32+
| + +
33+
34+
error[E0573]: expected type, found function `baz`
35+
--> $DIR/const-generic-function.rs:16:11
36+
|
37+
LL | foo::<baz()>();
38+
| ^^^^^ not a type
39+
40+
error[E0747]: unresolved item provided when a constant was expected
41+
--> $DIR/const-generic-function.rs:16:11
42+
|
43+
LL | foo::<baz()>();
44+
| ^^^^^
45+
|
46+
help: if this generic argument was intended as a const parameter, surround it with braces
47+
|
48+
LL | foo::<{ baz() }>();
49+
| + +
50+
51+
error: aborting due to 5 previous errors
52+
53+
Some errors have detailed explanations: E0573, E0747.
54+
For more information about an error, try `rustc --explain E0573`.

0 commit comments

Comments
 (0)