Fix unrelated negation edge case

This commit is contained in:
Brandt Bucher 2025-05-26 20:26:23 -07:00 committed by Alex Waygood
parent 14aa8bb871
commit 0006df9292
2 changed files with 11 additions and 1 deletions

View File

@ -39,6 +39,13 @@ def both(x: int):
reveal_type(x // x) # revealed: int reveal_type(x // x) # revealed: int
reveal_type(x / x) # revealed: int | float reveal_type(x / x) # revealed: int | float
reveal_type(x % x) # revealed: int reveal_type(x % x) # revealed: int
# Edge case where negation leads to overflow:
i64_max = 9223372036854775807
i64_min = -i64_max - 1
reveal_type(i64_max) # revealed: Literal[9223372036854775807]
reveal_type(i64_min) # revealed: Literal[-9223372036854775808]
reveal_type(-i64_min) # revealed: int
``` ```
## Power ## Power

View File

@ -6349,7 +6349,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
(_, Type::Never) => Type::Never, (_, Type::Never) => Type::Never,
(ast::UnaryOp::UAdd, Type::IntLiteral(value)) => Type::IntLiteral(value), (ast::UnaryOp::UAdd, Type::IntLiteral(value)) => Type::IntLiteral(value),
(ast::UnaryOp::USub, Type::IntLiteral(value)) => Type::IntLiteral(-value), (ast::UnaryOp::USub, Type::IntLiteral(value)) => value
.checked_neg()
.map(Type::IntLiteral)
.unwrap_or_else(|| KnownClass::Int.to_instance(self.db())),
(ast::UnaryOp::Invert, Type::IntLiteral(value)) => Type::IntLiteral(!value), (ast::UnaryOp::Invert, Type::IntLiteral(value)) => Type::IntLiteral(!value),
(ast::UnaryOp::UAdd, Type::BooleanLiteral(bool)) => Type::IntLiteral(i64::from(bool)), (ast::UnaryOp::UAdd, Type::BooleanLiteral(bool)) => Type::IntLiteral(i64::from(bool)),