Commit 89fa247
authored
Fix variance loophole for private vars (#18693)
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.File tree
7 files changed
+100
-23
lines changed- compiler/src/dotty/tools/dotc
- core
- typer
- library/src/scala/annotation/internal
- tests/neg
7 files changed
+100
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
993 | 993 | | |
994 | 994 | | |
995 | 995 | | |
| 996 | + | |
996 | 997 | | |
997 | 998 | | |
998 | 999 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
868 | 868 | | |
869 | 869 | | |
870 | 870 | | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
871 | 882 | | |
872 | 883 | | |
873 | 884 | | |
| |||
892 | 903 | | |
893 | 904 | | |
894 | 905 | | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | 906 | | |
908 | 907 | | |
909 | 908 | | |
| |||
937 | 936 | | |
938 | 937 | | |
939 | 938 | | |
940 | | - | |
| 939 | + | |
941 | 940 | | |
942 | 941 | | |
943 | 942 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1103 | 1103 | | |
1104 | 1104 | | |
1105 | 1105 | | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
1106 | 1118 | | |
1107 | 1119 | | |
1108 | 1120 | | |
| |||
1136 | 1148 | | |
1137 | 1149 | | |
1138 | 1150 | | |
1139 | | - | |
1140 | | - | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
1141 | 1155 | | |
1142 | 1156 | | |
1143 | | - | |
| 1157 | + | |
1144 | 1158 | | |
1145 | 1159 | | |
1146 | | - | |
1147 | | - | |
| 1160 | + | |
1148 | 1161 | | |
1149 | 1162 | | |
1150 | 1163 | | |
| |||
1157 | 1170 | | |
1158 | 1171 | | |
1159 | 1172 | | |
1160 | | - | |
1161 | 1173 | | |
1162 | 1174 | | |
1163 | 1175 | | |
| |||
Lines changed: 12 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
152 | | - | |
| 151 | + | |
| 152 | + | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
154 | 161 | | |
155 | | - | |
156 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
157 | 165 | | |
158 | 166 | | |
159 | 167 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 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 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments