Skip to content

Commit 9b5a23b

Browse files
Copilotnoti0na1
andcommitted
Fix issue #22310: Use strict type matching for override checks
Co-authored-by: noti0na1 <8036790+noti0na1@users.noreply.github.com>
1 parent 4246889 commit 9b5a23b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,11 @@ object RefChecks {
974974

975975
def isSignatureMatch(sym: Symbol) = sym.isType || {
976976
val self = clazz.thisType
977-
sym.asSeenFrom(self).matches(member.asSeenFrom(self))
977+
val symDenotation = sym.asSeenFrom(self)
978+
val memberDenotation = member.asSeenFrom(self)
979+
// Use matchesLoosely with alwaysCompareTypes = true to ensure strict type checking for overrides
980+
// This fixes issue #22310 where A0[String] was incorrectly considered to match A0[Object]
981+
symDenotation.matchesLoosely(memberDenotation, alwaysCompareTypes = true)
978982
&& !incompatibleRepeatedParam(sym, member)
979983
}
980984

tests/neg/i22310.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E177] Syntax Error: tests/neg/i22310.scala:9:2 --------------------------------
2+
9 | override def func(arg0: A0[Object], arg1: String): Unit = {} // error: overrides nothing
3+
| ^
4+
| override def func overrides nothing.
5+
-- [E177] Syntax Error: tests/neg/i22310.scala:16:2 -------------------------------
6+
16| override def method(x: B0[Object, Int], y: String, z: Int): Unit = {} // error: overrides nothing
7+
| ^
8+
| override def method overrides nothing.

tests/pos/i22310-valid.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test cases to ensure valid overrides still work after fixing #22310
2+
3+
abstract class ValidCase1[T] {
4+
def method(arg: ValidCase1[String], param: T): Unit
5+
}
6+
7+
abstract class ValidCase1Sub extends ValidCase1[String] {
8+
override def method(arg: ValidCase1[String], param: String): Unit = {} // OK - this should work
9+
}
10+
11+
// Variance cases should still work correctly
12+
abstract class CovariantCase[+T] {
13+
def method(param: CovariantCase[String]): T
14+
}
15+
16+
abstract class CovariantCaseSub extends CovariantCase[Object] {
17+
override def method(param: CovariantCase[String]): Object = "valid" // OK - covariance in return type
18+
}
19+
20+
// Test with concrete classes too
21+
class ConcreteBase[T] {
22+
def func(x: ConcreteBase[String], y: T): Unit = {}
23+
}
24+
25+
class ConcreteDerived extends ConcreteBase[String] {
26+
override def func(x: ConcreteBase[String], y: String): Unit = {} // OK - this should work
27+
}

0 commit comments

Comments
 (0)