[red-knot] Avoid unresolved-reference in unreachable code (#17169)

## Summary

This PR changes the inferred type for symbols in unreachable sections of
code to `Never` (instead of reporting them as unbound), in order to
silence false positive diagnostics. See the lengthy comment in the code
for further details.

## Test Plan

- Updated Markdown tests.
- Manually verified a couple of ecosystem diagnostic changes.
This commit is contained in:
David Peter
2025-04-03 16:52:11 +02:00
committed by GitHub
parent a1eb834a5f
commit fedd982fd5
3 changed files with 48 additions and 8 deletions

View File

@@ -654,9 +654,7 @@ def f(cond: bool) -> str:
reveal_type(x) # revealed: Literal["before"]
return "a"
x = "after-return"
# TODO: no unresolved-reference error
# error: [unresolved-reference]
reveal_type(x) # revealed: Unknown
reveal_type(x) # revealed: Never
else:
x = "else"
return reveal_type(x) # revealed: Literal["else"]

View File

@@ -211,9 +211,6 @@ def f():
print("unreachable")
# TODO: we should not emit an error here; we currently do, since there is no control flow path from this
# use of 'x' to any definition of 'x'.
# error: [unresolved-reference]
print(x)
```
@@ -228,8 +225,6 @@ def outer():
x = 1
def inner():
# TODO: we should not emit an error here
# error: [unresolved-reference]
return x # Name `x` used when not defined
while True:
pass