Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions conformance/third_party/conformance.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7700,8 +7700,8 @@
{
"code": -2,
"column": 5,
"concise_description": "Augmented assignment produces a value of type `int`, which is not assignable to `Literal[3, 4, 5]`",
"description": "Augmented assignment produces a value of type `int`, which is not assignable to `Literal[3, 4, 5]`",
"concise_description": "Augmented assignment produces a value of type `Literal[6, 7, 8]`, which is not assignable to `Literal[3, 4, 5]`",
"description": "Augmented assignment produces a value of type `Literal[6, 7, 8]`, which is not assignable to `Literal[3, 4, 5]`",
"line": 33,
"name": "bad-assignment",
"severity": "error",
Expand Down
17 changes: 17 additions & 0 deletions crates/pyrefly_types/src/lit_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ impl LitInt {
}
}

fn to_bigint(&self) -> BigInt {
match &self.0 {
LitIntInner::Small(x) => BigInt::from(*x),
LitIntInner::Big(x) => (**x).clone(),
}
}

pub fn add(&self, other: &Self) -> Self {
match (&self.0, &other.0) {
(LitIntInner::Small(x), LitIntInner::Small(y)) => match x.checked_add(*y) {
Some(sum) => Self::new(sum),
None => Self::new_big(BigInt::from(*x) + BigInt::from(*y)),
},
_ => Self::new_big(self.to_bigint() + other.to_bigint()),
}
}

pub fn negate(&self) -> Self {
match &self.0 {
LitIntInner::Small(x) => match x.checked_neg() {
Expand Down
10 changes: 10 additions & 0 deletions pyrefly/lib/alt/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
&& let Some(r) = self.untype_opt(rhs.clone(), x.right.range())
{
Type::type_form(self.union(l, r))
} else if x.op == Operator::Add
&& let Type::Literal(Lit::Int(l)) = lhs
&& let Type::Literal(Lit::Int(r)) = rhs
{
Type::Literal(Lit::Int(l.add(r)))
} else if x.op == Operator::Add
&& ((*lhs == Type::LiteralString && rhs.is_literal_string())
|| (*rhs == Type::LiteralString && lhs.is_literal_string()))
Expand Down Expand Up @@ -324,6 +329,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
&& rhs.is_literal_string()
{
Type::LiteralString
} else if x.op == Operator::Add
&& let Type::Literal(Lit::Int(l)) = lhs
&& let Type::Literal(Lit::Int(r)) = rhs
{
Type::Literal(Lit::Int(l.add(r)))
} else if x.op == Operator::Add
&& let Type::Tuple(ref l) = base
&& let Type::Tuple(r) = rhs
Expand Down
2 changes: 1 addition & 1 deletion pyrefly/lib/test/lsp/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ ff
4 | ff
^
Completion Results:
- (Variable) fff: int
- (Variable) fff: Literal[1, 2]
"#
.trim(),
report.trim(),
Expand Down
2 changes: 1 addition & 1 deletion pyrefly/lib/test/lsp/hover_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ xy = xy + 1
# main.py
3 | xy = xy + 1
^
Hover Result: `int`
Hover Result: `Literal[6]`
"#
.trim(),
report.trim(),
Expand Down
19 changes: 19 additions & 0 deletions pyrefly/lib/test/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ def f(a: int, b: int) -> None:
"#,
);

testcase!(
test_literal_int_add,
r#"
from typing import Literal, assert_type

a: Literal[1] = 1
b: Literal[2] = 2
c = a + b
assert_type(c, Literal[3])
d: Literal[3] = c

e = a + 5
assert_type(e, Literal[6])

f = 5 + b
assert_type(f, Literal[7])
"#,
);

testcase!(
test_bounded_type_var_comparison,
r#"
Expand Down