Auto merge of #105160 - nnethercote:rm-Lit-token_lit, r=petrochenkov
Remove `token::Lit` from `ast::MetaItemLit`. Currently `ast::MetaItemLit` represents the literal kind twice. This PR removes that redundancy. Best reviewed one commit at a time. r? `@petrochenkov`
This commit is contained in:
commit
2cd2070af7
28 changed files with 244 additions and 171 deletions
|
@ -1534,15 +1534,16 @@ impl<'a> Parser<'a> {
|
|||
&& (matches!(self.token.kind, token::CloseDelim(_) | token::Comma)
|
||||
|| self.token.is_op())
|
||||
{
|
||||
let lit = self.recover_unclosed_char(label_.ident, |self_| {
|
||||
self_.sess.create_err(UnexpectedTokenAfterLabel {
|
||||
span: self_.token.span,
|
||||
remove_label: None,
|
||||
enclose_in_block: None,
|
||||
})
|
||||
});
|
||||
let (lit, _) =
|
||||
self.recover_unclosed_char(label_.ident, Parser::mk_token_lit_char, |self_| {
|
||||
self_.sess.create_err(UnexpectedTokenAfterLabel {
|
||||
span: self_.token.span,
|
||||
remove_label: None,
|
||||
enclose_in_block: None,
|
||||
})
|
||||
});
|
||||
consume_colon = false;
|
||||
Ok(self.mk_expr(lo, ExprKind::Lit(lit.token_lit)))
|
||||
Ok(self.mk_expr(lo, ExprKind::Lit(lit)))
|
||||
} else if !ate_colon
|
||||
&& (self.check_noexpect(&TokenKind::Comma) || self.check_noexpect(&TokenKind::Gt))
|
||||
{
|
||||
|
@ -1617,12 +1618,13 @@ impl<'a> Parser<'a> {
|
|||
Ok(expr)
|
||||
}
|
||||
|
||||
/// Emit an error when a char is parsed as a lifetime because of a missing quote
|
||||
pub(super) fn recover_unclosed_char(
|
||||
/// Emit an error when a char is parsed as a lifetime because of a missing quote.
|
||||
pub(super) fn recover_unclosed_char<L>(
|
||||
&self,
|
||||
lifetime: Ident,
|
||||
mk_lit_char: impl FnOnce(Symbol, Span) -> L,
|
||||
err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>,
|
||||
) -> ast::MetaItemLit {
|
||||
) -> L {
|
||||
if let Some(mut diag) =
|
||||
self.sess.span_diagnostic.steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar)
|
||||
{
|
||||
|
@ -1644,11 +1646,7 @@ impl<'a> Parser<'a> {
|
|||
.emit();
|
||||
}
|
||||
let name = lifetime.without_first_quote().name;
|
||||
ast::MetaItemLit {
|
||||
token_lit: token::Lit::new(token::LitKind::Char, name, None),
|
||||
kind: ast::LitKind::Char(name.as_str().chars().next().unwrap_or('_')),
|
||||
span: lifetime.span,
|
||||
}
|
||||
mk_lit_char(name, lifetime.span)
|
||||
}
|
||||
|
||||
/// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead.
|
||||
|
@ -1764,8 +1762,8 @@ impl<'a> Parser<'a> {
|
|||
Some(lit) => match lit.kind {
|
||||
ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
|
||||
style,
|
||||
symbol: lit.token_lit.symbol,
|
||||
suffix: lit.token_lit.suffix,
|
||||
symbol: lit.symbol,
|
||||
suffix: lit.suffix,
|
||||
span: lit.span,
|
||||
symbol_unescaped,
|
||||
}),
|
||||
|
@ -1775,7 +1773,23 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_missing_lit(&mut self) -> PResult<'a, MetaItemLit> {
|
||||
pub(crate) fn mk_token_lit_char(name: Symbol, span: Span) -> (token::Lit, Span) {
|
||||
(token::Lit { symbol: name, suffix: None, kind: token::Char }, span)
|
||||
}
|
||||
|
||||
fn mk_meta_item_lit_char(name: Symbol, span: Span) -> MetaItemLit {
|
||||
ast::MetaItemLit {
|
||||
symbol: name,
|
||||
suffix: None,
|
||||
kind: ast::LitKind::Char(name.as_str().chars().next().unwrap_or('_')),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_missing_lit<L>(
|
||||
&mut self,
|
||||
mk_lit_char: impl FnOnce(Symbol, Span) -> L,
|
||||
) -> PResult<'a, L> {
|
||||
if let token::Interpolated(inner) = &self.token.kind {
|
||||
let expr = match inner.as_ref() {
|
||||
token::NtExpr(expr) => Some(expr),
|
||||
|
@ -1799,7 +1813,7 @@ impl<'a> Parser<'a> {
|
|||
// On an error path, eagerly consider a lifetime to be an unclosed character lit
|
||||
if self.token.is_lifetime() {
|
||||
let lt = self.expect_lifetime();
|
||||
Ok(self.recover_unclosed_char(lt.ident, err))
|
||||
Ok(self.recover_unclosed_char(lt.ident, mk_lit_char, err))
|
||||
} else {
|
||||
Err(err(self))
|
||||
}
|
||||
|
@ -1808,11 +1822,13 @@ impl<'a> Parser<'a> {
|
|||
pub(super) fn parse_token_lit(&mut self) -> PResult<'a, (token::Lit, Span)> {
|
||||
self.parse_opt_token_lit()
|
||||
.ok_or(())
|
||||
.or_else(|()| self.handle_missing_lit().map(|lit| (lit.token_lit, lit.span)))
|
||||
.or_else(|()| self.handle_missing_lit(Parser::mk_token_lit_char))
|
||||
}
|
||||
|
||||
pub(super) fn parse_meta_item_lit(&mut self) -> PResult<'a, MetaItemLit> {
|
||||
self.parse_opt_meta_item_lit().ok_or(()).or_else(|()| self.handle_missing_lit())
|
||||
self.parse_opt_meta_item_lit()
|
||||
.ok_or(())
|
||||
.or_else(|()| self.handle_missing_lit(Parser::mk_meta_item_lit_char))
|
||||
}
|
||||
|
||||
fn recover_after_dot(&mut self) -> Option<Token> {
|
||||
|
|
|
@ -411,16 +411,20 @@ impl<'a> Parser<'a> {
|
|||
{
|
||||
// Recover a `'a` as a `'a'` literal
|
||||
let lt = self.expect_lifetime();
|
||||
let lit = self.recover_unclosed_char(lt.ident, |self_| {
|
||||
let expected = expected.unwrap_or("pattern");
|
||||
let msg =
|
||||
format!("expected {}, found {}", expected, super::token_descr(&self_.token));
|
||||
let (lit, _) =
|
||||
self.recover_unclosed_char(lt.ident, Parser::mk_token_lit_char, |self_| {
|
||||
let expected = expected.unwrap_or("pattern");
|
||||
let msg = format!(
|
||||
"expected {}, found {}",
|
||||
expected,
|
||||
super::token_descr(&self_.token)
|
||||
);
|
||||
|
||||
let mut err = self_.struct_span_err(self_.token.span, &msg);
|
||||
err.span_label(self_.token.span, format!("expected {}", expected));
|
||||
err
|
||||
});
|
||||
PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit.token_lit)))
|
||||
let mut err = self_.struct_span_err(self_.token.span, &msg);
|
||||
err.span_label(self_.token.span, format!("expected {}", expected));
|
||||
err
|
||||
});
|
||||
PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit)))
|
||||
} else {
|
||||
// Try to parse everything else as literal with optional minus
|
||||
match self.parse_literal_maybe_minus() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue