Skip to content

Commit 0315aad

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Fix bug caused by accidentally pinning a var in tuple_infer
Summary: Fixes #1535. Reviewed By: stroxler Differential Revision: D86928878 fbshipit-source-id: 51ce5a6c03ecc872333f0ecd0f4a557ca0cea363
1 parent 30c8115 commit 0315aad

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

pyrefly/lib/alt/expr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,14 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
657657
}
658658
if !nontuples.is_empty() {
659659
// The non-tuple options may contain a type like Sequence[T] that provides an additional default hint.
660-
let nontuple_hint = self.unions(nontuples.into_iter().cloned().collect());
660+
// Filter out top-level Vars: they don't provide any hints, and we don't want to pin them.
661+
let nontuple_hint = self.unions(
662+
nontuples
663+
.into_iter()
664+
.filter(|t| !matches!(t, Type::Var(_)))
665+
.cloned()
666+
.collect(),
667+
);
661668
let nontuple_element_hint =
662669
self.decompose_tuple(HintRef::new(&nontuple_hint, hint.errors()));
663670
if let Some(nontuple_element_hint) = nontuple_element_hint {

pyrefly/lib/alt/unwrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::types::types::Var;
2323
// Soft type hints are used for `e1 or e1` expressions.
2424
pub struct Hint<'a>(Type, Option<&'a ErrorCollector>);
2525

26-
#[derive(Clone, Copy)]
26+
#[derive(Clone, Copy, Debug)]
2727
pub struct HintRef<'a, 'b>(&'b Type, Option<&'a ErrorCollector>);
2828

2929
impl<'a> Hint<'a> {

pyrefly/lib/test/generic_basic.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,13 @@ def f[T](x: T | None = None) -> tuple[T, T | int]: ...
434434
assert_type(f(), tuple[Any, int])
435435
"#,
436436
);
437+
438+
testcase!(
439+
test_pass_tuple_literal_through_identity_function,
440+
r#"
441+
def f[T](x: T) -> T:
442+
return x
443+
def g() -> int:
444+
return f((1, "hello world"))[0]
445+
"#,
446+
);

pyrefly/lib/test/natural.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,12 @@ assert_type(C(42), C[int])
124124
);
125125

126126
testcase!(
127-
bug = "Broken literal promotion for tuples, something happening during contextual typing against T",
128127
test_generic_tuple,
129128
r#"
130129
from typing import assert_type
131130
132131
def f[T](x: T) -> list[T]: ...
133132
134-
assert_type(f((1,)), list[tuple[int]]) # E: assert_type(list[tuple[Literal[1], ...]], list[tuple[int]]) failed
133+
assert_type(f((1,)), list[tuple[int]])
135134
"#,
136135
);

0 commit comments

Comments
 (0)