File tree Expand file tree Collapse file tree 3 files changed +59
-4
lines changed
compiler/rustc_ast/src/util Expand file tree Collapse file tree 3 files changed +59
-4
lines changed Original file line number Diff line number Diff line change 22
33// Predicates on exprs and stmts that the pretty-printer and parser use
44
5- use crate :: ast;
5+ use crate :: { ast, token :: Delimiter } ;
66
77/// Does this expression require a semicolon to be treated
88/// as a statement? The negation of this: 'can this expression
@@ -59,8 +59,12 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
5959 | While ( ..)
6060 | ConstBlock ( _) => break Some ( expr) ,
6161
62- // FIXME: These can end in `}`, but changing these would break stable code.
63- InlineAsm ( _) | OffsetOf ( _, _) | MacCall ( _) | IncludedBytes ( _) | FormatArgs ( _) => {
62+ MacCall ( mac) => {
63+ break ( mac. args . delim == Delimiter :: Brace ) . then_some ( expr) ;
64+ }
65+
66+ InlineAsm ( _) | OffsetOf ( _, _) | IncludedBytes ( _) | FormatArgs ( _) => {
67+ // These should have been denied pre-expansion.
6468 break None ;
6569 }
6670
Original file line number Diff line number Diff line change @@ -161,4 +161,29 @@ fn q() {
161161 } ;
162162}
163163
164+ fn r ( ) {
165+ let ok = format_args ! ( "" ) else { return ; } ;
166+
167+ let bad = format_args ! { "" } else { return ; } ;
168+ //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed
169+ }
170+
171+ fn s ( ) {
172+ macro_rules! a {
173+ ( ) => { { } }
174+ }
175+
176+ macro_rules! b {
177+ ( 1 ) => {
178+ let x = a!( ) else { return ; } ;
179+ } ;
180+ ( 2 ) => {
181+ let x = a! { } else { return ; } ;
182+ //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed
183+ } ;
184+ }
185+
186+ b ! ( 1 ) ; b ! ( 2 ) ;
187+ }
188+
164189fn main ( ) { }
Original file line number Diff line number Diff line change @@ -228,5 +228,31 @@ LL | x
228228LL ~ }) else {
229229 |
230230
231- error: aborting due to 17 previous errors
231+ error: right curly brace `}` before `else` in a `let...else` statement not allowed
232+ --> $DIR/bad-let-else-statement.rs:167:31
233+ |
234+ LL | let bad = format_args! {""} else { return; };
235+ | ^
236+ |
237+ help: wrap the expression in parentheses
238+ |
239+ LL | let bad = (format_args! {""}) else { return; };
240+ | + +
241+
242+ error: right curly brace `}` before `else` in a `let...else` statement not allowed
243+ --> $DIR/bad-let-else-statement.rs:181:25
244+ |
245+ LL | let x = a! {} else { return; };
246+ | ^
247+ ...
248+ LL | b!(1); b!(2);
249+ | ----- in this macro invocation
250+ |
251+ = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)
252+ help: wrap the expression in parentheses
253+ |
254+ LL | let x = (a! {}) else { return; };
255+ | + +
256+
257+ error: aborting due to 19 previous errors
232258
You can’t perform that action at this time.
0 commit comments