Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit c4973de

Browse files
jasontattonfacebook-github-bot
authored andcommitted
Add support for long integers
Summary: Here we improve the following: * The test case for long integers (big integers) * We have to use an unusual workaround to detect integer overflow of the `parse::<isize>` function as Meta is on a version of rust < 2022. * The workaround is documented and there is a todo as follows: ``` // TODO: use ParseIntError.kind() to detect integer overflow of // parse of const_value when Meta is on rust 2022. // In rust 2021 ParseIntError.kind is private // For now, store an overflow Err from parsing a large integer // Adapted from rust-lang/rust#22639 // and uutils/coreutils#2882 ``` This would seem to be the recommended workaround for detecting integer overflow. Reviewed By: stroxler Differential Revision: D42407915 Privacy Context Container: L1152058 fbshipit-source-id: 049dd6c74305fe193131fd1ea2ad72a0965d253a
1 parent da445b3 commit c4973de

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/cst_to_ast.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// https://github.com/tree-sitter/tree-sitter-python/blob/master/grammar.js
88

99
use std::collections::HashMap;
10+
use std::num::ParseIntError;
1011

1112
use ast::Alias;
1213
use ast::Arg;
@@ -74,6 +75,7 @@ pub struct Parser {
7475
// contingent on if we are on lhs or rhs of assignment or del expression
7576
current_expr_ctx: Vec<Option<ExprContext>>,
7677
increment_expression_column_offset: isize,
78+
integer_overflow_error: ParseIntError,
7779
}
7880

7981
///
@@ -229,6 +231,7 @@ impl Parser {
229231
ast_and_metadata: ASTAndMetaData::new(),
230232
current_expr_ctx: Vec::new(),
231233
increment_expression_column_offset: 0,
234+
integer_overflow_error: "184467440737095516150".parse::<isize>().err().unwrap(),
232235
}
233236
}
234237

@@ -3436,6 +3439,18 @@ impl Parser {
34363439

34373440
let integer_value = match const_value.parse::<isize>() {
34383441
Ok(value) => value,
3442+
Err(ref e) if *e == self.integer_overflow_error => {
3443+
// TODO: use ParseIntError.kind() to detect integer overflow of
3444+
// parse of const_value when Meta is on rust 2022.
3445+
// In rust 2021 ParseIntError.kind is private
3446+
// For now, store an overflow Err from parsing a large integer
3447+
// Adapted from https://github.com/rust-lang/rust/issues/22639
3448+
// and https://github.com/uutils/coreutils/pull/2882/
3449+
return Ok(ExprDesc::Constant {
3450+
value: Some(ConstantDesc::Num(Num::BigInt(const_value))),
3451+
kind: None,
3452+
});
3453+
}
34393454
Err(error_msg) => {
34403455
return Err(self.record_recoverable_error(
34413456
RecoverableError::UnexpectedExpression(format!(

tests/test_resources/unit_tests/ast_literals.pytest

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ f"{{{55}}}"
231231
f"{{{{70 + 4}}}}"
232232

233233
## long integers
234-
timestamp = 9223372036854775810
234+
timestamp1 = 9223372036854775810
235+
timestamp2 = -9223372036854775810
235236

236237
## complex numbers
237238
complex_half = 0.0302988j

tests/test_resources/unit_tests/ast_literals.pytest.expect_fails

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)