Auto merge of #107041 - Nilstrieb:back-to-being-clueless-whether-it-really-is-a-literal, r=compiler-errors

Revert "Improve heuristics whether `format_args` string is a source literal"

This reverts commit e6c02aad9345925cfed74f86b414c4d0715d381b (from #106195).

Keeps the code improvements from the PR and the test (as a known-bug).

Works around #106408 while a proper fix is discussed more thoroughly in #106505, as proposed by `@tmandry.`

Reopens #106191

r? compiler-errors
This commit is contained in:
bors 2023-01-18 22:58:30 +00:00
commit 8b11574ca0
5 changed files with 32 additions and 56 deletions

View File

@ -20,7 +20,6 @@ pub use Flag::*;
pub use Piece::*; pub use Piece::*;
pub use Position::*; pub use Position::*;
use rustc_lexer::unescape;
use std::iter; use std::iter;
use std::str; use std::str;
use std::string; use std::string;
@ -314,11 +313,12 @@ impl<'a> Parser<'a> {
append_newline: bool, append_newline: bool,
mode: ParseMode, mode: ParseMode,
) -> Parser<'a> { ) -> Parser<'a> {
let input_string_kind = find_width_map_from_snippet(s, snippet, style); let input_string_kind = find_width_map_from_snippet(snippet, style);
let (width_map, is_literal) = match input_string_kind { let (width_map, is_literal) = match input_string_kind {
InputStringKind::Literal { width_mappings } => (width_mappings, true), InputStringKind::Literal { width_mappings } => (width_mappings, true),
InputStringKind::NotALiteral => (Vec::new(), false), InputStringKind::NotALiteral => (Vec::new(), false),
}; };
Parser { Parser {
mode, mode,
input: s, input: s,
@ -856,7 +856,6 @@ impl<'a> Parser<'a> {
/// written code (code snippet) and the `InternedString` that gets processed in the `Parser` /// written code (code snippet) and the `InternedString` that gets processed in the `Parser`
/// in order to properly synthesise the intra-string `Span`s for error diagnostics. /// in order to properly synthesise the intra-string `Span`s for error diagnostics.
fn find_width_map_from_snippet( fn find_width_map_from_snippet(
input: &str,
snippet: Option<string::String>, snippet: Option<string::String>,
str_style: Option<usize>, str_style: Option<usize>,
) -> InputStringKind { ) -> InputStringKind {
@ -869,27 +868,8 @@ fn find_width_map_from_snippet(
return InputStringKind::Literal { width_mappings: Vec::new() }; return InputStringKind::Literal { width_mappings: Vec::new() };
} }
// Strip quotes.
let snippet = &snippet[1..snippet.len() - 1]; let snippet = &snippet[1..snippet.len() - 1];
// Macros like `println` add a newline at the end. That technically doens't make them "literals" anymore, but it's fine
// since we will never need to point our spans there, so we lie about it here by ignoring it.
// Since there might actually be newlines in the source code, we need to normalize away all trailing newlines.
// If we only trimmed it off the input, `format!("\n")` would cause a mismatch as here we they actually match up.
// Alternatively, we could just count the trailing newlines and only trim one from the input if they don't match up.
let input_no_nl = input.trim_end_matches('\n');
let Ok(unescaped) = unescape_string(snippet) else {
return InputStringKind::NotALiteral;
};
let unescaped_no_nl = unescaped.trim_end_matches('\n');
if unescaped_no_nl != input_no_nl {
// The source string that we're pointing at isn't our input, so spans pointing at it will be incorrect.
// This can for example happen with proc macros that respan generated literals.
return InputStringKind::NotALiteral;
}
let mut s = snippet.char_indices(); let mut s = snippet.char_indices();
let mut width_mappings = vec![]; let mut width_mappings = vec![];
while let Some((pos, c)) = s.next() { while let Some((pos, c)) = s.next() {
@ -972,19 +952,6 @@ fn find_width_map_from_snippet(
InputStringKind::Literal { width_mappings } InputStringKind::Literal { width_mappings }
} }
fn unescape_string(string: &str) -> Result<string::String, unescape::EscapeError> {
let mut buf = string::String::new();
let mut error = Ok(());
unescape::unescape_literal(string, unescape::Mode::Str, &mut |_, unescaped_char| {
match unescaped_char {
Ok(c) => buf.push(c),
Err(err) => error = Err(err),
}
});
error.map(|_| buf)
}
// Assert a reasonable size for `Piece` // Assert a reasonable size for `Piece`
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Piece<'_>, 16); rustc_data_structures::static_assert_size!(Piece<'_>, 16);

View File

@ -28,6 +28,7 @@ pub fn err_with_input_span(input: TokenStream) -> TokenStream {
TokenStream::from(TokenTree::Literal(lit)) TokenStream::from(TokenTree::Literal(lit))
} }
#[proc_macro] #[proc_macro]
pub fn respan_to_invalid_format_literal(input: TokenStream) -> TokenStream { pub fn respan_to_invalid_format_literal(input: TokenStream) -> TokenStream {
let mut s = Literal::string("{"); let mut s = Literal::string("{");
@ -38,3 +39,14 @@ pub fn respan_to_invalid_format_literal(input: TokenStream) -> TokenStream {
TokenTree::from(Group::new(Delimiter::Parenthesis, TokenTree::from(s).into())), TokenTree::from(Group::new(Delimiter::Parenthesis, TokenTree::from(s).into())),
]) ])
} }
#[proc_macro]
pub fn capture_a_with_prepended_space_preserve_span(input: TokenStream) -> TokenStream {
let mut s = Literal::string(" {a}");
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())),
])
}

View File

@ -0,0 +1,9 @@
// aux-build:format-string-proc-macro.rs
// check-pass
extern crate format_string_proc_macro;
fn main() {
let a = 0;
format_string_proc_macro::capture_a_with_prepended_space_preserve_span!("{a}");
}

View File

@ -1,10 +1,15 @@
// aux-build:format-string-proc-macro.rs // aux-build:format-string-proc-macro.rs
// check-fail
// known-bug: #106191
// unset-rustc-env:RUST_BACKTRACE
// had to be reverted
// error-pattern:internal compiler error
// failure-status:101
// dont-check-compiler-stderr
extern crate format_string_proc_macro; extern crate format_string_proc_macro;
fn main() { fn main() {
format_string_proc_macro::respan_to_invalid_format_literal!("¡"); format_string_proc_macro::respan_to_invalid_format_literal!("¡");
//~^ ERROR invalid format string: expected `'}'` but string was terminated
format_args!(r#concat!("¡ {")); format_args!(r#concat!("¡ {"));
//~^ ERROR invalid format string: expected `'}'` but string was terminated
} }

View File

@ -1,19 +1,2 @@
error: invalid format string: expected `'}'` but string was terminated query stack during panic:
--> $DIR/respanned-literal-issue-106191.rs:6:65 end of query stack
|
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