Parser modification to handle negative integer edge cases #121

Merged
sb10q merged 1 commits from integer_range_fix into master 2021-12-03 16:35:58 +08:00
Collaborator

The original python parser parses things like -2147483648 to be

UnaryOp(op=USub(),operand=Constant(value=2147483648))

This will cause problem of Integer out of bound at edge cases when a signed integer is the smallest one in its range. E.g., i32 ranges from -2147483648 to 2147483647, when we have a i32 literal -2147483648, the current parser will cause the type system to try get the type of 2147483648, which is not in i32, and hence the error.

There might be some other ways to handle this edge case without touching the parser, but I personally think to modify the parser would be adequate if I did not miss anything. Thanks!

The original python parser parses things like `-2147483648` to be ``` UnaryOp(op=USub(),operand=Constant(value=2147483648)) ``` This will cause problem of `Integer out of bound` at edge cases when a signed integer is the smallest one in its range. E.g., `i32` ranges from `-2147483648` to `2147483647`, when we have a i32 literal `-2147483648`, the current parser will cause the type system to try get the type of `2147483648`, which is not in i32, and hence the error. There might be some other ways to handle this edge case without touching the parser, but I personally think to modify the parser would be adequate if I did not miss anything. Thanks!
ychenfo added 1 commit 2021-12-02 03:05:14 +08:00
ychenfo changed title from Modify the parser to handle negative integer edge cases to Parser modification to handle negative integer edge cases 2021-12-02 03:05:49 +08:00
Owner

Do you know why it was done that way in RustPython?

Do you know why it was done that way in RustPython?
Author
Collaborator

Do you know why it was done that way in RustPython?

I think it does this way just to be consistent with original python where int has no size restriction:

Python 3.9.6 (default, Jun 28 2021, 08:57:49) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> print(ast.dump(ast.parse("-1"), indent = 4))
Module(
    body=[
        Expr(
            value=UnaryOp(
                op=USub(),
                operand=Constant(value=1)))],
    type_ignores=[])
> Do you know why it was done that way in RustPython? I think it does this way just to be consistent with original python where `int` has no size restriction: ``` Python 3.9.6 (default, Jun 28 2021, 08:57:49) [GCC 10.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ast >>> print(ast.dump(ast.parse("-1"), indent = 4)) Module( body=[ Expr( value=UnaryOp( op=USub(), operand=Constant(value=1)))], type_ignores=[]) ```
sb10q requested review from pca006132 2021-12-03 12:00:17 +08:00
pca006132 approved these changes 2021-12-03 16:34:34 +08:00
sb10q merged commit 1f3aa48361 into master 2021-12-03 16:35:58 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#121
No description provided.