-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ty] Fix stack overflows when attempting to infer the truthiness of a TypeVar with an invalid recursive bound or constraints #21833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
crates/ty_python_semantic/resources/corpus/truthiness_test_on_recursive_bound_typevar.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class name_1[name_2: name_2[0]]: | ||
| def name_4(name_3: name_2, /): | ||
| if name_3: | ||
| pass |
4 changes: 4 additions & 0 deletions
4
...s/ty_python_semantic/resources/corpus/truthiness_test_on_recursive_constrained_typevar.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class name_1[name_2: (name_2[0], int)]: | ||
| def name_4(name_3: name_2, /): | ||
| if name_3: | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...pported_bool_con…_-_Different_ways_that_…_-_Constrained_TypeVar_…_(d3fd18045d730775).snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: unsupported_bool_conversion.md - Different ways that `unsupported-bool-conversion` can occur - Constrained TypeVar has >=1 constraint that doesn't support boolean conversion | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | class NotBoolable: | ||
| 2 | __bool__ = None | ||
| 3 | | ||
| 4 | def f[T: (int, NotBoolable)](x: T) -> T: | ||
| 5 | # error: [unsupported-bool-conversion] | ||
| 6 | if x: | ||
| 7 | pass | ||
| 8 | return x | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[unsupported-bool-conversion]: Boolean conversion is unsupported for type variable `T@f` because constraint `NotBoolable` doesn't implement `__bool__` correctly | ||
| --> src/mdtest_snippet.py:6:8 | ||
| | | ||
| 4 | def f[T: (int, NotBoolable)](x: T) -> T: | ||
| 5 | # error: [unsupported-bool-conversion] | ||
| 6 | if x: | ||
| | ^ | ||
| 7 | pass | ||
| 8 | return x | ||
| | | ||
| info: rule `unsupported-bool-conversion` is enabled by default | ||
|
|
||
| ``` |
40 changes: 40 additions & 0 deletions
40
...pported_bool_con…_-_Different_ways_that_…_-_TypeVar_has_an_upper…_(c47b930ed7da5310).snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: unsupported_bool_conversion.md - Different ways that `unsupported-bool-conversion` can occur - TypeVar has an upper bound that doesn't support boolean conversion | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | class NotBoolable: | ||
| 2 | __bool__ = None | ||
| 3 | | ||
| 4 | def f[T: NotBoolable](x: T) -> T: | ||
| 5 | # error: [unsupported-bool-conversion] | ||
| 6 | if x: | ||
| 7 | pass | ||
| 8 | return x | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[unsupported-bool-conversion]: Boolean conversion is unsupported for type variable `T@f` because upper bound `NotBoolable` doesn't implement `__bool__` correctly | ||
| --> src/mdtest_snippet.py:6:8 | ||
| | | ||
| 4 | def f[T: NotBoolable](x: T) -> T: | ||
| 5 | # error: [unsupported-bool-conversion] | ||
| 6 | if x: | ||
| | ^ | ||
| 7 | pass | ||
| 8 | return x | ||
| | | ||
| info: rule `unsupported-bool-conversion` is enabled by default | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I first naively tried doing this
but that's no good -- the
TypeVarConstraints::as_type()call itself causes a stack overflow if one of the TypeVar constraints is recursive. So I had to refactortry_unionso that it could take aUnionTypeor aTypeVar.