Skip to content

Commit

Permalink
Merge pull request #92 from dtolnay/literal
Browse files Browse the repository at this point in the history
Support Literal as result of paste
  • Loading branch information
dtolnay committed Dec 12, 2022
2 parents 81cdb4f + b3b77f2 commit 80d037b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
33 changes: 33 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::env;
use std::process::Command;
use std::str;

fn main() {
println!("cargo:rerun-if-changed=build.rs");

let version = match rustc_version() {
Some(version) => version,
None => return,
};

if version.minor < 54 {
// https://github.com/rust-lang/rust/pull/84717
println!("cargo:rustc-cfg=no_literal_fromstr");
}
}

struct RustcVersion {
minor: u32,
}

fn rustc_version() -> Option<RustcVersion> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let minor = pieces.next()?.parse().ok()?;
Some(RustcVersion { minor })
}
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,26 @@ fn parse_bracket_as_segments(input: TokenStream, scope: Span) -> Result<Vec<Segm
fn pasted_to_tokens(mut pasted: String, span: Span) -> Result<TokenStream> {
let mut tokens = TokenStream::new();

#[cfg(not(no_literal_fromstr))]
{
use proc_macro::{LexError, Literal};
use std::str::FromStr;

if pasted.starts_with(|ch: char| ch.is_ascii_digit()) {
let literal = match panic::catch_unwind(|| Literal::from_str(&pasted)) {
Ok(Ok(literal)) => TokenTree::Literal(literal),
Ok(Err(LexError { .. })) | Err(_) => {
return Err(Error::new(
span,
&format!("`{:?}` is not a valid literal", pasted),
));
}
};
tokens.extend(iter::once(literal));
return Ok(tokens);
}
}

if pasted.starts_with('\'') {
let mut apostrophe = TokenTree::Punct(Punct::new('\'', Spacing::Joint));
apostrophe.set_span(span);
Expand Down
13 changes: 12 additions & 1 deletion tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_repeat() {
}

#[test]
fn test_literals() {
fn test_literal_to_identifier() {
const CONST0: &str = "const0";

let pasted = paste!([<CONST 0>]);
Expand All @@ -45,6 +45,17 @@ fn test_literals() {
assert_eq!(pasted, CONST0);
}

#[test]
fn test_literal_suffix() {
macro_rules! literal {
($bit:tt) => {
paste!([<1_u $bit>])
};
}

assert_eq!(literal!(32), 1);
}

#[test]
fn test_underscore() {
paste! {
Expand Down
12 changes: 8 additions & 4 deletions tests/ui/invalid-ident.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
error: `"0f"` is not a valid identifier
--> tests/ui/invalid-ident.rs:4:8
error: expected identifier, found `0f`
--> tests/ui/invalid-ident.rs:3:1
|
4 | fn [<0 f>]() {}
| ^^^^^^^
3 | / paste! {
4 | | fn [<0 f>]() {}
5 | | }
| |_^ expected identifier
|
= note: this error originates in the macro `paste` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `"f\""` is not a valid identifier
--> tests/ui/invalid-ident.rs:8:8
Expand Down

0 comments on commit 80d037b

Please sign in to comment.