Skip to content

Commit

Permalink
Some cleanup to maybe_parse_struct_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov authored and estebank committed Apr 19, 2019
1 parent 4c4ca60 commit aa393b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 54 deletions.
31 changes: 13 additions & 18 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2910,27 +2910,22 @@ impl<'a> Parser<'a> {
path: &ast::Path,
attrs: &ThinVec<Attribute>,
) -> Option<PResult<'a, P<Expr>>> {
// We don't want to assume it's a struct when encountering `{ <ident>: <ident> }` because
// it could be type ascription, like in `{ ident: u32 }`.
let isnt_ascription = self.look_ahead(1, |t| t.is_ident()) &&
self.look_ahead(2, |t| *t == token::Colon) && (
(self.look_ahead(3, |t| t.is_ident()) &&
self.look_ahead(4, |t| *t == token::Comma)) ||
self.look_ahead(3, |t| t.is_lit()) ||
self.look_ahead(3, |t| *t == token::BinOp(token::Minus)) &&
self.look_ahead(4, |t| t.is_lit())
);
let could_be_struct = self.look_ahead(1, |t| t.is_ident()) && (
self.look_ahead(2, |t| *t == token::Colon) && isnt_ascription
|| self.look_ahead(2, |t| *t == token::Comma)
// We could also check for `token::CloseDelim(token::Brace)`, but that would
// have false positives in the case of `if x == y { z } { a }`.
let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
let certainly_not_a_block = || self.look_ahead(1, |t| t.is_ident()) && (
// `{ ident, ` cannot start a block
self.look_ahead(2, |t| t == &token::Comma) ||
self.look_ahead(2, |t| t == &token::Colon) && (
// `{ ident: token, ` cannot start a block
self.look_ahead(4, |t| t == &token::Comma) ||
// `{ ident: ` cannot start a block unless it's a type ascription `ident: Type`
self.look_ahead(3, |t| !t.can_begin_type())
)
);
let bad_struct = self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
if !bad_struct || could_be_struct {

if struct_allowed || certainly_not_a_block() {
// This is a struct literal, but we don't can't accept them here
let expr = self.parse_struct_expr(lo, path.clone(), attrs.clone());
if let (Ok(expr), true) = (&expr, bad_struct) {
if let (Ok(expr), false) = (&expr, struct_allowed) {
let mut err = self.diagnostic().struct_span_err(
expr.span,
"struct literals are not allowed here",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ LL | }) {
|

error: aborting due to previous error

4 changes: 1 addition & 3 deletions src/test/ui/struct-literal-variant-in-if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ fn test_E(x: E) {
if x == E::I { field1: true, field2: 42 } {}
//~^ ERROR struct literals are not allowed here
if x == E::V { field: false } {}
//~^ ERROR expected identifier, found keyword `false`
//~| ERROR expected type, found keyword `false`
//~| ERROR expected value, found struct variant `E::V`
//~^ ERROR struct literals are not allowed here
if x == E::J { field: -42 } {}
//~^ ERROR struct literals are not allowed here
if x == E::K { field: "" } {}
Expand Down
44 changes: 11 additions & 33 deletions src/test/ui/struct-literal-variant-in-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,18 @@ help: surround the struct literal with parenthesis
LL | if x == (E::I { field1: true, field2: 42 }) {}
| ^ ^

error: expected identifier, found keyword `false`
--> $DIR/struct-literal-variant-in-if.rs:15:27
|
LL | if x == E::V { field: false } {}
| ^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
|
LL | if x == E::V { field: r#false } {}
| ^^^^^^^

error: expected type, found keyword `false`
--> $DIR/struct-literal-variant-in-if.rs:15:27
error: struct literals are not allowed here
--> $DIR/struct-literal-variant-in-if.rs:15:13
|
LL | if x == E::V { field: false } {}
| ^^^^^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-variant-in-if.rs:15:20
| ^^^^^^^^^^^^^^^^^^^^^
help: surround the struct literal with parenthesis
|
LL | if x == E::V { field: false } {}
| ^^^^^
= help: this might be indicative of a syntax error elsewhere
LL | if x == (E::V { field: false }) {}
| ^ ^

error: struct literals are not allowed here
--> $DIR/struct-literal-variant-in-if.rs:19:13
--> $DIR/struct-literal-variant-in-if.rs:17:13
|
LL | if x == E::J { field: -42 } {}
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -43,7 +29,7 @@ LL | if x == (E::J { field: -42 }) {}
| ^ ^

error: struct literals are not allowed here
--> $DIR/struct-literal-variant-in-if.rs:21:13
--> $DIR/struct-literal-variant-in-if.rs:19:13
|
LL | if x == E::K { field: "" } {}
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -60,14 +46,6 @@ LL | if x == E::V { field } {}
| |
| help: surround the struct literal with parenthesis: `(E::V { field })`

error[E0423]: expected value, found struct variant `E::V`
--> $DIR/struct-literal-variant-in-if.rs:15:13
|
LL | if x == E::V { field: false } {}
| ^^^^-----------------
| |
| help: surround the struct literal with parenthesis: `(E::V { field: false })`

error[E0308]: mismatched types
--> $DIR/struct-literal-variant-in-if.rs:10:20
|
Expand All @@ -81,15 +59,15 @@ LL | if x == E::V { field } {}
found type `bool`

error[E0308]: mismatched types
--> $DIR/struct-literal-variant-in-if.rs:23:20
--> $DIR/struct-literal-variant-in-if.rs:21:20
|
LL | let y: usize = ();
| ^^ expected usize, found ()
|
= note: expected type `usize`
found type `()`

error: aborting due to 9 previous errors
error: aborting due to 7 previous errors

Some errors occurred: E0308, E0423.
Some errors have detailed explanations: E0308, E0423.
For more information about an error, try `rustc --explain E0308`.

0 comments on commit aa393b0

Please sign in to comment.