Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Support Literal as result of paste #92

Merged
merged 4 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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