Skip to content

Commit 421d9c1

Browse files
committed
Clarify parse_dot_suffix_expr.
For the `MiddleDot` case, current behaviour: - For a case like `1.2`, `sym1` is `1` and `sym2` is `2`, and `self.token` holds `1.2`. - It creates a new ident token from `sym1` that it puts into `self.token`. - Then it does `bump_with` with a new dot token, which moves the `sym1` token into `prev_token`. - Then it does `bump_with` with a new ident token from `sym2`, which moves the `dot` token into `prev_token` and discards the `sym1` token. - Then it does `bump`, which puts whatever is next into `self.token`, moves the `sym2` token into `prev_token`, and discards the `dot` token altogether. New behaviour: - Skips creating and inserting the `sym1` and dot tokens, because they are unnecessary. - This also demonstrates that the comment about `Spacing::Alone` is wrong -- that value is never used. That comment was added in rust-lang#77250, and AFAICT it has always been incorrect. The commit also expands comments. I found this code hard to read previously, the examples in comments make it easier.
1 parent f311ef0 commit 421d9c1

File tree

1 file changed

+19
-19
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+19
-19
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use core::mem;
1616
use core::ops::ControlFlow;
1717
use rustc_ast::ptr::P;
1818
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
19-
use rustc_ast::tokenstream::Spacing;
2019
use rustc_ast::util::case::Case;
2120
use rustc_ast::util::classify;
2221
use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
@@ -999,6 +998,8 @@ impl<'a> Parser<'a> {
999998
}
1000999

10011000
pub fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
1001+
// At this point we've consumed something like `expr.` and `self.token` holds the token
1002+
// after the dot.
10021003
match self.token.uninterpolate().kind {
10031004
token::Ident(..) => self.parse_dot_suffix(base, lo),
10041005
token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
@@ -1008,41 +1009,40 @@ impl<'a> Parser<'a> {
10081009
}
10091010
token::Literal(token::Lit { kind: token::Float, symbol, suffix }) => {
10101011
Ok(match self.break_up_float(symbol, self.token.span) {
1011-
// 1e2
10121012
DestructuredFloat::Single(sym, _sp) => {
1013+
// `foo.1e2`: a single complete dot access, fully consumed. We end up with
1014+
// the `1e2` token in `self.prev_token` and the following token in
1015+
// `self.token`.
10131016
let ident_span = self.token.span;
10141017
self.bump();
10151018
self.mk_expr_tuple_field_access(lo, ident_span, base, sym, suffix)
10161019
}
1017-
// 1.
10181020
DestructuredFloat::TrailingDot(sym, ident_span, dot_span) => {
1021+
// `foo.1.`: a single complete dot access and the start of another.
1022+
// We end up with the `sym` (`1`) token in `self.prev_token` and a dot in
1023+
// `self.token`.
10191024
assert!(suffix.is_none());
10201025
self.token = Token::new(token::Ident(sym, IdentIsRaw::No), ident_span);
1021-
let ident_span = self.token.span;
10221026
self.bump_with((Token::new(token::Dot, dot_span), self.token_spacing));
10231027
self.mk_expr_tuple_field_access(lo, ident_span, base, sym, None)
10241028
}
1025-
// 1.2 | 1.2e3
10261029
DestructuredFloat::MiddleDot(
1027-
symbol1,
1030+
sym1,
10281031
ident1_span,
1029-
dot_span,
1030-
symbol2,
1032+
_dot_span,
1033+
sym2,
10311034
ident2_span,
10321035
) => {
1033-
self.token = Token::new(token::Ident(symbol1, IdentIsRaw::No), ident1_span);
1034-
// This needs to be `Spacing::Alone` to prevent regressions.
1035-
// See issue #76399 and PR #76285 for more details
1036-
let ident_span = self.token.span;
1037-
self.bump_with((Token::new(token::Dot, dot_span), Spacing::Alone));
1038-
let base1 =
1039-
self.mk_expr_tuple_field_access(lo, ident_span, base, symbol1, None);
1036+
// `foo.1.2` (or `foo.1.2e3`): two complete dot accesses. We end up with
1037+
// the `sym2` (`2` or `2e3`) token in `self.prev_token` and the following
1038+
// token in `self.token`.
10401039
let next_token2 =
1041-
Token::new(token::Ident(symbol2, IdentIsRaw::No), ident2_span);
1042-
self.bump_with((next_token2, self.token_spacing)); // `.`
1043-
let ident_span = self.token.span;
1040+
Token::new(token::Ident(sym2, IdentIsRaw::No), ident2_span);
1041+
self.bump_with((next_token2, self.token_spacing));
10441042
self.bump();
1045-
self.mk_expr_tuple_field_access(lo, ident_span, base1, symbol2, suffix)
1043+
let base1 =
1044+
self.mk_expr_tuple_field_access(lo, ident1_span, base, sym1, None);
1045+
self.mk_expr_tuple_field_access(lo, ident2_span, base1, sym2, suffix)
10461046
}
10471047
DestructuredFloat::Error => base,
10481048
})

0 commit comments

Comments
 (0)