Skip to content

Commit dce0f7f

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 #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 9c09116 commit dce0f7f

File tree

1 file changed

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

1 file changed

+19
-16
lines changed

compiler/rustc_parse/src/parser/expr.rs

+19-16
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 }) => {
@@ -1010,39 +1011,41 @@ impl<'a> Parser<'a> {
10101011
Ok(match self.break_up_float(symbol, self.token.span) {
10111012
// 1e2
10121013
DestructuredFloat::Single(sym, _sp) => {
1014+
// `foo.1e2`: a single complete dot access, fully consumed. We end up with
1015+
// the `1e2` token in `self.prev_token` and the following token in
1016+
// `self.token`.
10131017
let ident_span = self.token.span;
10141018
self.bump();
10151019
self.mk_expr_tuple_field_access(lo, ident_span, base, sym, suffix)
10161020
}
10171021
// 1.
10181022
DestructuredFloat::TrailingDot(sym, ident_span, dot_span) => {
1023+
// `foo.1.`: a single complete dot access and the start of another.
1024+
// We end up with the `sym` (`1`) token in `self.prev_token` and a dot in
1025+
// `self.token`.
10191026
assert!(suffix.is_none());
10201027
self.token = Token::new(token::Ident(sym, IdentIsRaw::No), ident_span);
1021-
let ident_span = self.token.span;
10221028
self.bump_with((Token::new(token::Dot, dot_span), self.token_spacing));
10231029
self.mk_expr_tuple_field_access(lo, ident_span, base, sym, None)
10241030
}
10251031
// 1.2 | 1.2e3
10261032
DestructuredFloat::MiddleDot(
1027-
symbol1,
1033+
sym1,
10281034
ident1_span,
1029-
dot_span,
1030-
symbol2,
1035+
_dot_span,
1036+
sym2,
10311037
ident2_span,
10321038
) => {
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);
1039+
// `foo.1.2` (or `foo.1.2e3`): two complete dot accesses. We end up with
1040+
// the `sym2` (`2` or `2e3`) token in `self.prev_token` and the following
1041+
// token in `self.token`.
10401042
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;
1043+
Token::new(token::Ident(sym2, IdentIsRaw::No), ident2_span);
1044+
self.bump_with((next_token2, self.token_spacing));
10441045
self.bump();
1045-
self.mk_expr_tuple_field_access(lo, ident_span, base1, symbol2, suffix)
1046+
let base1 =
1047+
self.mk_expr_tuple_field_access(lo, ident1_span, base, sym1, None);
1048+
self.mk_expr_tuple_field_access(lo, ident2_span, base1, sym2, suffix)
10461049
}
10471050
DestructuredFloat::Error => base,
10481051
})

0 commit comments

Comments
 (0)