Skip to content

Commit 8175606

Browse files
authored
Unrolled build for rust-lang#119062
Rollup merge of rust-lang#119062 - compiler-errors:asm-in-let-else, r=davidtwco,est31 Deny braced macro invocations in let-else Fixes rust-lang#119057 Pending T-lang decision cc `@dtolnay`
2 parents 92d7277 + ec263df commit 8175606

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

compiler/rustc_ast/src/util/classify.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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

compiler/rustc_parse/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ parse_sugg_turbofish_syntax = use `::<...>` instead of `<...>` to specify lifeti
724724
725725
parse_sugg_wrap_expression_in_parentheses = wrap the expression in parentheses
726726
727+
parse_sugg_wrap_macro_in_parentheses = use parentheses instead of braces for this macro
728+
727729
parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses
728730
729731
parse_switch_mut_let_order =

compiler/rustc_parse/src/errors.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -722,19 +722,32 @@ pub(crate) struct LabeledLoopInBreak {
722722
#[primary_span]
723723
pub span: Span,
724724
#[subdiagnostic]
725-
pub sub: WrapExpressionInParentheses,
725+
pub sub: WrapInParentheses,
726726
}
727727

728728
#[derive(Subdiagnostic)]
729-
#[multipart_suggestion(
730-
parse_sugg_wrap_expression_in_parentheses,
731-
applicability = "machine-applicable"
732-
)]
733-
pub(crate) struct WrapExpressionInParentheses {
734-
#[suggestion_part(code = "(")]
735-
pub left: Span,
736-
#[suggestion_part(code = ")")]
737-
pub right: Span,
729+
730+
pub(crate) enum WrapInParentheses {
731+
#[multipart_suggestion(
732+
parse_sugg_wrap_expression_in_parentheses,
733+
applicability = "machine-applicable"
734+
)]
735+
Expression {
736+
#[suggestion_part(code = "(")]
737+
left: Span,
738+
#[suggestion_part(code = ")")]
739+
right: Span,
740+
},
741+
#[multipart_suggestion(
742+
parse_sugg_wrap_macro_in_parentheses,
743+
applicability = "machine-applicable"
744+
)]
745+
MacroArgs {
746+
#[suggestion_part(code = "(")]
747+
left: Span,
748+
#[suggestion_part(code = ")")]
749+
right: Span,
750+
},
738751
}
739752

740753
#[derive(Diagnostic)]
@@ -936,7 +949,7 @@ pub(crate) struct InvalidExpressionInLetElse {
936949
pub span: Span,
937950
pub operator: &'static str,
938951
#[subdiagnostic]
939-
pub sugg: WrapExpressionInParentheses,
952+
pub sugg: WrapInParentheses,
940953
}
941954

942955
#[derive(Diagnostic)]
@@ -945,7 +958,7 @@ pub(crate) struct InvalidCurlyInLetElse {
945958
#[primary_span]
946959
pub span: Span,
947960
#[subdiagnostic]
948-
pub sugg: WrapExpressionInParentheses,
961+
pub sugg: WrapInParentheses,
949962
}
950963

951964
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ impl<'a> Parser<'a> {
18441844
let lexpr = self.parse_expr_labeled(label, true)?;
18451845
self.dcx().emit_err(errors::LabeledLoopInBreak {
18461846
span: lexpr.span,
1847-
sub: errors::WrapExpressionInParentheses {
1847+
sub: errors::WrapInParentheses::Expression {
18481848
left: lexpr.span.shrink_to_lo(),
18491849
right: lexpr.span.shrink_to_hi(),
18501850
},

compiler/rustc_parse/src/parser/stmt.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'a> Parser<'a> {
389389
self.dcx().emit_err(errors::InvalidExpressionInLetElse {
390390
span: init.span,
391391
operator: op.node.as_str(),
392-
sugg: errors::WrapExpressionInParentheses {
392+
sugg: errors::WrapInParentheses::Expression {
393393
left: init.span.shrink_to_lo(),
394394
right: init.span.shrink_to_hi(),
395395
},
@@ -400,12 +400,19 @@ impl<'a> Parser<'a> {
400400

401401
fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) {
402402
if let Some(trailing) = classify::expr_trailing_brace(init) {
403-
self.dcx().emit_err(errors::InvalidCurlyInLetElse {
404-
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)),
405-
sugg: errors::WrapExpressionInParentheses {
403+
let sugg = match &trailing.kind {
404+
ExprKind::MacCall(mac) => errors::WrapInParentheses::MacroArgs {
405+
left: mac.args.dspan.open,
406+
right: mac.args.dspan.close,
407+
},
408+
_ => errors::WrapInParentheses::Expression {
406409
left: trailing.span.shrink_to_lo(),
407410
right: trailing.span.shrink_to_hi(),
408411
},
412+
};
413+
self.dcx().emit_err(errors::InvalidCurlyInLetElse {
414+
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)),
415+
sugg,
409416
});
410417
}
411418
}

tests/ui/parser/bad-let-else-statement.rs

+25
Original file line numberDiff line numberDiff 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+
164189
fn main() {}

tests/ui/parser/bad-let-else-statement.stderr

+27-1
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,31 @@ LL | x
228228
LL ~ }) 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: use parentheses instead of braces for this macro
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: use parentheses instead of braces for this macro
253+
|
254+
LL | let x = a! () else { return; };
255+
| ~~
256+
257+
error: aborting due to 19 previous errors
232258

0 commit comments

Comments
 (0)