-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Validate source snippet when format input is raw string #152277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+147
−11
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b3d04e
Validate source snippet when format input is raw string
gurry 7497158
Added checks for the suffix
gurry 8e94039
Corrected a comment
gurry 3f56fa9
Added another test
gurry 43c78ad
Refactored test code to remove duplication
gurry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,32 @@ | ||
| extern crate proc_macro; | ||
|
|
||
| use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree}; | ||
| use std::iter::FromIterator; | ||
|
|
||
| /// Expands to a call to `println!` having a format string as its argument | ||
| /// but with the format string's span set to that of the proc macro's input token | ||
| #[proc_macro] | ||
| pub fn foo(input: TokenStream) -> TokenStream { | ||
| let token = input.into_iter().next().unwrap(); | ||
| let mut lit: Literal = r#"r"{}""#.parse().unwrap(); | ||
| lit.set_span(token.span()); | ||
| FromIterator::<TokenTree>::from_iter([ | ||
| Ident::new("println", Span::mixed_site()).into(), | ||
| Punct::new('!', Spacing::Alone).into(), | ||
| Group::new(Delimiter::Parenthesis, TokenTree::from(lit).into()).into(), | ||
| ]) | ||
| } | ||
|
|
||
|
|
||
| /// Same as `foo` but with the format string containing multiple `#`s | ||
| #[proc_macro] | ||
| pub fn foo2(input: TokenStream) -> TokenStream { | ||
| let token = input.into_iter().next().unwrap(); | ||
| let mut lit: Literal = r###"r##"{}"##"###.parse().unwrap(); | ||
| lit.set_span(token.span()); | ||
| FromIterator::<TokenTree>::from_iter([ | ||
| Ident::new("println", Span::mixed_site()).into(), | ||
| Punct::new('!', Spacing::Alone).into(), | ||
| Group::new(Delimiter::Parenthesis, TokenTree::from(lit).into()).into(), | ||
| ]) | ||
| } |
This file contains hidden or 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,21 @@ | ||
| // Regression test for ICE https://github.com/rust-lang/rust/issues/114865 | ||
|
|
||
| // Tests that we do not ICE when a proc macro expands | ||
| // to a string formatting macro (like println!) and respans | ||
| // this formatting macro's arg to that of its own input which | ||
| // happens to be a multi-byte string (see the auxiliary file | ||
| // ice-wrong-span-114865.rs). | ||
|
|
||
| //@ proc-macro: ice-wrong-span-114865.rs | ||
|
|
||
| extern crate ice_wrong_span_114865; | ||
|
|
||
| use ice_wrong_span_114865::{foo, foo2}; | ||
|
|
||
| fn main() { | ||
| foo!("字"); //~ ERROR 1 positional argument in format string, but no arguments were given | ||
| foo!("r字字"); //~ ERROR 1 positional argument in format string, but no arguments were given | ||
|
|
||
| foo2!("字"); //~ ERROR 1 positional argument in format string, but no arguments were given | ||
| foo2!("r字字"); //~ ERROR 1 positional argument in format string, but no arguments were given | ||
| } |
This file contains hidden or 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,26 @@ | ||
| error: 1 positional argument in format string, but no arguments were given | ||
| --> $DIR/ice-wrong-span-114865.rs:16:10 | ||
| | | ||
| LL | foo!("字"); | ||
| | ^^^^ | ||
|
|
||
| error: 1 positional argument in format string, but no arguments were given | ||
| --> $DIR/ice-wrong-span-114865.rs:17:10 | ||
| | | ||
| LL | foo!("r字字"); | ||
| | ^^^^^^^ | ||
|
|
||
| error: 1 positional argument in format string, but no arguments were given | ||
| --> $DIR/ice-wrong-span-114865.rs:19:11 | ||
| | | ||
| LL | foo2!("字"); | ||
| | ^^^^ | ||
|
|
||
| error: 1 positional argument in format string, but no arguments were given | ||
| --> $DIR/ice-wrong-span-114865.rs:20:11 | ||
| | | ||
| LL | foo2!("r字字"); | ||
| | ^^^^^^^ | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.