about summary refs log tree commit diff
diff options
context:
space:
mode:
authorclubby789 <jamie@hill-daniel.co.uk>2024-08-01 13:11:24 +0000
committerclubby789 <jamie@hill-daniel.co.uk>2024-08-01 13:11:24 +0000
commite157954cce4497738a728f8c295c71b229d35a66 (patch)
tree70929ac99ff57cedb499cc46a087f4bec17e09a3
parent97ac52f579fe1003a162324d448dad43a942b5f5 (diff)
downloadrust-e157954cce4497738a728f8c295c71b229d35a66.tar.gz
rust-e157954cce4497738a728f8c295c71b229d35a66.zip
Fix removed `box_syntax` diagnostic if source isn't available
-rw-r--r--compiler/rustc_parse/messages.ftl2
-rw-r--r--compiler/rustc_parse/src/errors.rs23
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs10
-rw-r--r--tests/ui/parser/removed-syntax/removed-syntax-box.stderr10
4 files changed, 28 insertions, 17 deletions
diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl
index 391a5791776..9d594120ec4 100644
--- a/compiler/rustc_parse/messages.ftl
+++ b/compiler/rustc_parse/messages.ftl
@@ -66,7 +66,7 @@ parse_box_not_pat = expected pattern, found {$descr}
     .suggestion = escape `box` to use it as an identifier
 
 parse_box_syntax_removed = `box_syntax` has been removed
-    .suggestion = use `Box::new()` instead
+parse_box_syntax_removed_suggestion = use `Box::new()` instead
 
 parse_cannot_be_raw_ident = `{$ident}` cannot be a raw identifier
 
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs
index c5e7e0df631..dfe47dd0beb 100644
--- a/compiler/rustc_parse/src/errors.rs
+++ b/compiler/rustc_parse/src/errors.rs
@@ -2725,15 +2725,24 @@ impl HelpUseLatestEdition {
 
 #[derive(Diagnostic)]
 #[diag(parse_box_syntax_removed)]
-pub struct BoxSyntaxRemoved<'a> {
+pub struct BoxSyntaxRemoved {
     #[primary_span]
-    #[suggestion(
-        code = "Box::new({code})",
-        applicability = "machine-applicable",
-        style = "verbose"
-    )]
     pub span: Span,
-    pub code: &'a str,
+    #[subdiagnostic]
+    pub sugg: AddBoxNew,
+}
+
+#[derive(Subdiagnostic)]
+#[multipart_suggestion(
+    parse_box_syntax_removed_suggestion,
+    applicability = "machine-applicable",
+    style = "verbose"
+)]
+pub struct AddBoxNew {
+    #[suggestion_part(code = "Box::new(")]
+    pub box_kw_and_lo: Span,
+    #[suggestion_part(code = ")")]
+    pub hi: Span,
 }
 
 #[derive(Diagnostic)]
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index a4d9d97045d..1b053c39e64 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -618,10 +618,12 @@ impl<'a> Parser<'a> {
     /// Parse `box expr` - this syntax has been removed, but we still parse this
     /// for now to provide a more useful error
     fn parse_expr_box(&mut self, box_kw: Span) -> PResult<'a, (Span, ExprKind)> {
-        let (span, _) = self.parse_expr_prefix_common(box_kw)?;
-        let inner_span = span.with_lo(box_kw.hi());
-        let code = self.psess.source_map().span_to_snippet(inner_span).unwrap();
-        let guar = self.dcx().emit_err(errors::BoxSyntaxRemoved { span: span, code: code.trim() });
+        let (span, expr) = self.parse_expr_prefix_common(box_kw)?;
+        // Make a multipart suggestion instead of `span_to_snippet` in case source isn't available
+        let box_kw_and_lo = box_kw.until(self.interpolated_or_expr_span(&expr));
+        let hi = span.shrink_to_hi();
+        let sugg = errors::AddBoxNew { box_kw_and_lo, hi };
+        let guar = self.dcx().emit_err(errors::BoxSyntaxRemoved { span, sugg });
         Ok((span, ExprKind::Err(guar)))
     }
 
diff --git a/tests/ui/parser/removed-syntax/removed-syntax-box.stderr b/tests/ui/parser/removed-syntax/removed-syntax-box.stderr
index 46b891587d5..60c39fd37c4 100644
--- a/tests/ui/parser/removed-syntax/removed-syntax-box.stderr
+++ b/tests/ui/parser/removed-syntax/removed-syntax-box.stderr
@@ -7,7 +7,7 @@ LL |     let _ = box ();
 help: use `Box::new()` instead
    |
 LL |     let _ = Box::new(());
-   |             ~~~~~~~~~~~~
+   |             ~~~~~~~~~  +
 
 error: `box_syntax` has been removed
   --> $DIR/removed-syntax-box.rs:10:13
@@ -18,7 +18,7 @@ LL |     let _ = box 1;
 help: use `Box::new()` instead
    |
 LL |     let _ = Box::new(1);
-   |             ~~~~~~~~~~~
+   |             ~~~~~~~~~ +
 
 error: `box_syntax` has been removed
   --> $DIR/removed-syntax-box.rs:11:13
@@ -29,7 +29,7 @@ LL |     let _ = box T { a: 12, b: 18 };
 help: use `Box::new()` instead
    |
 LL |     let _ = Box::new(T { a: 12, b: 18 });
-   |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+   |             ~~~~~~~~~                  +
 
 error: `box_syntax` has been removed
   --> $DIR/removed-syntax-box.rs:12:13
@@ -40,7 +40,7 @@ LL |     let _ = box [5; 30];
 help: use `Box::new()` instead
    |
 LL |     let _ = Box::new([5; 30]);
-   |             ~~~~~~~~~~~~~~~~~
+   |             ~~~~~~~~~       +
 
 error: `box_syntax` has been removed
   --> $DIR/removed-syntax-box.rs:13:22
@@ -51,7 +51,7 @@ LL |     let _: Box<()> = box ();
 help: use `Box::new()` instead
    |
 LL |     let _: Box<()> = Box::new(());
-   |                      ~~~~~~~~~~~~
+   |                      ~~~~~~~~~  +
 
 error: aborting due to 5 previous errors