Skip to content

Commit 13d4963

Browse files
mbovelHarrisL2kalil0321
authored
Compute the right span for abstract error messages (#23853)
Fixes #22941. Done during the compiler spree of September 1st, 2025. Co-authored-by: HarrisL2 <[email protected]> Co-authored-by: kalil0321 <[email protected]>
1 parent 998a11d commit 13d4963

File tree

10 files changed

+29
-11
lines changed

10 files changed

+29
-11
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ object RefChecks {
329329

330330
val mixinOverrideErrors = new mutable.ListBuffer[MixinOverrideError]()
331331

332+
/** Returns a `SourcePosition` containing the full span (with the correct
333+
* end) of the class name. */
334+
def clazzNamePos =
335+
if clazz.name == tpnme.ANON_CLASS then
336+
clazz.srcPos
337+
else
338+
val clazzNameEnd = clazz.srcPos.span.start + clazz.name.stripModuleClassSuffix.lastPart.length
339+
clazz.srcPos.sourcePos.copy(span = clazz.srcPos.span.withEnd(clazzNameEnd))
340+
332341
def printMixinOverrideErrors(): Unit =
333342
mixinOverrideErrors.toList match {
334343
case Nil =>
@@ -914,7 +923,7 @@ object RefChecks {
914923
checkNoAbstractDecls(clazz)
915924

916925
if (abstractErrors.nonEmpty)
917-
report.error(abstractErrorMessage, clazz.srcPos)
926+
report.error(abstractErrorMessage, clazzNamePos)
918927

919928
checkMemberTypesOK()
920929
checkCaseClassInheritanceInvariant()

tests/neg/i10666.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Error: tests/neg/i10666.scala:8:6 -----------------------------------------------------------------------------------
22
8 |class Bar extends Foo { // error
3-
| ^
3+
| ^^^
44
| class Bar needs to be abstract, since def foo[T <: B](tx: T): Unit in trait Foo is not defined
55
| (Note that
66
| parameter T in def foo[T <: B](tx: T): Unit in trait Foo does not match

tests/neg/i12828.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Error: tests/neg/i12828.scala:7:7 -----------------------------------------------------------------------------------
22
7 |object Baz extends Bar[Int] // error: not implemented
3-
| ^
3+
| ^^^
44
| object creation impossible, since def foo(x: A): Unit in trait Foo is not defined
55
| (Note that
66
| parameter A in def foo(x: A): Unit in trait Foo does not match

tests/neg/i13466.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Error: tests/neg/i13466.scala:9:6 -----------------------------------------------------------------------------------
22
9 |given none: SomeTrait[Finally] with {} // error
3-
| ^
3+
| ^^^^
44
| object creation impossible, since:
55
| it has 3 unimplemented members.
66
| /** As seen from module class none$, the missing signatures are as follows.

tests/neg/i19731.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Error: tests/neg/i19731.scala:4:6 -----------------------------------------------------------------------------------
22
4 |class F1 extends Foo: // error
3-
| ^
3+
| ^^
44
| class F1 needs to be abstract, since def foo(): Unit in class F1 is not defined
55
-- Error: tests/neg/i19731.scala:7:6 -----------------------------------------------------------------------------------
66
7 |class F2 extends Foo: // error
7-
| ^
7+
| ^^
88
| class F2 needs to be abstract, since:
99
| it has 2 unimplemented members.
1010
| /** As seen from class F2, the missing signatures are as follows.
@@ -14,7 +14,7 @@
1414
| def foo(x: Int): Unit = ???
1515
-- Error: tests/neg/i19731.scala:16:6 ----------------------------------------------------------------------------------
1616
16 |class B1 extends Bar: // error
17-
| ^
17+
| ^^
1818
| class B1 needs to be abstract, since:
1919
| it has 2 unimplemented members.
2020
| /** As seen from class B1, the missing signatures are as follows.

tests/neg/i21335.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- Error: tests/neg/i21335.scala:7:6 -----------------------------------------------------------------------------------
22
7 |class Z1 extends Bar1 // error
3-
| ^
3+
| ^^
44
| class Z1 needs to be abstract, since override def bar(): Bar1 in trait Bar1 is not defined
55
-- Error: tests/neg/i21335.scala:12:6 ----------------------------------------------------------------------------------
66
12 |class Z2 extends Bar2 // error
7-
| ^
7+
| ^^
88
| class Z2 needs to be abstract, since def bar(): Bar2 in trait Bar2 is not defined

tests/neg/i22941.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i22941.scala:4:6 -----------------------------------------------------------------------------------
2+
4 |class Baz extends Foo: // error
3+
| ^^^
4+
| class Baz needs to be abstract, since def bar: String in trait Foo is not defined

tests/neg/i22941.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Foo:
2+
def bar: String
3+
4+
class Baz extends Foo: // error
5+
val a = "hello"

tests/neg/i9329.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Error: tests/neg/i9329.scala:8:6 ------------------------------------------------------------------------------------
22
8 |class GrandSon extends Son // error
3-
| ^
3+
| ^^^^^^^^
44
|class GrandSon needs to be abstract, since def name: String in trait Parent is not defined
55
|(The class implements abstract override def name: String in trait Son but that definition still needs an implementation)

tests/neg/targetName-override.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
| method ++ of type (xs: Alpha[String]): Alpha[String] misses a target name annotation @targetName(append)
1616
-- Error: tests/neg/targetName-override.scala:14:6 ---------------------------------------------------------------------
1717
14 |class Beta extends Alpha[String] { // error: needs to be abstract
18-
| ^
18+
| ^^^^
1919
|class Beta needs to be abstract, since there is a deferred declaration of method foo in class Alpha of type (x: String): String which is not implemented in a subclass

0 commit comments

Comments
 (0)