Skip to content

Commit e157954

Browse files
committed
Fix removed box_syntax diagnostic if source isn't available
1 parent 97ac52f commit e157954

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ parse_box_not_pat = expected pattern, found {$descr}
6666
.suggestion = escape `box` to use it as an identifier
6767
6868
parse_box_syntax_removed = `box_syntax` has been removed
69-
.suggestion = use `Box::new()` instead
69+
parse_box_syntax_removed_suggestion = use `Box::new()` instead
7070
7171
parse_cannot_be_raw_ident = `{$ident}` cannot be a raw identifier
7272

compiler/rustc_parse/src/errors.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -2725,15 +2725,24 @@ impl HelpUseLatestEdition {
27252725

27262726
#[derive(Diagnostic)]
27272727
#[diag(parse_box_syntax_removed)]
2728-
pub struct BoxSyntaxRemoved<'a> {
2728+
pub struct BoxSyntaxRemoved {
27292729
#[primary_span]
2730-
#[suggestion(
2731-
code = "Box::new({code})",
2732-
applicability = "machine-applicable",
2733-
style = "verbose"
2734-
)]
27352730
pub span: Span,
2736-
pub code: &'a str,
2731+
#[subdiagnostic]
2732+
pub sugg: AddBoxNew,
2733+
}
2734+
2735+
#[derive(Subdiagnostic)]
2736+
#[multipart_suggestion(
2737+
parse_box_syntax_removed_suggestion,
2738+
applicability = "machine-applicable",
2739+
style = "verbose"
2740+
)]
2741+
pub struct AddBoxNew {
2742+
#[suggestion_part(code = "Box::new(")]
2743+
pub box_kw_and_lo: Span,
2744+
#[suggestion_part(code = ")")]
2745+
pub hi: Span,
27372746
}
27382747

27392748
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,12 @@ impl<'a> Parser<'a> {
618618
/// Parse `box expr` - this syntax has been removed, but we still parse this
619619
/// for now to provide a more useful error
620620
fn parse_expr_box(&mut self, box_kw: Span) -> PResult<'a, (Span, ExprKind)> {
621-
let (span, _) = self.parse_expr_prefix_common(box_kw)?;
622-
let inner_span = span.with_lo(box_kw.hi());
623-
let code = self.psess.source_map().span_to_snippet(inner_span).unwrap();
624-
let guar = self.dcx().emit_err(errors::BoxSyntaxRemoved { span: span, code: code.trim() });
621+
let (span, expr) = self.parse_expr_prefix_common(box_kw)?;
622+
// Make a multipart suggestion instead of `span_to_snippet` in case source isn't available
623+
let box_kw_and_lo = box_kw.until(self.interpolated_or_expr_span(&expr));
624+
let hi = span.shrink_to_hi();
625+
let sugg = errors::AddBoxNew { box_kw_and_lo, hi };
626+
let guar = self.dcx().emit_err(errors::BoxSyntaxRemoved { span, sugg });
625627
Ok((span, ExprKind::Err(guar)))
626628
}
627629

tests/ui/parser/removed-syntax/removed-syntax-box.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _ = box ();
77
help: use `Box::new()` instead
88
|
99
LL | let _ = Box::new(());
10-
| ~~~~~~~~~~~~
10+
| ~~~~~~~~~ +
1111

1212
error: `box_syntax` has been removed
1313
--> $DIR/removed-syntax-box.rs:10:13
@@ -18,7 +18,7 @@ LL | let _ = box 1;
1818
help: use `Box::new()` instead
1919
|
2020
LL | let _ = Box::new(1);
21-
| ~~~~~~~~~~~
21+
| ~~~~~~~~~ +
2222

2323
error: `box_syntax` has been removed
2424
--> $DIR/removed-syntax-box.rs:11:13
@@ -29,7 +29,7 @@ LL | let _ = box T { a: 12, b: 18 };
2929
help: use `Box::new()` instead
3030
|
3131
LL | let _ = Box::new(T { a: 12, b: 18 });
32-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
| ~~~~~~~~~ +
3333

3434
error: `box_syntax` has been removed
3535
--> $DIR/removed-syntax-box.rs:12:13
@@ -40,7 +40,7 @@ LL | let _ = box [5; 30];
4040
help: use `Box::new()` instead
4141
|
4242
LL | let _ = Box::new([5; 30]);
43-
| ~~~~~~~~~~~~~~~~~
43+
| ~~~~~~~~~ +
4444

4545
error: `box_syntax` has been removed
4646
--> $DIR/removed-syntax-box.rs:13:22
@@ -51,7 +51,7 @@ LL | let _: Box<()> = box ();
5151
help: use `Box::new()` instead
5252
|
5353
LL | let _: Box<()> = Box::new(());
54-
| ~~~~~~~~~~~~
54+
| ~~~~~~~~~ +
5555

5656
error: aborting due to 5 previous errors
5757

0 commit comments

Comments
 (0)