Skip to content

Commit 0f4f5f9

Browse files
committed
Take parameter dependencies into account
Take parameter dependencies into account when typechecking arguments.
1 parent 473fe77 commit 0f4f5f9

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
178178
*/
179179
protected def normalizedFun: Tree
180180

181+
protected def typeOfArg(arg: Arg): Type
182+
181183
/** If constructing trees, pull out all parts of the function
182184
* which are not idempotent into separate prefix definitions
183185
*/
@@ -380,8 +382,16 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
380382
if (success) formals match {
381383
case formal :: formals1 =>
382384

383-
def addTyped(arg: Arg, formal: Type) =
385+
/** Add result of typing argument `arg` against parameter type `formal`.
386+
* @return A type transformation to apply to all arguments following this one.
387+
*/
388+
def addTyped(arg: Arg, formal: Type): Type => Type = {
384389
addArg(typedArg(arg, formal), formal)
390+
if (methodType.isParamDependent)
391+
_.substParam(MethodParam(methodType, n), typeOfArg(arg))
392+
else
393+
identity
394+
}
385395

386396
def missingArg(n: Int): Unit = {
387397
val pname = methodType.paramNames(n)
@@ -395,8 +405,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
395405
val getter = findDefaultGetter(n + numArgs(normalizedFun))
396406
if (getter.isEmpty) missingArg(n)
397407
else {
398-
addTyped(treeToArg(spliceMeth(getter withPos normalizedFun.pos, normalizedFun)), formal)
399-
matchArgs(args1, formals1, n + 1)
408+
val substParam = addTyped(
409+
treeToArg(spliceMeth(getter withPos normalizedFun.pos, normalizedFun)),
410+
formal)
411+
matchArgs(args1, formals1.mapconserve(substParam), n + 1)
400412
}
401413
}
402414

@@ -420,8 +432,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
420432
case EmptyTree :: args1 =>
421433
tryDefault(n, args1)
422434
case arg :: args1 =>
423-
addTyped(arg, formal)
424-
matchArgs(args1, formals1, n + 1)
435+
val substParam = addTyped(arg, formal)
436+
matchArgs(args1, formals1.mapconserve(substParam), n + 1)
425437
case nil =>
426438
tryDefault(n, args)
427439
}
@@ -477,6 +489,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
477489
def argType(arg: Tree, formal: Type): Type = normalize(arg.tpe, formal)
478490
def treeToArg(arg: Tree): Tree = arg
479491
def isVarArg(arg: Tree): Boolean = tpd.isWildcardStarArg(arg)
492+
def typeOfArg(arg: Tree): Type = arg.tpe
480493
def harmonizeArgs(args: List[Tree]) = harmonize(args)
481494
}
482495

@@ -494,6 +507,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
494507
def argType(arg: Type, formal: Type): Type = arg
495508
def treeToArg(arg: Tree): Type = arg.tpe
496509
def isVarArg(arg: Type): Boolean = arg.isRepeatedParam
510+
def typeOfArg(arg: Type): Type = arg
497511
def harmonizeArgs(args: List[Type]) = harmonizeTypes(args)
498512
}
499513

@@ -592,6 +606,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
592606
extends TypedApply(app, fun, methRef, proto.args, resultType) {
593607
def typedArg(arg: untpd.Tree, formal: Type): TypedArg = proto.typedArg(arg, formal.widenExpr)
594608
def treeToArg(arg: Tree): untpd.Tree = untpd.TypedSplice(arg)
609+
def typeOfArg(arg: untpd.Tree) = proto.typeOfArg(arg)
595610
}
596611

597612
/** Subclass of Application for type checking an Apply node with typed arguments. */
@@ -603,6 +618,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
603618
// not match the abstract method in Application and an abstract class error results.
604619
def typedArg(arg: tpd.Tree, formal: Type): TypedArg = arg
605620
def treeToArg(arg: Tree): Tree = arg
621+
def typeOfArg(arg: Tree) = arg.tpe
606622
}
607623

608624
/** If `app` is a `this(...)` constructor call, the this-call argument context,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ object ProtoTypes {
233233
typer.adapt(targ, formal, arg)
234234
}
235235

236+
/** The type of the argument `arg`.
237+
* @pre `arg` ahs been typed before
238+
*/
239+
def typeOfArg(arg: untpd.Tree)(implicit ctx: Context): Type =
240+
myTypedArg(arg).tpe
241+
236242
private var myTupled: Type = NoType
237243

238244
/** The same proto-type but with all arguments combined in a single tuple */

0 commit comments

Comments
 (0)