]> git.lizzy.rs Git - rust.git/commitdiff
Inline and remove `validate_int_literal`.
authorNicholas Nethercote <n.nethercote@gmail.com>
Thu, 3 Nov 2022 23:02:29 +0000 (10:02 +1100)
committerNicholas Nethercote <n.nethercote@gmail.com>
Fri, 4 Nov 2022 03:24:41 +0000 (14:24 +1100)
It has a single callsite, and is fairly small. The `Float` match arm
already has base-specific checking inline, so this makes things more
consistent.

compiler/rustc_lexer/src/lib.rs
compiler/rustc_parse/src/lexer/mod.rs

index 51515976e4ee97371520f892b854ad076ba36fb4..0d29d7b1e3d9b04b3d926e4e75d4045a8d2026ab 100644 (file)
@@ -203,13 +203,13 @@ pub enum RawStrError {
 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
 pub enum Base {
     /// Literal starts with "0b".
-    Binary,
+    Binary = 2,
     /// Literal starts with "0o".
-    Octal,
-    /// Literal starts with "0x".
-    Hexadecimal,
+    Octal = 8,
     /// Literal doesn't contain a prefix.
-    Decimal,
+    Decimal = 10,
+    /// Literal starts with "0x".
+    Hexadecimal = 16,
 }
 
 /// `rustc` allows files to have a shebang, e.g. "#!/usr/bin/rustrun",
index 61b5be4240414a0a759d6cdfc33a8e2862e319af..9de0c74f4b1d2f28c654425e8f1c2802b71ddf54 100644 (file)
@@ -437,7 +437,19 @@ fn cook_lexer_literal(
                         .emit();
                     (token::Integer, sym::integer(0))
                 } else {
-                    self.validate_int_literal(base, start, end);
+                    if matches!(base, Base::Binary | Base::Octal) {
+                        let base = base as u32;
+                        let s = self.str_from_to(start + BytePos(2), end);
+                        for (idx, c) in s.char_indices() {
+                            if c != '_' && c.to_digit(base).is_none() {
+                                self.err_span_(
+                                    start + BytePos::from_usize(2 + idx),
+                                    start + BytePos::from_usize(2 + idx + c.len_utf8()),
+                                    &format!("invalid digit for a base {} literal", base),
+                                );
+                            }
+                        }
+                    }
                     (token::Integer, self.symbol_from_to(start, end))
                 }
             }
@@ -683,23 +695,6 @@ fn cook_quoted(
         });
         (kind, Symbol::intern(lit_content))
     }
-
-    fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {
-        let base = match base {
-            Base::Binary => 2,
-            Base::Octal => 8,
-            _ => return,
-        };
-        let s = self.str_from_to(content_start + BytePos(2), content_end);
-        for (idx, c) in s.char_indices() {
-            let idx = idx as u32;
-            if c != '_' && c.to_digit(base).is_none() {
-                let lo = content_start + BytePos(2 + idx);
-                let hi = content_start + BytePos(2 + idx + c.len_utf8() as u32);
-                self.err_span_(lo, hi, &format!("invalid digit for a base {} literal", base));
-            }
-        }
-    }
 }
 
 pub fn nfc_normalize(string: &str) -> Symbol {