diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-05-09 23:56:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-09 23:56:09 +0200 |
| commit | 39edc68c690b2711def27f229ad3d113c55f6354 (patch) | |
| tree | a0ca4ffa4446344ec7af9c8292ddf259c0b4e120 /src/libsyntax/util | |
| parent | 62ab971bb7143601a8734a9d3f072232e46eb973 (diff) | |
| parent | 54430ad53ab00bf86090a8d11844db1c40b2ca24 (diff) | |
Rollup merge of #60188 - estebank:recover-block, r=varkor
Identify when a stmt could have been parsed as an expr There are some expressions that can be parsed as a statement without a trailing semicolon depending on the context, which can lead to confusing errors due to the same looking code being accepted in some places and not others. Identify these cases and suggest enclosing in parenthesis making the parse non-ambiguous without changing the accepted grammar. Fix #54186, cc #54482, fix #59975, fix #47287.
Diffstat (limited to 'src/libsyntax/util')
| -rw-r--r-- | src/libsyntax/util/parser.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libsyntax/util/parser.rs b/src/libsyntax/util/parser.rs index 80dabffaba9..86e89945afe 100644 --- a/src/libsyntax/util/parser.rs +++ b/src/libsyntax/util/parser.rs @@ -207,6 +207,31 @@ impl AssocOp { ObsoleteInPlace | Assign | AssignOp(_) | As | DotDot | DotDotEq | Colon => None } } + + /// This operator could be used to follow a block unambiguously. + /// + /// This is used for error recovery at the moment, providing a suggestion to wrap blocks with + /// parentheses while having a high degree of confidence on the correctness of the suggestion. + pub fn can_continue_expr_unambiguously(&self) -> bool { + use AssocOp::*; + match self { + BitXor | // `{ 42 } ^ 3` + Assign | // `{ 42 } = { 42 }` + Divide | // `{ 42 } / 42` + Modulus | // `{ 42 } % 2` + ShiftRight | // `{ 42 } >> 2` + LessEqual | // `{ 42 } <= 3` + Greater | // `{ 42 } > 3` + GreaterEqual | // `{ 42 } >= 3` + AssignOp(_) | // `{ 42 } +=` + LAnd | // `{ 42 } &&foo` + As | // `{ 42 } as usize` + // Equal | // `{ 42 } == { 42 }` Accepting these here would regress incorrect + // NotEqual | // `{ 42 } != { 42 } struct literals parser recovery. + Colon => true, // `{ 42 }: usize` + _ => false, + } + } } pub const PREC_RESET: i8 = -100; |
