Commit 17776df
authored
Track type variable dependencies to guide instantiation decisions (#16042)
We now keep track of reverse type variable dependencies in constraints.
E.g. if a constraint contains a clause like
A >: List[B]
We associate with `B` info that `A` depends co-variantly on it. Or, if
A <: B => C
we associate with `B` that `A` depends co-variantly on it and with `C`
that `A` depends contra-variantly on it. Here, co-variant means that
the allowable range of `A` narrows if the referred-to variable `B`
grows, and
contra-variant means that the allowable range of `A` narrows if the
referred-to
variable `C` shrinks. If there's an invariant reference such as
A <: Array[B]
Then `A` depends both co- and contra-variantly on `B`.
These dependencies are then used to guide type variable instantiation.
If an eligible type variable does not appear in the type of a typed
expression,
we interpolate it to one of its bounds. Previously this was done in an
ad-hoc
manner where we minimized the type variable if it had a lower bound and
maximized
it otherwise. We now take reverse dependencies into account. If
maximizing a type
variable would narrow the remaining constraint we minimize, and if
minimizing
a type variable would narrow the remaining constraint we maximize. Only
if
the type variable is not referred to from the remaining constraint we
resort
to the old heuristic based on the lower bound.
Fixes #15864
Todo: This could be generalized in several directions:
- We could base the dependency tracking on type param refs instead of
type variables.
That could make the `replace` operation in a constraint more efficient.
- We could base more interpolation decisions on dependencies. E.g. we
could
interpolate a type variable only if both the type of an expression and
the enclosing
constraint agree in which direction this should be done.File tree
11 files changed
+581
-158
lines changed- compiler
- src/dotty/tools/dotc
- config
- core
- printing
- typer
- test/dotty/tools/dotc
- transform
- tests/pos
11 files changed
+581
-158
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
| |||
184 | 189 | | |
185 | 190 | | |
186 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
187 | 195 | | |
188 | 196 | | |
189 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| 312 | + | |
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
131 | | - | |
| 132 | + | |
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| |||
165 | 166 | | |
166 | 167 | | |
167 | 168 | | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | 169 | | |
172 | 170 | | |
173 | 171 | | |
174 | 172 | | |
175 | 173 | | |
176 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
177 | 195 | | |
178 | 196 | | |
179 | 197 | | |
| |||
0 commit comments