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
6 changes: 6 additions & 0 deletions crates/starpls_hir/src/def/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,13 @@ impl ScopeCollector<'_> {
targets,
stmts,
} => {
// The iterable is evaluated before the loop variables are bound, so it must be
// collected in the enclosing scope. Allocating a fresh scope for the targets (as is
// done for assignments and comprehensions) prevents a name in the iterable from
// resolving to the loop variable itself, e.g. the inner `x` in `for x in x.foo:`,
// which would otherwise cause type inference to recurse indefinitely.
self.collect_expr(*iterable, *current, None);
*current = self.alloc_scope(*current);
targets.iter().copied().for_each(|expr| {
self.collect_expr(expr, *current, Some(*iterable));
});
Expand Down
12 changes: 12 additions & 0 deletions crates/starpls_hir/src/def/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ for x, y in 1, 2, 3:
)
}

#[test]
fn test_loop_variable_not_in_scope_for_iterable() {
check_scope(
r"
def f(items):
for x in items$0:
pass
",
&["f", "items"],
)
}

#[test]
fn test_lambda() {
check_scope(
Expand Down
1 change: 1 addition & 0 deletions crates/starpls_hir/src/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,7 @@ pub(crate) struct InferenceContext {
pub(crate) type_of_load_item: FxHashMap<FileLoadItemId, Ty>,
pub(crate) type_of_param: FxHashMap<FileParamId, Ty>,
pub(crate) source_assign_done: FxHashSet<FileExprId>,
pub(crate) source_assign_in_progress: FxHashSet<FileExprId>,
pub(crate) flow_node_type_cache: FxHashMap<CodeFlowCacheKey, Option<Ty>>,
pub(crate) definition_is_used: FxHashMap<InFile<Either<ExprId, StmtId>>, bool>,
}
Expand Down
10 changes: 10 additions & 0 deletions crates/starpls_hir/src/typeck/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,17 @@ impl TyContext<'_> {
if self.cx.source_assign_done.contains(&key) {
return;
}

// Guard against cycles, e.g. when inferring the type of a name resolves to an assignment
// whose source expression refers back to that same name. Re-entering here for a source
// expression that is still being processed would otherwise recurse until the stack
// overflows. Bailing out leaves the targets without a type, which callers treat as
// `Unknown`.
if !self.cx.source_assign_in_progress.insert(key) {
return;
}
self.infer_source_expr_assign_inner(file, source, expected_ty, execution_scope);
self.cx.source_assign_in_progress.remove(&key);
self.cx.source_assign_done.insert(key);
}

Expand Down
66 changes: 66 additions & 0 deletions crates/starpls_hir/src/typeck/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,72 @@ def f():
);
}

#[test]
fn test_for_iterable_shadowed_by_loop_variable() {
// The iterable of a `for` statement is evaluated before the loop variables are bound, so a
// name in the iterable must resolve to the enclosing definition rather than to the loop
// variable itself. Previously this caused infinite recursion during type inference.
check_infer(
r#"
def f(items):
for x in [1, 2, 3]:
for x in [x]:
x
"#,
expect![[r#"
23..24 "x": int
29..30 "1": Literal[1]
32..33 "2": Literal[2]
35..36 "3": Literal[3]
28..37 "[1, 2, 3]": list[int]
51..52 "x": int
57..58 "x": int
56..59 "[x]": list[int]
73..74 "x": int
"#]],
);
}

#[test]
fn test_for_iterable_shadowed_by_loop_variable_with_code_flow_analysis() {
check_infer_with_code_flow_analysis(
r#"
def f(items):
for x in [1, 2, 3]:
for x in [x]:
x
"#,
expect![[r#"
23..24 "x": int
29..30 "1": Literal[1]
32..33 "2": Literal[2]
35..36 "3": Literal[3]
28..37 "[1, 2, 3]": list[int]
51..52 "x": int
57..58 "x": int
56..59 "[x]": list[int]
73..74 "x": int
"#]],
);
}

#[test]
fn test_for_iterable_undefined_loop_variable() {
check_infer(
r#"
for x in x:
x
"#,
expect![[r#"
5..6 "x": Unknown
10..11 "x": Unbound
17..18 "x": Unknown

10..11 "x" is not defined
"#]],
);
}

#[test]
fn test_unused_definitions() {
check_infer_with_unused_definitions(
Expand Down