fix(checker): leaking for-loop iterator target
All checks were successful
Tests / tests (pull_request) Successful in 6s

This commit is contained in:
2026-07-08 19:21:19 +02:00
parent 06f71f2945
commit ab7012c538
4 changed files with 138 additions and 3 deletions

View File

@@ -601,6 +601,10 @@ class PythonTyper(
pass
def visit_for_stmt(self, stmt: p.ForStmt) -> None:
outer_env: Environment = self.env
inner_env: Environment = Environment(self.env)
self.env = inner_env
item_type: Type = UnknownType()
iterator_type: Type = self.type_of(stmt.iterator)
if iterator_type != UnknownType():
@@ -614,8 +618,10 @@ class PythonTyper(
self._assign(stmt.location, stmt.target, item_type)
self.judge(stmt.target, item_type)
env: Environment = Environment(self.env)
body_returned: bool = self.process_block(stmt.body, env)
body_returned: bool = self.process_block(stmt.body, inner_env)
self.env = outer_env
if body_returned:
raise ReturnException()

View File

@@ -180,9 +180,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
pass
def visit_for_stmt(self, stmt: p.ForStmt) -> None:
self.begin_scope()
self.resolve(stmt.iterator)
self._visit_assign(stmt.target)
self.begin_scope()
self.resolve(*stmt.body)
self.end_scope()

View File

@@ -38,3 +38,8 @@ def fact(n: int) -> int:
if n <= 1:
return 1
return n * fact(n - 1)
for i in [1, 2, 3]:
_ = i
_ = i

View File

@@ -83,6 +83,20 @@
]
},
"message": "Unknown variable"
},
{
"type": "Warning",
"location": {
"start": [
45,
4
],
"end": [
45,
5
]
},
"message": "Unknown variable"
}
],
"judgments": [
@@ -522,6 +536,116 @@
"type": {
"name": "int"
}
},
{
"location": {
"from": "L43:10",
"to": "L43:11"
},
"expr": {
"_type": "LiteralExpr",
"value": 1
},
"type": {
"name": "int"
}
},
{
"location": {
"from": "L43:13",
"to": "L43:14"
},
"expr": {
"_type": "LiteralExpr",
"value": 2
},
"type": {
"name": "int"
}
},
{
"location": {
"from": "L43:16",
"to": "L43:17"
},
"expr": {
"_type": "LiteralExpr",
"value": 3
},
"type": {
"name": "int"
}
},
{
"location": {
"from": "L43:9",
"to": "L43:18"
},
"expr": {
"_type": "ListExpr",
"items": [
{
"_type": "LiteralExpr",
"value": 1
},
{
"_type": "LiteralExpr",
"value": 2
},
{
"_type": "LiteralExpr",
"value": 3
}
]
},
"type": {
"name": "list",
"args": [
{
"name": "int"
}
],
"body": {
"name": "list"
}
}
},
{
"location": {
"from": "L43:4",
"to": "L43:5"
},
"expr": {
"_type": "VariableExpr",
"name": "i"
},
"type": {
"name": "int"
}
},
{
"location": {
"from": "L44:8",
"to": "L44:9"
},
"expr": {
"_type": "VariableExpr",
"name": "i"
},
"type": {
"name": "int"
}
},
{
"location": {
"from": "L45:4",
"to": "L45:5"
},
"expr": {
"_type": "VariableExpr",
"name": "i"
},
"type": {}
}
]
}