Skip to content

Commit 67ae357

Browse files
Instantiate type vars which occur in arguments applications
1 parent 468ca6e commit 67ae357

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,10 @@ object Inferencing {
392392
* - The result expression `e` of a block `{s1; .. sn; e}`.
393393
*/
394394
def tvarsInParams(tree: Tree, locked: TypeVars)(using Context): List[TypeVar] = {
395-
@tailrec def boundVars(tree: Tree, acc: List[TypeVar]): List[TypeVar] = tree match {
396-
case Apply(fn, _) => boundVars(fn, acc)
395+
def boundVars(tree: Tree, acc: List[TypeVar]): List[TypeVar] = tree match {
396+
case Apply(fn, args) =>
397+
val argTpVars = args.flatMap(boundVars(_, Nil))
398+
boundVars(fn, acc ++ argTpVars)
397399
case TypeApply(fn, targs) =>
398400
val tvars = targs.filter(_.isInstanceOf[InferredTypeTree]).tpes.collect {
399401
case tvar: TypeVar
@@ -406,16 +408,18 @@ object Inferencing {
406408
case Block(_, expr) => boundVars(expr, acc)
407409
case _ => acc
408410
}
409-
@tailrec def occurring(tree: Tree, toTest: List[TypeVar], acc: List[TypeVar]): List[TypeVar] =
411+
def occurring(tree: Tree, toTest: List[TypeVar], acc: List[TypeVar]): List[TypeVar] =
410412
if (toTest.isEmpty) acc
411413
else tree match {
412-
case Apply(fn, _) =>
414+
case Apply(fn, args) =>
415+
val argsOcc = args.flatMap(occurring(_, toTest, Nil))
416+
val argsNocc = toTest.filterNot(argsOcc.contains)
413417
fn.tpe.widen match {
414418
case mtp: MethodType =>
415-
val (occ, nocc) = toTest.partition(tvar => mtp.paramInfos.exists(tvar.occursIn))
416-
occurring(fn, nocc, occ ::: acc)
419+
val (occ, nocc) = argsNocc.partition(tvar => mtp.paramInfos.exists(tvar.occursIn))
420+
occurring(fn, nocc, occ ::: argsOcc ::: acc)
417421
case _ =>
418-
occurring(fn, toTest, acc)
422+
occurring(fn, argsNocc, argsOcc ::: acc)
419423
}
420424
case TypeApply(fn, targs) => occurring(fn, toTest, acc)
421425
case Select(pre, _) => occurring(pre, toTest, acc)

tests/pos/i18578.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
trait Animal
3+
class Dog extends Animal
4+
5+
trait Ev[T]
6+
7+
given Ev[Dog] = ???
8+
given Ev[Animal] = ???
9+
given[T: Ev]: Ev[Set[T]] = ???
10+
11+
def f[T: Ev](v: T): Unit = ???
12+
13+
def main =
14+
val s = Set(new Dog)
15+
// f(s) // WORKS
16+
f(Set(new Dog)) // FAILS
17+
// instantiates to

0 commit comments

Comments
 (0)