Skip to content

Commit

Permalink
Report only non-overridden unimplemented members
Browse files Browse the repository at this point in the history
Previously, when a concrete class A had unimplemented members that are
overridden, all overrides would be reported as unimplemented in the
error message. This would produce error messages that are not accurate,
and that suggest stubs that are not correct.

This patch fixes the issue by reporting in the error message only the
unimplemented members that are not overridden by other unimplemented
members.

Fixes #21335
  • Loading branch information
Duhemm committed Aug 6, 2024
1 parent bad02ab commit 61cd930
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,13 @@ object RefChecks {
&& withMode(Mode.IgnoreCaptures)(mbrDenot.matchesLoosely(impl, alwaysCompareTypes = true)))
.exists

/** Filter out symbols from `syms` that are overridden by a symbol appearing later in the list.
* Symbols that are not overridden are kept. */
def lastOverrides(syms: List[Symbol]): List[Symbol] =
syms.foldLeft(Nil):
case (acc, sym) if acc.exists(s => isOverridingPair(s, sym, clazz.thisType)) => acc
case (acc, sym) => sym :: acc

/** The term symbols in this class and its baseclasses that are
* abstract in this class. We can't use memberNames for that since
* a concrete member might have the same signature as an abstract
Expand All @@ -708,7 +715,7 @@ object RefChecks {
for bc <- clazz.baseClasses; sym <- bc.info.decls.toList do
if sym.is(DeferredTerm) && !isImplemented(sym) && !ignoreDeferred(sym) then
buf += sym
buf.toList
lastOverrides(buf.toList)

// 2. Check that only abstract classes have deferred members
def checkNoAbstractMembers(): Unit = {
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i21335.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Error: tests/neg/i21335.scala:7:6 -----------------------------------------------------------------------------------
7 |class Z1 extends Bar1 // error
| ^
| class Z1 needs to be abstract, since override def bar(): Bar1 in trait Bar1 is not defined
-- Error: tests/neg/i21335.scala:12:6 ----------------------------------------------------------------------------------
12 |class Z2 extends Bar2 // error
| ^
| class Z2 needs to be abstract, since def bar(): Bar2 in trait Bar2 is not defined
12 changes: 12 additions & 0 deletions tests/neg/i21335.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Foo:
def bar(): Foo

trait Bar1 extends Foo:
override def bar(): Bar1

class Z1 extends Bar1 // error

trait Bar2 extends Foo:
def bar(): Bar2

class Z2 extends Bar2 // error

0 comments on commit 61cd930

Please sign in to comment.