Skip to content

Commit 1c7c573

Browse files
committed
Address review comments.
1 parent 2f89a95 commit 1c7c573

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

compiler/rustc_parse/src/parser/expr.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,21 @@ impl<'a> Parser<'a> {
993993
(span.shrink_to_hi(), format!("`{}`", snippet))
994994
}
995995
(token::CloseDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))), _) => {
996-
// Don't need to report an error in this case.
996+
// No need to report an error. This case will only occur when parsing a pasted
997+
// metavariable, and we should have emitted an error when parsing the macro call in
998+
// the first place. E.g. in this code:
999+
// ```
1000+
// macro_rules! m { ($e:expr) => { $e }; }
1001+
//
1002+
// fn main() {
1003+
// let f = 1;
1004+
// m!(f.);
1005+
// }
1006+
// ```
1007+
// we'll get an error "unexpected token: `)` when parsing the `m!(f.)`, so we don't
1008+
// want to issue a second error when parsing the expansion `«f.»` (where `«`/`»`
1009+
// represent the invisible delimiters).
1010+
self.dcx().span_delayed_bug(span, "bad dot expr in metavariable");
9971011
return;
9981012
}
9991013
_ => (span, actual),
@@ -2086,7 +2100,7 @@ impl<'a> Parser<'a> {
20862100
.or_else(|()| self.handle_missing_lit(Parser::mk_meta_item_lit_char))
20872101
}
20882102

2089-
fn recover_after_dot(&mut self) -> Option<Token> {
2103+
fn recover_after_dot(&mut self) {
20902104
let mut recovered = None;
20912105
if self.token == token::Dot {
20922106
// Attempt to recover `.4` as `0.4`. We don't currently have any syntax where
@@ -2121,12 +2135,14 @@ impl<'a> Parser<'a> {
21212135
}
21222136
}
21232137

2124-
recovered
2138+
if let Some(recovered) = recovered {
2139+
self.token = recovered;
2140+
}
21252141
}
21262142

21272143
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
21282144
/// `Lit::from_token` (excluding unary negation).
2129-
pub fn eat_token_lit(&mut self) -> Option<token::Lit> {
2145+
fn eat_token_lit(&mut self) -> Option<token::Lit> {
21302146
match self.token.uninterpolate().kind {
21312147
token::Ident(name, IdentIsRaw::No) if name.is_bool_lit() => {
21322148
self.bump();
@@ -2165,34 +2181,28 @@ impl<'a> Parser<'a> {
21652181
/// Matches `lit = true | false | token_lit`.
21662182
/// Returns `None` if the next token is not a literal.
21672183
fn parse_opt_token_lit(&mut self) -> Option<(token::Lit, Span)> {
2168-
match self.recover_after_dot() {
2169-
Some(recovered) => self.token = recovered,
2170-
None => {}
2171-
}
2184+
self.recover_after_dot();
21722185
let span = self.token.span;
21732186
self.eat_token_lit().map(|token_lit| (token_lit, span))
21742187
}
21752188

21762189
/// Matches `lit = true | false | token_lit`.
21772190
/// Returns `None` if the next token is not a literal.
21782191
fn parse_opt_meta_item_lit(&mut self) -> Option<MetaItemLit> {
2179-
match self.recover_after_dot() {
2180-
Some(recovered) => self.token = recovered,
2181-
None => {}
2182-
}
2192+
self.recover_after_dot();
21832193
let span = self.token.span;
2184-
let span2 = self.uninterpolated_token_span();
2194+
let uninterpolated_span = self.uninterpolated_token_span();
21852195
self.eat_token_lit().map(|token_lit| {
21862196
match MetaItemLit::from_token_lit(token_lit, span) {
21872197
Ok(lit) => lit,
21882198
Err(err) => {
2189-
let guar = report_lit_error(&self.psess, err, token_lit, span2);
2199+
let guar = report_lit_error(&self.psess, err, token_lit, uninterpolated_span);
21902200
// Pack possible quotes and prefixes from the original literal into
21912201
// the error literal's symbol so they can be pretty-printed faithfully.
21922202
let suffixless_lit = token::Lit::new(token_lit.kind, token_lit.symbol, None);
21932203
let symbol = Symbol::intern(&suffixless_lit.to_string());
21942204
let token_lit = token::Lit::new(token::Err(guar), symbol, token_lit.suffix);
2195-
MetaItemLit::from_token_lit(token_lit, span2).unwrap_or_else(|_| unreachable!())
2205+
MetaItemLit::from_token_lit(token_lit, uninterpolated_span).unwrap()
21962206
}
21972207
}
21982208
})

compiler/rustc_parse/src/parser/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1303,11 +1303,7 @@ impl<'a> Parser<'a> {
13031303
looker: impl FnOnce(&TokenTree) -> R,
13041304
) -> Option<R> {
13051305
assert_ne!(dist, 0);
1306-
1307-
match self.token_cursor.curr.look_ahead(dist - 1) {
1308-
Some(tt) => Some(looker(tt)),
1309-
_ => None,
1310-
}
1306+
self.token_cursor.curr.look_ahead(dist - 1).map(looker)
13111307
}
13121308

13131309
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.

0 commit comments

Comments
 (0)