File tree 3 files changed +59
-4
lines changed
compiler/rustc_ast/src/util
3 files changed +59
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
// Predicates on exprs and stmts that the pretty-printer and parser use
4
4
5
- use crate :: ast;
5
+ use crate :: { ast, token :: Delimiter } ;
6
6
7
7
/// Does this expression require a semicolon to be treated
8
8
/// 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> {
59
59
| While ( ..)
60
60
| ConstBlock ( _) => break Some ( expr) ,
61
61
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.
64
68
break None ;
65
69
}
66
70
Original file line number Diff line number Diff line change @@ -161,4 +161,29 @@ fn q() {
161
161
} ;
162
162
}
163
163
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
+
164
189
fn main ( ) { }
Original file line number Diff line number Diff line change @@ -228,5 +228,31 @@ LL | x
228
228
LL ~ }) else {
229
229
|
230
230
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
232
258
You can’t perform that action at this time.
0 commit comments