Skip to content

Commit 79d5952

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

File tree

1 file changed

+33
-41
lines changed
  • compiler/rustc_lexer/src

1 file changed

+33
-41
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ impl Cursor<'_> {
410410
// Numeric literal.
411411
c @ '0'..='9' => {
412412
let (literal_kind, suffix_start) = self.number(c);
413+
let suffix_start = suffix_start.unwrap_or(self.pos_within_token());
414+
self.eat_literal_suffix();
413415
TokenKind::Literal { kind: literal_kind, suffix_start }
414416
}
415417

@@ -604,9 +606,9 @@ impl Cursor<'_> {
604606
}
605607
}
606608

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) {
609+
/// Parses a number and in `.1` returns the offset of the literal suffix if
610+
/// different from the current position on return.
611+
fn number(&mut self, first_digit: char) -> (LiteralKind, Option<u32>) {
610612
debug_assert!('0' <= self.prev() && self.prev() <= '9');
611613
let mut base = Base::Decimal;
612614
if first_digit == '0' {
@@ -616,27 +618,21 @@ impl Cursor<'_> {
616618
base = Base::Binary;
617619
self.bump();
618620
if !self.eat_decimal_digits() {
619-
let suffix_start = self.pos_within_token();
620-
self.eat_literal_suffix();
621-
return (Int { base, empty_int: true }, suffix_start);
621+
return (Int { base, empty_int: true }, None);
622622
}
623623
}
624624
'o' => {
625625
base = Base::Octal;
626626
self.bump();
627627
if !self.eat_decimal_digits() {
628-
let suffix_start = self.pos_within_token();
629-
self.eat_literal_suffix();
630-
return (Int { base, empty_int: true }, suffix_start);
628+
return (Int { base, empty_int: true }, None);
631629
}
632630
}
633631
'x' => {
634632
base = Base::Hexadecimal;
635633
self.bump();
636634
if !self.eat_hexadecimal_digits() {
637-
let suffix_start = self.pos_within_token();
638-
self.eat_literal_suffix();
639-
return (Int { base, empty_int: true }, suffix_start);
635+
return (Int { base, empty_int: true }, None);
640636
}
641637
}
642638
// Not a base prefix; consume additional digits.
@@ -649,9 +645,7 @@ impl Cursor<'_> {
649645

650646
// Just a 0.
651647
_ => {
652-
let suffix_start = self.pos_within_token();
653-
self.eat_literal_suffix();
654-
return (Int { base, empty_int: false }, suffix_start);
648+
return (Int { base, empty_int: false }, None);
655649
}
656650
}
657651
} else {
@@ -668,11 +662,11 @@ impl Cursor<'_> {
668662
// with a number
669663
self.bump();
670664
let mut empty_exponent = false;
671-
let mut suffix_start = self.pos_within_token();
665+
let mut suffix_start = None;
672666
if self.first().is_ascii_digit() {
673667
self.eat_decimal_digits();
674668
// This will be the start of the suffix if there is no exponent
675-
suffix_start = self.pos_within_token();
669+
suffix_start = Some(self.pos_within_token());
676670
match (self.first(), self.second()) {
677671
('e' | 'E', '_') => {
678672
// check if series of `_` is ended by a digit. If yes
@@ -682,21 +676,22 @@ impl Cursor<'_> {
682676
while matches!(self.first(), '_') {
683677
self.bump();
684678
}
679+
// If we find a digit, then the exponential was valid
680+
// so the suffix will start at the cursor as usual.
685681
if self.first().is_ascii_digit() {
686682
self.eat_decimal_digits();
687-
suffix_start = self.pos_within_token();
683+
suffix_start = None;
688684
}
689685
}
690686
('e' | 'E', '0'..='9' | '+' | '-') => {
691687
// Definitely an exponent (which still can be empty).
692688
self.bump();
693689
empty_exponent = !self.eat_float_exponent();
694-
suffix_start = self.pos_within_token();
690+
suffix_start = None;
695691
}
696692
_ => (),
697693
}
698694
}
699-
self.eat_literal_suffix();
700695
(Float { base, empty_exponent }, suffix_start)
701696
}
702697
('e' | 'E', '_') => {
@@ -708,28 +703,20 @@ impl Cursor<'_> {
708703
}
709704
if self.first().is_ascii_digit() {
710705
self.eat_decimal_digits();
711-
let suffix_start = self.pos_within_token();
712-
self.eat_literal_suffix();
713-
(Float { base, empty_exponent: false }, suffix_start)
706+
(Float { base, empty_exponent: false }, None)
714707
} else {
715-
// No digit means suffix, and therefore int
716-
self.eat_literal_suffix();
717-
(Int { base, empty_int: false }, non_exponent_suffix_start)
708+
// No digit means the suffix begins at `e`, meaning the number
709+
// is an integer.
710+
(Int { base, empty_int: false }, Some(non_exponent_suffix_start))
718711
}
719712
}
720713
('e' | 'E', '0'..='9' | '+' | '-') => {
721714
// // Definitely an exponent (which still can be empty).
722715
self.bump();
723716
let empty_exponent = !self.eat_float_exponent();
724-
let suffix_start = self.pos_within_token();
725-
self.eat_literal_suffix();
726-
(Float { base, empty_exponent }, suffix_start)
727-
}
728-
_ => {
729-
let suffix_start = self.pos_within_token();
730-
self.eat_literal_suffix();
731-
(Int { base, empty_int: false }, suffix_start)
717+
(Float { base, empty_exponent }, None)
732718
}
719+
_ => (Int { base, empty_int: false }, None),
733720
}
734721
}
735722

@@ -1023,19 +1010,24 @@ impl Cursor<'_> {
10231010
self.eat_decimal_digits()
10241011
}
10251012

1026-
// Eats the suffix of the literal, e.g. "u8".
1027-
fn eat_literal_suffix(&mut self) {
1028-
self.eat_identifier();
1013+
/// Eats the suffix of the literal, e.g. "u8".
1014+
///
1015+
/// Returns `true` if anything was eaten.
1016+
fn eat_literal_suffix(&mut self) -> bool {
1017+
self.eat_identifier()
10291018
}
10301019

1031-
// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1032-
// identifier.
1033-
fn eat_identifier(&mut self) {
1020+
/// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1021+
/// identifier.
1022+
///
1023+
/// Returns `true` if anything was eaten.
1024+
fn eat_identifier(&mut self) -> bool {
10341025
if !is_id_start(self.first()) {
1035-
return;
1026+
return false;
10361027
}
10371028
self.bump();
10381029

10391030
self.eat_while(is_id_continue);
1031+
true
10401032
}
10411033
}

0 commit comments

Comments
 (0)