Skip to content

Commit 27cc0db

Browse files
committed
Auto merge of #60965 - petrochenkov:lit3, r=matklad
syntax: Continue refactoring literals A follow up to #60679. a2fd002: Similarly to `EscapeError`, literal parsing now produces a `LitError`. This way we can get rid of `diag: Option<(Span, &Handler)>` in interfaces while leaving attr/mod alone. d9516d1: Gathers all components of a literal token in a single struct.
2 parents f688ba6 + 90d15e7 commit 27cc0db

27 files changed

+526
-558
lines changed

src/librustc/hir/print.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,7 @@ impl<'a> State<'a> {
12491249

12501250
fn print_literal(&mut self, lit: &hir::Lit) -> io::Result<()> {
12511251
self.maybe_print_comment(lit.span.lo())?;
1252-
let (token, suffix) = lit.node.to_lit_token();
1253-
self.writer().word(pprust::literal_to_string(token, suffix))
1252+
self.writer().word(pprust::literal_to_string(lit.node.to_lit_token()))
12541253
}
12551254

12561255
pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {

src/librustc/ich/impls_syntax.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
165165
impl_stable_hash_for!(struct ::syntax::ast::Lit {
166166
node,
167167
token,
168-
suffix,
169168
span
170169
});
171170

@@ -288,17 +287,23 @@ for tokenstream::TokenStream {
288287
}
289288
}
290289

291-
impl_stable_hash_for!(enum token::Lit {
292-
Bool(val),
293-
Byte(val),
294-
Char(val),
295-
Err(val),
296-
Integer(val),
297-
Float(val),
298-
Str_(val),
299-
ByteStr(val),
300-
StrRaw(val, n),
301-
ByteStrRaw(val, n)
290+
impl_stable_hash_for!(enum token::LitKind {
291+
Bool,
292+
Byte,
293+
Char,
294+
Integer,
295+
Float,
296+
Str,
297+
ByteStr,
298+
StrRaw(n),
299+
ByteStrRaw(n),
300+
Err
301+
});
302+
303+
impl_stable_hash_for!(struct token::Lit {
304+
kind,
305+
symbol,
306+
suffix
302307
});
303308

304309
fn hash_token<'a, 'gcx, W: StableHasherResult>(
@@ -348,10 +353,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
348353
token::Token::CloseDelim(delim_token) => {
349354
std_hash::Hash::hash(&delim_token, hasher);
350355
}
351-
token::Token::Literal(lit, opt_name) => {
352-
lit.hash_stable(hcx, hasher);
353-
opt_name.hash_stable(hcx, hasher);
354-
}
356+
token::Token::Literal(lit) => lit.hash_stable(hcx, hasher),
355357

356358
token::Token::Ident(ident, is_raw) => {
357359
ident.name.hash_stable(hcx, hasher);

src/librustdoc/html/highlight.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,17 @@ impl<'a> Classifier<'a> {
310310
}
311311
}
312312

313-
token::Literal(lit, _suf) => {
314-
match lit {
313+
token::Literal(lit) => {
314+
match lit.kind {
315315
// Text literals.
316-
token::Byte(..) | token::Char(..) | token::Err(..) |
317-
token::ByteStr(..) | token::ByteStrRaw(..) |
318-
token::Str_(..) | token::StrRaw(..) => Class::String,
316+
token::Byte | token::Char | token::Err |
317+
token::ByteStr | token::ByteStrRaw(..) |
318+
token::Str | token::StrRaw(..) => Class::String,
319319

320320
// Number literals.
321-
token::Integer(..) | token::Float(..) => Class::Number,
321+
token::Integer | token::Float => Class::Number,
322322

323-
token::Bool(..) => panic!("literal token contains `Lit::Bool`"),
323+
token::Bool => panic!("literal token contains `Lit::Bool`"),
324324
}
325325
}
326326

src/libsyntax/ast.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1347,8 +1347,6 @@ pub enum StrStyle {
13471347
pub struct Lit {
13481348
/// The original literal token as written in source code.
13491349
pub token: token::Lit,
1350-
/// The original literal suffix as written in source code.
1351-
pub suffix: Option<Symbol>,
13521350
/// The "semantic" representation of the literal lowered from the original tokens.
13531351
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
13541352
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.

src/libsyntax/attr/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl MetaItemKind {
554554
Some(TokenTree::Token(_, token::Eq)) => {
555555
tokens.next();
556556
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
557-
Lit::from_token(&token, span, None).map(MetaItemKind::NameValue)
557+
Lit::from_token(&token, span).ok().map(MetaItemKind::NameValue)
558558
} else {
559559
None
560560
};
@@ -599,7 +599,7 @@ impl NestedMetaItem {
599599
where I: Iterator<Item = TokenTree>,
600600
{
601601
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
602-
if let Some(lit) = Lit::from_token(&token, span, None) {
602+
if let Ok(lit) = Lit::from_token(&token, span) {
603603
tokens.next();
604604
return Some(NestedMetaItem::Literal(lit));
605605
}

src/libsyntax/diagnostics/plugin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
7777
},
7878
(3, Some(&TokenTree::Token(_, token::Ident(ref code, _))),
7979
Some(&TokenTree::Token(_, token::Comma)),
80-
Some(&TokenTree::Token(_, token::Literal(token::StrRaw(description, _), None)))) => {
81-
(code, Some(description))
80+
Some(&TokenTree::Token(_, token::Literal(token::Lit { symbol, .. })))) => {
81+
(code, Some(symbol))
8282
}
8383
_ => unreachable!()
8484
};

0 commit comments

Comments
 (0)