Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only implement a deferred given in a class if its parent won't implement it #21206

Merged
merged 1 commit into from
Aug 1, 2024
Merged
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
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,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.
*/
Expand All @@ -3055,6 +3055,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)
Expand Down Expand Up @@ -3084,6 +3088,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)
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i21189-alt.scala
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 10 additions & 0 deletions tests/pos/i21189.scala
Original file line number Diff line number Diff line change
@@ -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]
Loading