Skip to content

Commit 2cc7782

Browse files
Add suggestion for bad block fragment error
1 parent 04075b3 commit 2cc7782

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

Diff for: compiler/rustc_parse/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ parse_int_literal_too_large = integer literal is too large
353353
354354
parse_invalid_block_macro_segment = cannot use a `block` macro fragment here
355355
.label = the `block` fragment is within this context
356+
.suggestion = wrap this in another block
356357
357358
parse_invalid_char_in_escape = {parse_invalid_char_in_escape_msg}: `{$ch}`
358359
.label = {parse_invalid_char_in_escape_msg}

Diff for: compiler/rustc_parse/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ pub(crate) struct InvalidBlockMacroSegment {
333333
pub span: Span,
334334
#[label]
335335
pub context: Span,
336+
#[subdiagnostic]
337+
pub wrap: WrapInExplicitBlock,
338+
}
339+
340+
#[derive(Subdiagnostic)]
341+
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
342+
pub(crate) struct WrapInExplicitBlock {
343+
#[suggestion_part(code = "{{ ")]
344+
pub lo: Span,
345+
#[suggestion_part(code = " }}")]
346+
pub hi: Span,
336347
}
337348

338349
#[derive(Diagnostic)]

Diff for: compiler/rustc_parse/src/parser/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,10 @@ impl<'a> Parser<'a> {
21862186
self.sess.emit_err(errors::InvalidBlockMacroSegment {
21872187
span: self.token.span,
21882188
context: lo.to(self.token.span),
2189+
wrap: errors::WrapInExplicitBlock {
2190+
lo: self.token.span.shrink_to_lo(),
2191+
hi: self.token.span.shrink_to_hi(),
2192+
},
21892193
});
21902194
}
21912195

Diff for: tests/ui/parser/bad-interpolated-block.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | m!({});
1010
| ------ in this macro invocation
1111
|
1212
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: wrap this in another block
14+
|
15+
LL | 'lab: { $b };
16+
| + +
1317

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

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

3850
error: aborting due to 3 previous errors
3951

Diff for: tests/ui/parser/labeled-no-colon-expr.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ LL | m!({});
7777
| ------ in this macro invocation
7878
|
7979
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
80+
help: wrap this in another block
81+
|
82+
LL | 'l5 { $b };
83+
| + +
8084

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

0 commit comments

Comments
 (0)