Parameterise Parser::{recover_unclosed_char,handle_missing_lit}.

These two methods both produce a `MetaItemLit`, and then some of the
call sites convert the `MetaItemLit` to a `token::Lit` with
`as_token_lit`.

This commit parameterises these two methods with a `mk_lit_char`
closure, which can be used to produce either `MetaItemLit` or
`token::Lit` directly as necessary.
This commit is contained in:
Nicholas Nethercote 2022-12-05 14:39:56 +11:00
parent 4ae956f600
commit d887615b4c
2 changed files with 49 additions and 30 deletions

View file

@ -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.as_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() {