Commit 3cc86de
committed
Fix variance loophole for private vars
In Scala 2 a setter was created at Typer for private, non-local vars. Variance
checking then worked on the setter. But in Scala 3, the setter is only created
later, which caused a loophole for variance checking.
This PR does actually better than Scala 2 in the following sense: A private variable
counts as an invariant occurrence only if it is assigned with a selector different
from `this`. Or conversely, a variable containing a covariant type parameter in its
type can be read from different objects, but all assignments must be via this. The
motivation is that such variables effectively behave like vals for the purposes
of variance checking.1 parent 48bb59c commit 3cc86de
File tree
5 files changed
+71
-20
lines changed- compiler/src/dotty/tools/dotc
- core
- transform
- typer
- tests/neg
5 files changed
+71
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
864 | 864 | | |
865 | 865 | | |
866 | 866 | | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
867 | 878 | | |
868 | 879 | | |
869 | 880 | | |
| |||
888 | 899 | | |
889 | 900 | | |
890 | 901 | | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | 902 | | |
904 | 903 | | |
905 | 904 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
112 | 119 | | |
113 | 120 | | |
114 | 121 | | |
| |||
149 | 156 | | |
150 | 157 | | |
151 | 158 | | |
| 159 | + | |
| 160 | + | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
| |||
257 | 266 | | |
258 | 267 | | |
259 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
260 | 277 | | |
261 | 278 | | |
262 | 279 | | |
| |||
395 | 412 | | |
396 | 413 | | |
397 | 414 | | |
398 | | - | |
399 | 415 | | |
400 | 416 | | |
401 | 417 | | |
| |||
472 | 488 | | |
473 | 489 | | |
474 | 490 | | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
475 | 494 | | |
476 | 495 | | |
477 | 496 | | |
| |||
Lines changed: 14 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
| 22 | + | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
152 | | - | |
| 151 | + | |
| 152 | + | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
154 | 160 | | |
155 | | - | |
156 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
157 | 164 | | |
158 | 165 | | |
159 | 166 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
0 commit comments