Skip to content

Handle negative literals in token_to_literal #19371

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 33 additions & 13 deletions crates/tt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::fmt;
use buffer::Cursor;
use intern::Symbol;
use iter::{TtElement, TtIter};
use stdx::{impl_from, itertools::Itertools as _};
use stdx::impl_from;

pub use text_size::{TextRange, TextSize};

Expand Down Expand Up @@ -560,18 +560,38 @@ where
{
use rustc_lexer::LiteralKind;

let token = rustc_lexer::tokenize(text).next_tuple();
let Some((rustc_lexer::Token {
kind: rustc_lexer::TokenKind::Literal { kind, suffix_start },
..
},)) = token
else {
return Literal {
span,
symbol: Symbol::intern(text),
kind: LitKind::Err(()),
suffix: None,
};
let mut tokens = rustc_lexer::tokenize(text);

let (kind, suffix_start) = match (tokens.next(), tokens.next()) {
// Negative int/float literals, handle leading minus.
(
Some(rustc_lexer::Token { kind: rustc_lexer::TokenKind::Minus, .. }),
Some(rustc_lexer::Token {
kind:
rustc_lexer::TokenKind::Literal {
kind: kind @ (LiteralKind::Int { .. } | LiteralKind::Float { .. }),
suffix_start,
},
..
}),
) => (kind, suffix_start + 1),
// Other literals
(
Some(rustc_lexer::Token {
kind: rustc_lexer::TokenKind::Literal { kind, suffix_start },
..
}),
None,
) => (kind, suffix_start),
// Not a literal
_ => {
return Literal {
span,
symbol: Symbol::intern(text),
kind: LitKind::Err(()),
suffix: None,
}
}
};

let (kind, start_offset, end_offset) = match kind {
Expand Down