Skip to content

Commit cfb8823

Browse files
only return offset when different to default
Co-authored-by: Vadim Petrochenkov <[email protected]>
1 parent eaeda23 commit cfb8823

File tree

1 file changed

+37
-27
lines changed
  • compiler/rustc_lexer/src

1 file changed

+37
-27
lines changed

compiler/rustc_lexer/src/lib.rs

+37-27
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ impl Cursor<'_> {
410410
// Numeric literal.
411411
c @ '0'..='9' => {
412412
let (literal_kind, suffix_start) = self.number(c);
413-
TokenKind::Literal { kind: literal_kind, suffix_start }
413+
TokenKind::Literal {
414+
kind: literal_kind,
415+
suffix_start: suffix_start.unwrap_or(self.pos_within_token()),
416+
}
414417
}
415418

416419
// Guarded string literal prefix: `#"` or `##`
@@ -604,9 +607,9 @@ impl Cursor<'_> {
604607
}
605608
}
606609

607-
/// Parses a number and in `.1` returns the offset of the literal suffix
608-
/// (this will be at the end of the token if there is no suffix)
609-
fn number(&mut self, first_digit: char) -> (LiteralKind, u32) {
610+
/// Parses a number and in `.1` returns the offset of the literal suffix if
611+
/// different from the output of `.pos_within_token()`.
612+
fn number(&mut self, first_digit: char) -> (LiteralKind, Option<u32>) {
610613
debug_assert!('0' <= self.prev() && self.prev() <= '9');
611614
let mut base = Base::Decimal;
612615
if first_digit == '0' {
@@ -617,26 +620,26 @@ impl Cursor<'_> {
617620
self.bump();
618621
if !self.eat_decimal_digits() {
619622
let suffix_start = self.pos_within_token();
620-
self.eat_literal_suffix();
621-
return (Int { base, empty_int: true }, suffix_start);
623+
let offset = self.eat_literal_suffix().then_some(suffix_start);
624+
return (Int { base, empty_int: true }, offset);
622625
}
623626
}
624627
'o' => {
625628
base = Base::Octal;
626629
self.bump();
627630
if !self.eat_decimal_digits() {
628631
let suffix_start = self.pos_within_token();
629-
self.eat_literal_suffix();
630-
return (Int { base, empty_int: true }, suffix_start);
632+
let offset = self.eat_literal_suffix().then_some(suffix_start);
633+
return (Int { base, empty_int: true }, offset);
631634
}
632635
}
633636
'x' => {
634637
base = Base::Hexadecimal;
635638
self.bump();
636639
if !self.eat_hexadecimal_digits() {
637640
let suffix_start = self.pos_within_token();
638-
self.eat_literal_suffix();
639-
return (Int { base, empty_int: true }, suffix_start);
641+
let offset = self.eat_literal_suffix().then_some(suffix_start);
642+
return (Int { base, empty_int: true }, offset);
640643
}
641644
}
642645
// Not a base prefix; consume additional digits.
@@ -650,8 +653,8 @@ impl Cursor<'_> {
650653
// Just a 0.
651654
_ => {
652655
let suffix_start = self.pos_within_token();
653-
self.eat_literal_suffix();
654-
return (Int { base, empty_int: false }, suffix_start);
656+
let offset = self.eat_literal_suffix().then_some(suffix_start);
657+
return (Int { base, empty_int: false }, offset);
655658
}
656659
}
657660
} else {
@@ -696,8 +699,8 @@ impl Cursor<'_> {
696699
_ => (),
697700
}
698701
}
699-
self.eat_literal_suffix();
700-
(Float { base, empty_exponent }, suffix_start)
702+
let offset = self.eat_literal_suffix().then_some(suffix_start);
703+
(Float { base, empty_exponent }, offset)
701704
}
702705
('e' | 'E', '_') => {
703706
// See above block for similar approach.
@@ -710,25 +713,27 @@ impl Cursor<'_> {
710713
self.eat_decimal_digits();
711714
let suffix_start = self.pos_within_token();
712715
self.eat_literal_suffix();
713-
(Float { base, empty_exponent: false }, suffix_start)
716+
let offset = self.eat_literal_suffix().then_some(suffix_start);
717+
(Float { base, empty_exponent: false }, offset)
714718
} else {
715719
// No digit means suffix, and therefore int
716720
self.eat_literal_suffix();
717-
(Int { base, empty_int: false }, non_exponent_suffix_start)
721+
let offset = self.eat_literal_suffix().then_some(non_exponent_suffix_start);
722+
(Int { base, empty_int: false }, offset)
718723
}
719724
}
720725
('e' | 'E', '0'..='9' | '+' | '-') => {
721726
// // Definitely an exponent (which still can be empty).
722727
self.bump();
723728
let empty_exponent = !self.eat_float_exponent();
724729
let suffix_start = self.pos_within_token();
725-
self.eat_literal_suffix();
726-
(Float { base, empty_exponent }, suffix_start)
730+
let offset = self.eat_literal_suffix().then_some(suffix_start);
731+
(Float { base, empty_exponent }, offset)
727732
}
728733
_ => {
729734
let suffix_start = self.pos_within_token();
730-
self.eat_literal_suffix();
731-
(Int { base, empty_int: false }, suffix_start)
735+
let offset = self.eat_literal_suffix().then_some(suffix_start);
736+
(Int { base, empty_int: false }, offset)
732737
}
733738
}
734739
}
@@ -1023,19 +1028,24 @@ impl Cursor<'_> {
10231028
self.eat_decimal_digits()
10241029
}
10251030

1026-
// Eats the suffix of the literal, e.g. "u8".
1027-
fn eat_literal_suffix(&mut self) {
1028-
self.eat_identifier();
1031+
/// Eats the suffix of the literal, e.g. "u8".
1032+
///
1033+
/// Returns `true` if anything was eaten.
1034+
fn eat_literal_suffix(&mut self) -> bool {
1035+
self.eat_identifier()
10291036
}
10301037

1031-
// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1032-
// identifier.
1033-
fn eat_identifier(&mut self) {
1038+
/// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1039+
/// identifier.
1040+
///
1041+
/// Returns `true` if anything was eaten.
1042+
fn eat_identifier(&mut self) -> bool {
10341043
if !is_id_start(self.first()) {
1035-
return;
1044+
return false;
10361045
}
10371046
self.bump();
10381047

10391048
self.eat_while(is_id_continue);
1049+
true
10401050
}
10411051
}

0 commit comments

Comments
 (0)