Skip to content

Commit

Permalink
Account for const fn with type params calls in const expr recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Apr 28, 2020
1 parent 00aeabc commit 200034a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/librustc_parse/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ impl<'a> Parser<'a> {
}
})
.is_some();
// This will be true when a trait object type `Foo +` has been parsed.
let was_op = self.prev_token.kind == token::BinOp(token::Plus);
// This will be true when a trait object type `Foo +` or a path which was a `const fn` with
// type params has been parsed.
let was_op =
matches!(self.prev_token.kind, token::BinOp(token::Plus | token::Shr) | token::Gt);
if !is_op && !was_op {
// We perform these checks and early return to avoid taking a snapshot unnecessarily.
return Err(err);
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/const-generics/const-expression-missing-braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,27 @@ fn e() {
fn f() {
foo::<100 - BAR>(); // ok
}
fn g() {
foo::<bar<i32>()>(); //~ ERROR expected one of
}
fn h() {
foo::<bar::<i32>()>(); //~ ERROR expected one of
}
fn i() {
foo::<bar::<i32>() + BAR>(); //~ ERROR expected one of
}
fn j() {
foo::<bar::<i32>() - BAR>(); //~ ERROR expected one of
}
fn k() {
foo::<BAR - bar::<i32>()>(); //~ ERROR expected one of
}
fn l() {
foo::<BAR - bar::<i32>()>(); //~ ERROR expected one of
}

const fn bar<const C: usize>() -> usize {
C
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,72 @@ help: to write a `const` expression, surround it with braces for it to be unambi
LL | foo::<{ BAR - BAR }>();
| ^ ^

error: expected one of `,` or `>`, found `(`
--> $DIR/const-expression-missing-braces.rs:31:19
|
LL | foo::<bar<i32>()>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ bar<i32>() }>();
| ^ ^

error: expected one of `,` or `>`, found `(`
--> $DIR/const-expression-missing-braces.rs:34:21
|
LL | foo::<bar::<i32>()>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ bar::<i32>() }>();
| ^ ^

error: expected one of `,` or `>`, found `(`
--> $DIR/const-expression-missing-braces.rs:37:21
|
LL | foo::<bar::<i32>() + BAR>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ bar::<i32>() + BAR }>();
| ^ ^

error: expected one of `,` or `>`, found `(`
--> $DIR/const-expression-missing-braces.rs:40:21
|
LL | foo::<bar::<i32>() - BAR>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ bar::<i32>() - BAR }>();
| ^ ^

error: expected one of `,` or `>`, found `-`
--> $DIR/const-expression-missing-braces.rs:43:15
|
LL | foo::<BAR - bar::<i32>()>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ BAR - bar::<i32>() }>();
| ^ ^

error: expected one of `,` or `>`, found `-`
--> $DIR/const-expression-missing-braces.rs:46:15
|
LL | foo::<BAR - bar::<i32>()>();
| ^ expected one of `,` or `>`
|
help: to write a `const` expression, surround it with braces for it to be unambiguous
|
LL | foo::<{ BAR - bar::<i32>() }>();
| ^ ^

error: likely `const` expression parsed as trait bounds
--> $DIR/const-expression-missing-braces.rs:13:11
|
Expand All @@ -42,5 +108,5 @@ help: if you meant to write a `const` expression, surround the expression with b
LL | foo::<{ BAR + BAR }>();
| ^ ^

error: aborting due to 4 previous errors
error: aborting due to 10 previous errors

0 comments on commit 200034a

Please sign in to comment.