Skip to content

Commit af05d2a

Browse files
Suggest wrapping mac args in parens rather than the whole expression
1 parent 9e215d2 commit af05d2a

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

compiler/rustc_parse/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ parse_sugg_turbofish_syntax = use `::<...>` instead of `<...>` to specify lifeti
718718
719719
parse_sugg_wrap_expression_in_parentheses = wrap the expression in parentheses
720720
721+
parse_sugg_wrap_macro_in_parentheses = use parentheses instead of braces for this macro
722+
721723
parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses
722724
723725
parse_switch_mut_let_order =

compiler/rustc_parse/src/errors.rs

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

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

737750
#[derive(Diagnostic)]
@@ -933,7 +946,7 @@ pub(crate) struct InvalidExpressionInLetElse {
933946
pub span: Span,
934947
pub operator: &'static str,
935948
#[subdiagnostic]
936-
pub sugg: WrapExpressionInParentheses,
949+
pub sugg: WrapInParentheses,
937950
}
938951

939952
#[derive(Diagnostic)]
@@ -942,7 +955,7 @@ pub(crate) struct InvalidCurlyInLetElse {
942955
#[primary_span]
943956
pub span: Span,
944957
#[subdiagnostic]
945-
pub sugg: WrapExpressionInParentheses,
958+
pub sugg: WrapInParentheses,
946959
}
947960

948961
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ impl<'a> Parser<'a> {
17871787
let lexpr = self.parse_expr_labeled(label, true)?;
17881788
self.sess.emit_err(errors::LabeledLoopInBreak {
17891789
span: lexpr.span,
1790-
sub: errors::WrapExpressionInParentheses {
1790+
sub: errors::WrapInParentheses::Expression {
17911791
left: lexpr.span.shrink_to_lo(),
17921792
right: lexpr.span.shrink_to_hi(),
17931793
},

compiler/rustc_parse/src/parser/stmt.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a> Parser<'a> {
388388
self.sess.emit_err(errors::InvalidExpressionInLetElse {
389389
span: init.span,
390390
operator: op.node.as_str(),
391-
sugg: errors::WrapExpressionInParentheses {
391+
sugg: errors::WrapInParentheses::Expression {
392392
left: init.span.shrink_to_lo(),
393393
right: init.span.shrink_to_hi(),
394394
},
@@ -399,12 +399,19 @@ impl<'a> Parser<'a> {
399399

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

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ error: right curly brace `}` before `else` in a `let...else` statement not allow
234234
LL | let bad = asm! {" nop "} else { return; };
235235
| ^
236236
|
237-
help: wrap the expression in parentheses
237+
help: use parentheses instead of braces for this macro
238238
|
239-
LL | let bad = (asm! {" nop "}) else { return; };
240-
| + +
239+
LL | let bad = asm! (" nop ") else { return; };
240+
| ~ ~
241241

242242
error: right curly brace `}` before `else` in a `let...else` statement not allowed
243243
--> $DIR/bad-let-else-statement.rs:183:25
@@ -249,10 +249,10 @@ LL | b!(1); b!(2);
249249
| ----- in this macro invocation
250250
|
251251
= 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
252+
help: use parentheses instead of braces for this macro
253253
|
254-
LL | let x = (a! {}) else { return; };
255-
| + +
254+
LL | let x = a! () else { return; };
255+
| ~~
256256

257257
error: aborting due to 19 previous errors
258258

0 commit comments

Comments
 (0)