Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ object ProtoTypes {
/** Test compatibility after normalization in a fresh typerstate. */
def normalizedCompatible(tp: Type, pt: Type)(implicit ctx: Context) = ctx.typerState.test {
val normTp = normalize(tp, pt)
isCompatible(normTp, pt) || pt.isRef(defn.UnitClass) && normTp.isParameterless
isCompatible(normTp, pt) ||
pt.isRef(defn.UnitClass) && normTp.isParameterless ||
pt.isInstanceOf[ApplyingProto] && isCompatible(tp, pt)
// Note for the last line:
// Avoid widening of `tp` if the expected type is `ApplyingProto`, as
// default parameters require a TermRef to insert the default values,
// widening to MethodType will incorrectly disqualify a valid candidate.
//
// Check note in `SelectionProto.isMatchedBy` and tests/pos/i3352.scala
}

private def disregardProto(pt: Type)(implicit ctx: Context): Boolean = pt.dealias match {
Expand Down Expand Up @@ -101,7 +109,19 @@ object ProtoTypes {
val mbr = if (privateOK) tp1.member(name) else tp1.nonPrivateMember(name)
def qualifies(m: SingleDenotation) =
memberProto.isRef(defn.UnitClass) ||
compat.normalizedCompatible(m.info, memberProto)
compat.normalizedCompatible(m.info, memberProto) ||
memberProto.isInstanceOf[ApplyingProto] && compat.normalizedCompatible(m.namedType, memberProto)
// Note for the last line:
// If the expected type is an applying type and `m` refers to a method,
// `m.info` will get a method type, which loses the information about default
// parameters for methods. When the expected type is an application taking
// default values, compatibility check will fail due to mismatch in the number
// of parameters.
//
// To make `TestApplication` succeed, we need to try `m.namedType` here so
// that default parameters will be inserted.
//
// check tests/pos/i3352.scala
mbr match { // hasAltWith inlined for performance
case mbr: SingleDenotation => mbr.exists && qualifies(mbr)
case _ => mbr hasAltWith qualifies
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/i3352.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Test {
class Foo {
def bar(x: String): Int = 1
}

implicit class FooOps(foo: Foo) {
def bar(x: Int, y: Int = 2): Int = 2 // compiles with no default argument
}

def test(foo: Foo): Unit = {
foo.bar(1)
}
}