Skip to content

Commit 30c6d64

Browse files
committed
fix scala#4419: liftFun should also update method parameter types
WIP - tests/pos/pickleinf.scala still fails
1 parent 75b66c8 commit 30c6d64

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
193193
*/
194194
protected def liftFun(): Unit = ()
195195

196+
/** Whether `liftFun` is needed? It is the case if default arguments are used.
197+
*/
198+
protected def needLiftFun: Boolean =
199+
!isJavaAnnotConstr(methRef.symbol) &&
200+
args.size < reqiredArgNum(funType)
201+
196202
/** A flag signalling that the typechecking the application was so far successful */
197203
private[this] var _ok = true
198204

@@ -205,12 +211,27 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
205211
/** The function's type after widening and instantiating polytypes
206212
* with TypeParamRefs in constraint set
207213
*/
208-
val methType = funType.widen match {
214+
lazy val methType: Type = constrainType(liftedFunType)
215+
216+
def constrainType(tp: Type) = tp.widen match {
209217
case funType: MethodType => funType
210218
case funType: PolyType => constrained(funType).resultType
211219
case tp => tp //was: funType
212220
}
213221

222+
def reqiredArgNum(tp: Type): Int = tp.widen match {
223+
case funType: MethodType => funType.paramInfos.size
224+
case funType: PolyType => reqiredArgNum(funType.resultType)
225+
case tp => args.size
226+
}
227+
228+
def liftedFunType =
229+
if (needLiftFun) {
230+
liftFun()
231+
constrainType(normalizedFun.tpe)
232+
}
233+
else funType
234+
214235
/** The arguments re-ordered so that each named argument matches the
215236
* same-named formal parameter.
216237
*/
@@ -220,7 +241,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
220241
else
221242
args
222243

223-
protected def init() = methType match {
244+
protected def init() = methType.widen match {
224245
case methType: MethodType =>
225246
// apply the result type constraint, unless method type is dependent
226247
val resultApprox = resultTypeApprox(methType)
@@ -231,6 +252,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
231252
()
232253
else
233254
fail(err.typeMismatchMsg(methType.resultType, resultType))
255+
234256
// match all arguments with corresponding formal parameters
235257
matchArgs(orderedArgs, methType.paramInfos, 0)
236258
case _ =>
@@ -425,8 +447,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
425447
}
426448

427449
def tryDefault(n: Int, args1: List[Arg]): Unit = {
428-
if (!isJavaAnnotConstr(methRef.symbol))
429-
liftFun()
450+
// if (!isJavaAnnotConstr(methRef.symbol))
451+
// liftFun()
430452
val getter = findDefaultGetter(n + numArgs(normalizedFun))
431453
if (getter.isEmpty) missingArg(n)
432454
else {
@@ -543,7 +565,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
543565
private[this] var typedArgBuf = new mutable.ListBuffer[Tree]
544566
private[this] var liftedDefs: mutable.ListBuffer[Tree] = null
545567
private[this] var myNormalizedFun: Tree = fun
546-
init()
547568

548569
def addArg(arg: Tree, formal: Type): Unit =
549570
typedArgBuf += adapt(arg, formal.widenExpr)
@@ -598,6 +619,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
598619
private def sameSeq[T <: Trees.Tree[_]](xs: List[T], ys: List[T]): Boolean = firstDiff(xs, ys) < 0
599620

600621
val result = {
622+
init()
623+
601624
var typedArgs = typedArgBuf.toList
602625
def app0 = cpy.Apply(app)(normalizedFun, typedArgs) // needs to be a `def` because typedArgs can change later
603626
val app1 =
@@ -606,7 +629,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
606629
if (!sameSeq(args, orderedArgs.dropWhile(_ eq EmptyTree)) && !isJavaAnnotConstr(methRef.symbol)) {
607630
// need to lift arguments to maintain evaluation order in the
608631
// presence of argument reorderings.
609-
610632
liftFun()
611633

612634
// lift arguments in the definition order

tests/pos/i4419.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Foo(config: String) {
2+
case class Bar(val x: Int) {
3+
def doThings: String = config //Do whatever here
4+
}
5+
}
6+
7+
8+
object Test {
9+
def test(foo: Foo)(bar: foo.Bar = foo.Bar(5)) = ???
10+
11+
test(new Foo("port"))()
12+
}

0 commit comments

Comments
 (0)