Skip to content

Commit 0e0dab3

Browse files
authored
Merge pull request #4428 from dotty-staging/fix-4419
Fix #4419: liftFun should also update method parameter types
2 parents 4e5cf82 + abf7924 commit 0e0dab3

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

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

Lines changed: 21 additions & 3 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,25 @@ 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 = liftedFunType.widen match {
209215
case funType: MethodType => funType
210216
case funType: PolyType => constrained(funType).resultType
211217
case tp => tp //was: funType
212218
}
213219

220+
def reqiredArgNum(tp: Type): Int = tp.widen match {
221+
case funType: MethodType => funType.paramInfos.size
222+
case funType: PolyType => reqiredArgNum(funType.resultType)
223+
case tp => args.size
224+
}
225+
226+
lazy val liftedFunType =
227+
if (needLiftFun) {
228+
liftFun()
229+
normalizedFun.tpe
230+
}
231+
else funType
232+
214233
/** The arguments re-ordered so that each named argument matches the
215234
* same-named formal parameter.
216235
*/
@@ -231,6 +250,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
231250
()
232251
else
233252
fail(err.typeMismatchMsg(methType.resultType, resultType))
253+
234254
// match all arguments with corresponding formal parameters
235255
matchArgs(orderedArgs, methType.paramInfos, 0)
236256
case _ =>
@@ -425,8 +445,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
425445
}
426446

427447
def tryDefault(n: Int, args1: List[Arg]): Unit = {
428-
if (!isJavaAnnotConstr(methRef.symbol))
429-
liftFun()
430448
val getter = findDefaultGetter(n + numArgs(normalizedFun))
431449
if (getter.isEmpty) missingArg(n)
432450
else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ abstract class Lifter {
4646
if (noLift(expr)) expr
4747
else {
4848
val name = UniqueName.fresh(prefix)
49-
var liftedType = fullyDefinedType(expr.tpe.widen, "lifted expression", expr.pos)
49+
// don't instantiate here, as the type params could be further constrained, see tests/pos/pickleinf.scala
50+
var liftedType = expr.tpe.widen
5051
if (liftedFlags.is(Method)) liftedType = ExprType(liftedType)
5152
val lifted = ctx.newSymbol(ctx.owner, name, liftedFlags, liftedType, coord = positionCoord(expr.pos))
5253
defs += liftedDef(lifted, expr).withPos(expr.pos)

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)