diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index f2a7124f9fa4..32f04c13a3d6 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3044,7 +3044,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer body /** Implement givens that were declared with a `deferred` rhs. - * The a given value matching the declared type is searched in a + * The given value matching the declared type is searched in a * context directly enclosing the current class, in which all given * parameters of the current class are also defined. */ @@ -3061,6 +3061,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer false else true + def willBeimplementedInParentClass(m: TermRef) = + val superCls = cls.superClass + superCls.exists && superCls.asClass.baseClasses.contains(m.symbol.owner) + def givenImpl(mbr: TermRef): ValDef = val dcl = mbr.symbol val target = dcl.info.asSeenFrom(cls.thisType, dcl.owner) @@ -3090,6 +3094,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer cls.thisType.implicitMembers //.showing(i"impl def givens for $cls/$result") .filter(_.symbol.isAllOf(DeferredGivenFlags, butNot = Param)) + .filter(!willBeimplementedInParentClass(_)) // only implement the given in the topmost class //.showing(i"impl def filtered givens for $cls/$result") .filter(isGivenValue) .map(givenImpl) diff --git a/tests/pos/i21189-alt.scala b/tests/pos/i21189-alt.scala new file mode 100644 index 000000000000..08213cd627d4 --- /dev/null +++ b/tests/pos/i21189-alt.scala @@ -0,0 +1,12 @@ +//> using options -source:future -language:experimental.modularity + +class MySortedSet[T : Ord] extends SortedSet[T] + +trait Ord[T] + +trait Sorted[T] extends ParentOfSorted[T] + +trait ParentOfSorted[T]: + given Ord[T] as ord = compiletime.deferred + +class SortedSet[T : Ord] extends Sorted[T] diff --git a/tests/pos/i21189.scala b/tests/pos/i21189.scala new file mode 100644 index 000000000000..88a0bf601476 --- /dev/null +++ b/tests/pos/i21189.scala @@ -0,0 +1,10 @@ +//> using options -source:future -language:experimental.modularity + +class MySortedSet[T : Ord] extends SortedSet[T] + +trait Ord[T] + +trait Sorted[T]: + given Ord[T] as ord = compiletime.deferred + +class SortedSet[T : Ord] extends Sorted[T]