Skip to content

rustc: Tweak Literal APIs of proc_macro #48894

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

Closed
Closed
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
34 changes: 28 additions & 6 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,24 +471,37 @@ impl fmt::Display for Literal {
}

macro_rules! int_literals {
($($int_kind:ident),*) => {$(
($($int_kind:ident => $signed:ident,)*) => {$(
/// Integer literal.
#[unstable(feature = "proc_macro", issue = "38356")]
pub fn $int_kind(n: $int_kind) -> Literal {
Literal::typed_integer(n as i128, stringify!($int_kind))
pub fn $int_kind(n: $signed) -> Literal {
Literal::typed_integer(n as u128, stringify!($int_kind))
}

/// Integer literal.
#[unstable(feature = "proc_macro", issue = "38356")]
pub fn $signed(n: $signed) -> Literal {
Literal::typed_integer(n as u128, stringify!($signed))
}
)*}
}

impl Literal {
/// Integer literal
#[unstable(feature = "proc_macro", issue = "38356")]
pub fn integer(n: i128) -> Literal {
pub fn integer(n: u128) -> Literal {
Literal(token::Literal(token::Lit::Integer(Symbol::intern(&n.to_string())), None))
}

int_literals!(u8, i8, u16, i16, u32, i32, u64, i64, usize, isize);
fn typed_integer(n: i128, kind: &'static str) -> Literal {
int_literals!(
i8 => u8,
i16 => u16,
i32 => u32,
i64 => u64,
isize => usize,
);

fn typed_integer(n: u128, kind: &'static str) -> Literal {
Literal(token::Literal(token::Lit::Integer(Symbol::intern(&n.to_string())),
Some(Symbol::intern(kind))))
}
Expand All @@ -499,6 +512,9 @@ impl Literal {
if !n.is_finite() {
panic!("Invalid float literal {}", n);
}
if n.is_sign_negative() {
panic!("float literal cannot be negative {}", n);
}
Literal(token::Literal(token::Lit::Float(Symbol::intern(&n.to_string())), None))
}

Expand All @@ -508,6 +524,9 @@ impl Literal {
if !n.is_finite() {
panic!("Invalid f32 literal {}", n);
}
if n.is_sign_negative() {
panic!("f32 literal cannot be negative {}", n);
}
Literal(token::Literal(token::Lit::Float(Symbol::intern(&n.to_string())),
Some(Symbol::intern("f32"))))
}
Expand All @@ -518,6 +537,9 @@ impl Literal {
if !n.is_finite() {
panic!("Invalid f64 literal {}", n);
}
if n.is_sign_negative() {
panic!("f64 literal cannot be negative {}", n);
}
Literal(token::Literal(token::Lit::Float(Symbol::intern(&n.to_string())),
Some(Symbol::intern("f64"))))
}
Expand Down
2 changes: 1 addition & 1 deletion src/libproc_macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'a> Quote for &'a str {

impl Quote for usize {
fn quote(self) -> TokenStream {
TokenNode::Literal(Literal::integer(self as i128)).into()
TokenNode::Literal(Literal::integer(self as u128)).into()
}
}

Expand Down