about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNilstrieb <48135649+Nilstrieb@users.noreply.github.com>2022-12-27 22:15:25 +0100
committerNilstrieb <48135649+Nilstrieb@users.noreply.github.com>2022-12-28 17:43:18 +0100
commite6c02aad9345925cfed74f86b414c4d0715d381b (patch)
tree98fd6b6b4199045d3c413f5cec5318dbb9b8ce22 /src
parent1322e476bf5eecfad98f0b200f15c1b46a0d46d2 (diff)
downloadrust-e6c02aad9345925cfed74f86b414c4d0715d381b.tar.gz
rust-e6c02aad9345925cfed74f86b414c4d0715d381b.zip
Improve heuristics whether `format_args` string is a source literal
Previously, it only checked whether there was _a_ literal at the span of
the first argument, not whether the literal actually matched up. This
caused issues when a proc macro was generating a different literal with
the same span.

This requires an annoying special case for literals ending in `\n`
because otherwise `println` wouldn't give detailed diagnostics anymore
which would be bad.
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/fmt/auxiliary/format-string-proc-macro.rs14
-rw-r--r--src/test/ui/fmt/respanned-literal-issue-106191.rs10
-rw-r--r--src/test/ui/fmt/respanned-literal-issue-106191.stderr19
3 files changed, 42 insertions, 1 deletions
diff --git a/src/test/ui/fmt/auxiliary/format-string-proc-macro.rs b/src/test/ui/fmt/auxiliary/format-string-proc-macro.rs
index e44a84776bc..539c8fb27b3 100644
--- a/src/test/ui/fmt/auxiliary/format-string-proc-macro.rs
+++ b/src/test/ui/fmt/auxiliary/format-string-proc-macro.rs
@@ -5,7 +5,8 @@
 
 extern crate proc_macro;
 
-use proc_macro::{Literal, Span, TokenStream, TokenTree};
+use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
+use std::iter::FromIterator;
 
 #[proc_macro]
 pub fn foo_with_input_span(input: TokenStream) -> TokenStream {
@@ -26,3 +27,14 @@ pub fn err_with_input_span(input: TokenStream) -> TokenStream {
 
     TokenStream::from(TokenTree::Literal(lit))
 }
+
+#[proc_macro]
+pub fn respan_to_invalid_format_literal(input: TokenStream) -> TokenStream {
+    let mut s = Literal::string("{");
+    s.set_span(input.into_iter().next().unwrap().span());
+    TokenStream::from_iter([
+        TokenTree::from(Ident::new("format", Span::call_site())),
+        TokenTree::from(Punct::new('!', Spacing::Alone)),
+        TokenTree::from(Group::new(Delimiter::Parenthesis, TokenTree::from(s).into())),
+    ])
+}
diff --git a/src/test/ui/fmt/respanned-literal-issue-106191.rs b/src/test/ui/fmt/respanned-literal-issue-106191.rs
new file mode 100644
index 00000000000..44642a10fc0
--- /dev/null
+++ b/src/test/ui/fmt/respanned-literal-issue-106191.rs
@@ -0,0 +1,10 @@
+// aux-build:format-string-proc-macro.rs
+
+extern crate format_string_proc_macro;
+
+fn main() {
+    format_string_proc_macro::respan_to_invalid_format_literal!("¡");
+    //~^ ERROR invalid format string: expected `'}'` but string was terminated
+    format_args!(r#concat!("¡        {"));
+    //~^ ERROR invalid format string: expected `'}'` but string was terminated
+}
diff --git a/src/test/ui/fmt/respanned-literal-issue-106191.stderr b/src/test/ui/fmt/respanned-literal-issue-106191.stderr
new file mode 100644
index 00000000000..73a3af65a38
--- /dev/null
+++ b/src/test/ui/fmt/respanned-literal-issue-106191.stderr
@@ -0,0 +1,19 @@
+error: invalid format string: expected `'}'` but string was terminated
+  --> $DIR/respanned-literal-issue-106191.rs:6:65
+   |
+LL |     format_string_proc_macro::respan_to_invalid_format_literal!("¡");
+   |                                                                 ^^^ expected `'}'` in format string
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+
+error: invalid format string: expected `'}'` but string was terminated
+  --> $DIR/respanned-literal-issue-106191.rs:8:18
+   |
+LL |     format_args!(r#concat!("¡        {"));
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^ 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 2 previous errors
+