From 783d05c2562a09aa0df6440ee392edbe7678abaf Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Tue, 6 Aug 2024 18:07:14 +0200 Subject: [PATCH] Report only non-overridden unimplemented members 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 [Cherry-picked 0d50a30628e0ce83a63cf40ec48afadda6ecd56a] --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 12 +++++++++++- tests/neg/i21335.check | 8 ++++++++ tests/neg/i21335.scala | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i21335.check create mode 100644 tests/neg/i21335.scala diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index faee2dd0ca56..1aad8543f97a 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -600,6 +600,15 @@ 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] = + val deduplicated = + syms.foldLeft(List.empty[Symbol]): + case (acc, sym) if acc.exists(s => isOverridingPair(s, sym, clazz.thisType)) => acc + case (acc, sym) => sym :: acc + deduplicated.reverse + /** 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 @@ -622,7 +631,8 @@ object RefChecks { val missingMethods = grouped.toList flatMap { case (name, syms) => - syms.filterConserve(!_.isSetter) + lastOverrides(syms) + .filterConserve(!_.isSetter) .distinctBy(_.signature) // Avoid duplication for similar definitions (#19731) } diff --git a/tests/neg/i21335.check b/tests/neg/i21335.check new file mode 100644 index 000000000000..a7ee092eec0e --- /dev/null +++ b/tests/neg/i21335.check @@ -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 diff --git a/tests/neg/i21335.scala b/tests/neg/i21335.scala new file mode 100644 index 000000000000..270765c80535 --- /dev/null +++ b/tests/neg/i21335.scala @@ -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