Skip to content

Commit 5dae5f2

Browse files
committed
Fix #2000: Make implicit and non-implicit functions incomparable with <:<
Implicit and non-implicit functions are incomparable with <:<, but are treated as equivalent with `matches`. This means implicit and non-implicit functions of the same types override each other, but RefChecks will give an error because their types are not subtypes. Also contains a test for #2002.
1 parent f467be6 commit 5dae5f2

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
489489
case tp1 @ MethodType(_, formals1) =>
490490
(tp1.signature consistentParams tp2.signature) &&
491491
matchingParams(formals1, formals2, tp1.isJava, tp2.isJava) &&
492-
(!tp1.isImplicit || tp2.isImplicit) && // non-implicit functions shadow implicit ones
492+
(tp1.isImplicit == tp2.isImplicit) &&
493493
isSubType(tp1.resultType, tp2.resultType.subst(tp2, tp1))
494494
case _ =>
495495
false

compiler/test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class tests extends CompilerTest {
196196
@Test def neg_nopredef = compileFile(negCustomArgs, "nopredef", List("-Yno-predef"))
197197
@Test def neg_noimports = compileFile(negCustomArgs, "noimports", List("-Yno-imports"))
198198
@Test def neg_noimpots2 = compileFile(negCustomArgs, "noimports2", List("-Yno-imports"))
199+
@Test def neg_i2002 = compileFile(negCustomArgs, "i2002", Nil)
199200

200201
@Test def run_all = runFiles(runDir)
201202

tests/neg/customArgs/i2002.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Test {
2+
def foo(i: Int): Int = i
3+
def foo(implicit i: Int): Int = i // error
4+
}

tests/neg/i2000.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
object test1 {
2+
class C[A] { def foo(a: A) = "c" }
3+
class D extends C[String] { override def foo(implicit s: String) = "d" } // error
4+
}
5+
6+
object test2 {
7+
class C[A] { final def foo(a: A) = "c" }
8+
class D extends C[String] { def foo(implicit s: String) = "d" } // error
9+
object Test {
10+
def main(args: Array[String]) =
11+
new D
12+
}
13+
}
14+
15+
object test3 {
16+
class A {
17+
def foo(implicit i: Int): Int = i + i
18+
}
19+
20+
class B extends A {
21+
override def foo(i: Int): Int = i // error
22+
}
23+
}

0 commit comments

Comments
 (0)