Skip to content

Commit a1ac372

Browse files
authored
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!(), }; ```
2 parents f94942d + 34585cb commit a1ac372

File tree

7 files changed

+138
-11
lines changed

7 files changed

+138
-11
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::base::{ExtCtxt, ResolverExpand};
22

33
use rustc_ast as ast;
4-
use rustc_ast::token;
5-
use rustc_ast::token::Nonterminal;
6-
use rustc_ast::token::NtIdent;
4+
use rustc_ast::token::{self, Nonterminal, NtIdent, TokenKind};
75
use rustc_ast::tokenstream::{self, CanSynthesizeMissingTokens};
86
use rustc_ast::tokenstream::{DelimSpan, Spacing::*, TokenStream, TreeAndSpacing};
97
use rustc_ast_pretty::pprust;
@@ -541,6 +539,33 @@ impl server::Ident for Rustc<'_> {
541539
}
542540

543541
impl server::Literal for Rustc<'_> {
542+
fn from_str(&mut self, s: &str) -> Result<Self::Literal, ()> {
543+
let override_span = None;
544+
let stream = parse_stream_from_source_str(
545+
FileName::proc_macro_source_code(s),
546+
s.to_owned(),
547+
self.sess,
548+
override_span,
549+
);
550+
if stream.len() != 1 {
551+
return Err(());
552+
}
553+
let tree = stream.into_trees().next().unwrap();
554+
let token = match tree {
555+
tokenstream::TokenTree::Token(token) => token,
556+
tokenstream::TokenTree::Delimited { .. } => return Err(()),
557+
};
558+
let span_data = token.span.data();
559+
if (span_data.hi.0 - span_data.lo.0) as usize != s.len() {
560+
// There is a comment or whitespace adjacent to the literal.
561+
return Err(());
562+
}
563+
let lit = match token.kind {
564+
TokenKind::Literal(lit) => lit,
565+
_ => return Err(()),
566+
};
567+
Ok(Literal { lit, span: self.call_site })
568+
}
544569
fn debug_kind(&mut self, literal: &Self::Literal) -> String {
545570
format!("{:?}", literal.lit.kind)
546571
}

library/proc_macro/src/bridge/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ macro_rules! with_api {
107107
Literal {
108108
fn drop($self: $S::Literal);
109109
fn clone($self: &$S::Literal) -> $S::Literal;
110+
fn from_str(s: &str) -> Result<$S::Literal, ()>;
110111
fn debug_kind($self: &$S::Literal) -> String;
111112
fn symbol($self: &$S::Literal) -> String;
112113
fn suffix($self: &$S::Literal) -> Option<String>;
@@ -315,6 +316,19 @@ impl<T: Unmark> Unmark for Option<T> {
315316
}
316317
}
317318

319+
impl<T: Mark, E: Mark> Mark for Result<T, E> {
320+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
321+
fn mark(unmarked: Self::Unmarked) -> Self {
322+
unmarked.map(T::mark).map_err(E::mark)
323+
}
324+
}
325+
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
326+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
327+
fn unmark(self) -> Self::Unmarked {
328+
self.map(T::unmark).map_err(E::unmark)
329+
}
330+
}
331+
318332
macro_rules! mark_noop {
319333
($($ty:ty),* $(,)?) => {
320334
$(

library/proc_macro/src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub struct LexError {
9191
_inner: (),
9292
}
9393

94+
impl LexError {
95+
fn new() -> Self {
96+
LexError { _inner: () }
97+
}
98+
}
99+
94100
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
95101
impl fmt::Display for LexError {
96102
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1171,6 +1177,28 @@ impl Literal {
11711177
}
11721178
}
11731179

1180+
/// Parse a single literal from its stringified representation.
1181+
///
1182+
/// In order to parse successfully, the input string must not contain anything
1183+
/// but the literal token. Specifically, it must not contain whitespace or
1184+
/// comments in addition to the literal.
1185+
///
1186+
/// The resulting literal token will have a `Span::call_site()` span.
1187+
///
1188+
/// NOTE: some errors may cause panics instead of returning `LexError`. We
1189+
/// reserve the right to change these errors into `LexError`s later.
1190+
#[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
1191+
impl FromStr for Literal {
1192+
type Err = LexError;
1193+
1194+
fn from_str(src: &str) -> Result<Self, LexError> {
1195+
match bridge::client::Literal::from_str(src) {
1196+
Ok(literal) => Ok(Literal(literal)),
1197+
Err(()) => Err(LexError::new()),
1198+
}
1199+
}
1200+
}
1201+
11741202
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
11751203
// based on it (the reverse of the usual relationship between the two).
11761204
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#![feature(proc_macro_span)]
1+
use proc_macro::{LineColumn, Punct, Spacing};
22

3-
use proc_macro::{LineColumn, Punct};
3+
pub fn test() {
4+
test_line_column_ord();
5+
test_punct_eq();
6+
}
47

5-
#[test]
68
fn test_line_column_ord() {
79
let line0_column0 = LineColumn { line: 0, column: 0 };
810
let line0_column1 = LineColumn { line: 0, column: 1 };
@@ -11,10 +13,9 @@ fn test_line_column_ord() {
1113
assert!(line0_column1 < line1_column0);
1214
}
1315

14-
#[test]
1516
fn test_punct_eq() {
16-
// Good enough if it typechecks, since proc_macro::Punct can't exist in a test.
17-
fn _check(punct: Punct) {
18-
let _ = punct == ':';
19-
}
17+
let colon_alone = Punct::new(':', Spacing::Alone);
18+
assert_eq!(colon_alone, ':');
19+
let colon_joint = Punct::new(':', Spacing::Joint);
20+
assert_eq!(colon_joint, ':');
2021
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
#![crate_name = "proc_macro_api_tests"]
6+
#![feature(proc_macro_span)]
7+
#![deny(dead_code)] // catch if a test function is never called
8+
9+
extern crate proc_macro;
10+
11+
mod cmp;
12+
mod parse;
13+
14+
use proc_macro::TokenStream;
15+
16+
#[proc_macro]
17+
pub fn run(input: TokenStream) -> TokenStream {
18+
assert!(input.is_empty());
19+
20+
cmp::test();
21+
parse::test();
22+
23+
TokenStream::new()
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use proc_macro::Literal;
2+
3+
pub fn test() {
4+
test_parse_literal();
5+
}
6+
7+
fn test_parse_literal() {
8+
assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1");
9+
assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0");
10+
assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'");
11+
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
12+
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
13+
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
14+
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong");
15+
16+
assert!("0 1".parse::<Literal>().is_err());
17+
assert!("'a".parse::<Literal>().is_err());
18+
assert!(" 0".parse::<Literal>().is_err());
19+
assert!("0 ".parse::<Literal>().is_err());
20+
assert!("/* comment */0".parse::<Literal>().is_err());
21+
assert!("0/* comment */".parse::<Literal>().is_err());
22+
assert!("0// comment".parse::<Literal>().is_err());
23+
}

src/test/ui/proc-macro/test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
// aux-build:api/mod.rs
3+
4+
//! This is for everything that *would* be a #[test] inside of libproc_macro,
5+
//! except for the fact that proc_macro objects are not capable of existing
6+
//! inside of an ordinary Rust test execution, only inside a macro.
7+
8+
extern crate proc_macro_api_tests;
9+
10+
proc_macro_api_tests::run!();
11+
12+
fn main() {}

0 commit comments

Comments
 (0)