Skip to content

Commit

Permalink
Auto merge of rust-lang#60965 - petrochenkov:lit3, r=matklad
Browse files Browse the repository at this point in the history
syntax: Continue refactoring literals

A follow up to rust-lang#60679.

rust-lang@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.

rust-lang@d9516d1: Gathers all components of a literal token in a single struct.
  • Loading branch information
bors committed May 23, 2019
2 parents f688ba6 + 90d15e7 commit 27cc0db
Show file tree
Hide file tree
Showing 27 changed files with 526 additions and 558 deletions.
3 changes: 1 addition & 2 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,8 +1249,7 @@ impl<'a> State<'a> {

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

pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
Expand Down
34 changes: 18 additions & 16 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
impl_stable_hash_for!(struct ::syntax::ast::Lit {
node,
token,
suffix,
span
});

Expand Down Expand Up @@ -288,17 +287,23 @@ for tokenstream::TokenStream {
}
}

impl_stable_hash_for!(enum token::Lit {
Bool(val),
Byte(val),
Char(val),
Err(val),
Integer(val),
Float(val),
Str_(val),
ByteStr(val),
StrRaw(val, n),
ByteStrRaw(val, n)
impl_stable_hash_for!(enum token::LitKind {
Bool,
Byte,
Char,
Integer,
Float,
Str,
ByteStr,
StrRaw(n),
ByteStrRaw(n),
Err
});

impl_stable_hash_for!(struct token::Lit {
kind,
symbol,
suffix
});

fn hash_token<'a, 'gcx, W: StableHasherResult>(
Expand Down Expand Up @@ -348,10 +353,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
token::Token::CloseDelim(delim_token) => {
std_hash::Hash::hash(&delim_token, hasher);
}
token::Token::Literal(lit, opt_name) => {
lit.hash_stable(hcx, hasher);
opt_name.hash_stable(hcx, hasher);
}
token::Token::Literal(lit) => lit.hash_stable(hcx, hasher),

token::Token::Ident(ident, is_raw) => {
ident.name.hash_stable(hcx, hasher);
Expand Down
14 changes: 7 additions & 7 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,17 @@ impl<'a> Classifier<'a> {
}
}

token::Literal(lit, _suf) => {
match lit {
token::Literal(lit) => {
match lit.kind {
// Text literals.
token::Byte(..) | token::Char(..) | token::Err(..) |
token::ByteStr(..) | token::ByteStrRaw(..) |
token::Str_(..) | token::StrRaw(..) => Class::String,
token::Byte | token::Char | token::Err |
token::ByteStr | token::ByteStrRaw(..) |
token::Str | token::StrRaw(..) => Class::String,

// Number literals.
token::Integer(..) | token::Float(..) => Class::Number,
token::Integer | token::Float => Class::Number,

token::Bool(..) => panic!("literal token contains `Lit::Bool`"),
token::Bool => panic!("literal token contains `Lit::Bool`"),
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,6 @@ pub enum StrStyle {
pub struct Lit {
/// The original literal token as written in source code.
pub token: token::Lit,
/// The original literal suffix as written in source code.
pub suffix: Option<Symbol>,
/// The "semantic" representation of the literal lowered from the original tokens.
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ impl MetaItemKind {
Some(TokenTree::Token(_, token::Eq)) => {
tokens.next();
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
Lit::from_token(&token, span, None).map(MetaItemKind::NameValue)
Lit::from_token(&token, span).ok().map(MetaItemKind::NameValue)
} else {
None
};
Expand Down Expand Up @@ -599,7 +599,7 @@ impl NestedMetaItem {
where I: Iterator<Item = TokenTree>,
{
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
if let Some(lit) = Lit::from_token(&token, span, None) {
if let Ok(lit) = Lit::from_token(&token, span) {
tokens.next();
return Some(NestedMetaItem::Literal(lit));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
},
(3, Some(&TokenTree::Token(_, token::Ident(ref code, _))),
Some(&TokenTree::Token(_, token::Comma)),
Some(&TokenTree::Token(_, token::Literal(token::StrRaw(description, _), None)))) => {
(code, Some(description))
Some(&TokenTree::Token(_, token::Literal(token::Lit { symbol, .. })))) => {
(code, Some(symbol))
}
_ => unreachable!()
};
Expand Down
Loading

0 comments on commit 27cc0db

Please sign in to comment.