-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #84717 - dtolnay:literalfromstr, r=petrochenkov
impl FromStr for proc_macro::Literal Note that unlike `impl FromStr for proc_macro::TokenStream`, this impl does not permit whitespace or comments. The input string must consist of nothing but your literal. - `"1".parse::<Literal>()` ⟶ ok - `"1.0".parse::<Literal>()` ⟶ ok - `"'a'".parse::<Literal>()` ⟶ ok - `"\"\n\"".parse::<Literal>()` ⟶ ok - `"0 1".parse::<Literal>()` ⟶ LexError - `" 0".parse::<Literal>()` ⟶ LexError - `"0 ".parse::<Literal>()` ⟶ LexError - `"/* comment */0".parse::<Literal>()` ⟶ LexError - `"0/* comment */".parse::<Literal>()` ⟶ LexError - `"0// comment".parse::<Literal>()` ⟶ LexError --- ## Use case ```rust let hex_int: Literal = format!("0x{:x}", int).parse().unwrap(); ``` The only way this is expressible in the current API is significantly worse. ```rust let hex_int = match format!("0x{:x}", int) .parse::<TokenStream>() .unwrap() .into_iter() .next() .unwrap() { TokenTree::Literal(literal) => literal, _ => unreachable!(), }; ```
- Loading branch information
Showing
7 changed files
with
138 additions
and
11 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
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
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,24 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
#![crate_name = "proc_macro_api_tests"] | ||
#![feature(proc_macro_span)] | ||
#![deny(dead_code)] // catch if a test function is never called | ||
|
||
extern crate proc_macro; | ||
|
||
mod cmp; | ||
mod parse; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
#[proc_macro] | ||
pub fn run(input: TokenStream) -> TokenStream { | ||
assert!(input.is_empty()); | ||
|
||
cmp::test(); | ||
parse::test(); | ||
|
||
TokenStream::new() | ||
} |
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,23 @@ | ||
use proc_macro::Literal; | ||
|
||
pub fn test() { | ||
test_parse_literal(); | ||
} | ||
|
||
fn test_parse_literal() { | ||
assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1"); | ||
assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0"); | ||
assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'"); | ||
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\""); | ||
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\""); | ||
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##"); | ||
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong"); | ||
|
||
assert!("0 1".parse::<Literal>().is_err()); | ||
assert!("'a".parse::<Literal>().is_err()); | ||
assert!(" 0".parse::<Literal>().is_err()); | ||
assert!("0 ".parse::<Literal>().is_err()); | ||
assert!("/* comment */0".parse::<Literal>().is_err()); | ||
assert!("0/* comment */".parse::<Literal>().is_err()); | ||
assert!("0// comment".parse::<Literal>().is_err()); | ||
} |
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,12 @@ | ||
// check-pass | ||
// aux-build:api/mod.rs | ||
|
||
//! This is for everything that *would* be a #[test] inside of libproc_macro, | ||
//! except for the fact that proc_macro objects are not capable of existing | ||
//! inside of an ordinary Rust test execution, only inside a macro. | ||
|
||
extern crate proc_macro_api_tests; | ||
|
||
proc_macro_api_tests::run!(); | ||
|
||
fn main() {} |