Skip to content

Commit 532c287

Browse files
committed
fix: Only implement a deferred given in a class if its parent won't
implement it
1 parent 12d50a2 commit 532c287

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30383038
body
30393039

30403040
/** Implement givens that were declared with a `deferred` rhs.
3041-
* The a given value matching the declared type is searched in a
3041+
* The given value matching the declared type is searched in a
30423042
* context directly enclosing the current class, in which all given
30433043
* parameters of the current class are also defined.
30443044
*/
@@ -3055,6 +3055,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30553055
false
30563056
else true
30573057

3058+
def willBeimplementedInParentClass(m: TermRef) =
3059+
val superCls = cls.superClass
3060+
superCls.exists && superCls.asClass.baseClasses.contains(m.symbol.owner)
3061+
30583062
def givenImpl(mbr: TermRef): ValDef =
30593063
val dcl = mbr.symbol
30603064
val target = dcl.info.asSeenFrom(cls.thisType, dcl.owner)
@@ -3084,6 +3088,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30843088
cls.thisType.implicitMembers
30853089
//.showing(i"impl def givens for $cls/$result")
30863090
.filter(_.symbol.isAllOf(DeferredGivenFlags, butNot = Param))
3091+
.filter(!willBeimplementedInParentClass(_)) // only implement the given in the topmost class
30873092
//.showing(i"impl def filtered givens for $cls/$result")
30883093
.filter(isGivenValue)
30893094
.map(givenImpl)

Diff for: tests/pos/i21189-alt.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//> using options -source:future -language:experimental.modularity
2+
3+
class MySortedSet[T : Ord] extends SortedSet[T]
4+
5+
trait Ord[T]
6+
7+
trait Sorted[T] extends ParentOfSorted[T]
8+
9+
trait ParentOfSorted[T]:
10+
given Ord[T] as ord = compiletime.deferred
11+
12+
class SortedSet[T : Ord] extends Sorted[T]

Diff for: tests/pos/i21189.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//> using options -source:future -language:experimental.modularity
2+
3+
class MySortedSet[T : Ord] extends SortedSet[T]
4+
5+
trait Ord[T]
6+
7+
trait Sorted[T]:
8+
given Ord[T] as ord = compiletime.deferred
9+
10+
class SortedSet[T : Ord] extends Sorted[T]

0 commit comments

Comments
 (0)