]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #91575 - compiler-errors:issue-91556, r=cjgillot
authorMatthias Krรผger <matthias.krueger@famsik.de>
Fri, 10 Dec 2021 21:40:35 +0000 (22:40 +0100)
committerGitHub <noreply@github.com>
Fri, 10 Dec 2021 21:40:35 +0000 (22:40 +0100)
Fix ICE on format string of macro with secondary-label

This generalizes the fix #86104 to also correctly skip `Span::from_inner` for the `secondary_label` of a format macro parsing error as well.

We can alternatively skip the `span_label` diagnostic call for the secondary label as well, since that label probably only makes sense when the _proper_ span is computed.

Fixes #91556

compiler/rustc_builtin_macros/src/format.rs
src/test/ui/fmt/issue-91556.rs [new file with mode: 0644]
src/test/ui/fmt/issue-91556.stderr [new file with mode: 0644]

index 62a55c0e49ed08ed94a0263688f01e6ceedba9ba..8c343b6ea081733c08cbd8c1a62c9110b7936e1e 100644 (file)
@@ -995,8 +995,9 @@ pub fn expand_preparsed_format_args(
             e.note(&note);
         }
         if let Some((label, span)) = err.secondary_label {
-            let sp = fmt_span.from_inner(span);
-            e.span_label(sp, label);
+            if efmt_kind_is_lit {
+                e.span_label(fmt_span.from_inner(span), label);
+            }
         }
         e.emit();
         return DummyResult::raw_expr(sp, true);
diff --git a/src/test/ui/fmt/issue-91556.rs b/src/test/ui/fmt/issue-91556.rs
new file mode 100644 (file)
index 0000000..e782e6f
--- /dev/null
@@ -0,0 +1,8 @@
+fn main() {
+  let _ = format!(concat!("{0}๐–ณ๐–พ๐—Œ๐—{"), i);
+  //~^ ERROR: invalid format string: expected `'}'` but string was terminated
+  //~| NOTE: if you intended to print `{`, you can escape it using `{{`
+  //~| NOTE: in this expansion of concat!
+  //~| NOTE: in this expansion of concat!
+  //~| NOTE: expected `'}'` in format string
+}
diff --git a/src/test/ui/fmt/issue-91556.stderr b/src/test/ui/fmt/issue-91556.stderr
new file mode 100644 (file)
index 0000000..dbd5aef
--- /dev/null
@@ -0,0 +1,11 @@
+error: invalid format string: expected `'}'` but string was terminated
+  --> $DIR/issue-91556.rs:2:19
+   |
+LL |   let _ = format!(concat!("{0}๐–ณ๐–พ๐—Œ๐—{"), i);
+   |                   ^^^^^^^^^^^^^^^^^^^ expected `'}'` in format string
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+   = note: this error originates in the macro `concat` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+