Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion for bad block fragment error #112978

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ parse_int_literal_too_large = integer literal is too large

parse_invalid_block_macro_segment = cannot use a `block` macro fragment here
.label = the `block` fragment is within this context
.suggestion = wrap this in another block

parse_invalid_char_in_escape = {parse_invalid_char_in_escape_msg}: `{$ch}`
.label = {parse_invalid_char_in_escape_msg}
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ pub(crate) struct InvalidBlockMacroSegment {
pub span: Span,
#[label]
pub context: Span,
#[subdiagnostic]
pub wrap: WrapInExplicitBlock,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
pub(crate) struct WrapInExplicitBlock {
#[suggestion_part(code = "{{ ")]
pub lo: Span,
#[suggestion_part(code = " }}")]
pub hi: Span,
}

#[derive(Diagnostic)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,10 @@ impl<'a> Parser<'a> {
self.sess.emit_err(errors::InvalidBlockMacroSegment {
span: self.token.span,
context: lo.to(self.token.span),
wrap: errors::WrapInExplicitBlock {
lo: self.token.span.shrink_to_lo(),
hi: self.token.span.shrink_to_hi(),
},
});
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/parser/bad-interpolated-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ LL | m!({});
| ------ in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap this in another block
|
LL | 'lab: { $b };
| + +

error: cannot use a `block` macro fragment here
--> $DIR/bad-interpolated-block.rs:6:16
Expand All @@ -23,6 +27,10 @@ LL | m!({});
| ------ in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap this in another block
|
LL | unsafe { $b };
| + +

error: cannot use a `block` macro fragment here
--> $DIR/bad-interpolated-block.rs:7:23
Expand All @@ -34,6 +42,10 @@ LL | m!({});
| ------ in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap this in another block
|
LL | |x: u8| -> () { $b };
| + +

error: aborting due to 3 previous errors

4 changes: 4 additions & 0 deletions tests/ui/parser/labeled-no-colon-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ LL | m!({});
| ------ in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap this in another block
|
LL | 'l5 { $b };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this suggestion is incomplete, but would be applied with the suggestion above ("help: add : after the label").

| + +

error: labeled expression must be followed by `:`
--> $DIR/labeled-no-colon-expr.rs:14:8
Expand Down