forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#102519 - Alexendoo:format-args-macro-str, r=m…
…-ou-se Fix `format_args` capture for macro expanded format strings Since rust-lang#100996 `format_args` capture for macro expanded strings aren't prevented when the span of the expansion points to a string literal, e.g. ```rust // not a terribly realistic example, but also happens for proc_macros that set // the span of the output to an input str literal, such as indoc macro_rules! x { ($e:expr) => { $e } } fn main() { let a = 1; println!(x!("{a}")); } ``` The tests didn't catch it as the span of `concat!()` points to the macro invocation r? `@m-ou-se`
- Loading branch information
Showing
7 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{Literal, Span, TokenStream, TokenTree}; | ||
|
||
#[proc_macro] | ||
pub fn foo_with_input_span(input: TokenStream) -> TokenStream { | ||
let span = input.into_iter().next().unwrap().span(); | ||
|
||
let mut lit = Literal::string("{foo}"); | ||
lit.set_span(span); | ||
|
||
TokenStream::from(TokenTree::Literal(lit)) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn err_with_input_span(input: TokenStream) -> TokenStream { | ||
let span = input.into_iter().next().unwrap().span(); | ||
|
||
let mut lit = Literal::string(" }"); | ||
lit.set_span(span); | ||
|
||
TokenStream::from(TokenTree::Literal(lit)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
// aux-build:format-string-proc-macro.rs | ||
|
||
#[macro_use] | ||
extern crate format_string_proc_macro; | ||
|
||
macro_rules! def_site { | ||
() => { "{foo}" } //~ ERROR: there is no argument named `foo` | ||
} | ||
|
||
macro_rules! call_site { | ||
($fmt:literal) => { $fmt } | ||
} | ||
|
||
fn main() { | ||
format!(concat!("{foo}")); //~ ERROR: there is no argument named `foo` | ||
format!(concat!("{ba", "r} {}"), 1); //~ ERROR: there is no argument named `bar` | ||
|
||
format!(def_site!()); | ||
format!(call_site!("{foo}")); //~ ERROR: there is no argument named `foo` | ||
|
||
format!(foo_with_input_span!("")); //~ ERROR: there is no argument named `foo` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: invalid format string: unmatched `}` found | ||
--> $DIR/format-expanded-string.rs:19:13 | ||
| | ||
LL | format!(concat!("abc}")); | ||
| ^^^^^^^^^^^^^^^ unmatched `}` 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: invalid format string: unmatched `}` found | ||
--> $DIR/format-expanded-string.rs:22:34 | ||
| | ||
LL | format!(err_with_input_span!("")); | ||
| ^^ unmatched `}` in format string | ||
| | ||
= note: if you intended to print `}`, you can escape it using `}}` | ||
|
||
error: aborting due to 2 previous errors | ||
|