Repository navigation
Commit 96f8a99
committed
Fix narrowing of bool literal match subjects (#22202)
An expression such as `False` has type `bool` with a known value, stored as
`last_known_value`. When narrowing against a bool literal pattern,
`conditional_types()` expands the instance into `Literal[True] |
Literal[False]` so that one member can be removed, but the expansion dropped
the known value.
As a result, `match False: case False:` left `Literal[True]` as the remaining
type, the match was not considered exhaustive, and a following `case other`
captured `Literal[False]` even though the subject could only be `False`.
Keep the known value when expanding a sum type: if the instance already has a
last known value, expand to that literal instead of the full union. The
existing behaviour for plain `bool` and enum instances is unchanged.1 parent 033f958 commit 96f8a99
2 files changed
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1153 | 1153 | | |
1154 | 1154 | | |
1155 | 1155 | | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
1156 | 1162 | | |
1157 | 1163 | | |
1158 | 1164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
53 | 81 | | |
54 | 82 | | |
55 | 83 | | |
| |||
0 commit comments