From e6c02aad9345925cfed74f86b414c4d0715d381b Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 27 Dec 2022 22:15:25 +0100 Subject: 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. --- src/test/ui/fmt/auxiliary/format-string-proc-macro.rs | 14 +++++++++++++- src/test/ui/fmt/respanned-literal-issue-106191.rs | 10 ++++++++++ src/test/ui/fmt/respanned-literal-issue-106191.stderr | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/fmt/respanned-literal-issue-106191.rs create mode 100644 src/test/ui/fmt/respanned-literal-issue-106191.stderr (limited to 'src/test') 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 + -- cgit 1.4.1-3-g733a5