mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 21:28:21 +00:00
Include radix base prefix in large number representation (#7700)
## Summary When lexing a number like `0x995DC9BBDF1939FA` that exceeds our small number representation, we were only storing the portion after the base (in this case, `995DC9BBDF1939FA`). When using that representation in code generation, this could lead to invalid syntax, since `995DC9BBDF1939FA)` on its own is not a valid integer. This PR modifies the code to store the full span, including the radix prefix. See: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739802958. ## Test Plan `cargo test`
This commit is contained in:
parent
316f75987d
commit
f45281345d
3 changed files with 25 additions and 7 deletions
|
@ -40,16 +40,23 @@ impl Int {
|
|||
Self(Number::Big(value.into()))
|
||||
}
|
||||
|
||||
/// Parse an [`Int`] from a string with a given radix.
|
||||
pub fn from_str_radix(s: &str, radix: u32) -> Result<Self, std::num::ParseIntError> {
|
||||
match i64::from_str_radix(s, radix) {
|
||||
/// Parse an [`Int`] from a string with a given radix, like `0x95D`.
|
||||
///
|
||||
/// Takes, as input, the numerical portion (`95D`), the parsed base (`16`), and the entire
|
||||
/// token (`0x95D`).
|
||||
pub fn from_str_radix(
|
||||
number: &str,
|
||||
radix: u32,
|
||||
token: &str,
|
||||
) -> Result<Self, std::num::ParseIntError> {
|
||||
match i64::from_str_radix(number, radix) {
|
||||
Ok(value) => Ok(Int::small(value)),
|
||||
Err(err) => {
|
||||
if matches!(
|
||||
err.kind(),
|
||||
std::num::IntErrorKind::PosOverflow | std::num::IntErrorKind::NegOverflow
|
||||
) {
|
||||
Ok(Int::big(s))
|
||||
Ok(Int::big(token))
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue