Skip to content

Fix missing members reporting for var setters #23476

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

Merged
merged 1 commit into from
Jul 5, 2025
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
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,16 @@ object RefChecks {
// to consolidate getters and setters.
val grouped = missing.groupBy(_.underlyingSymbol.name)

def isDuplicateSetter(sym: Symbol): Boolean =
sym.isSetter && {
val field = sym.accessedFieldOrGetter
grouped.getOrElse(field.name, Nil).contains(field)
}

val missingMethods = grouped.toList flatMap {
case (name, syms) =>
lastOverrides(syms)
.filterConserve(!_.isSetter)
.filterConserve(!isDuplicateSetter(_)) // Avoid reporting override error for both `x` and setter `x_=`
.distinctBy(_.signature) // Avoid duplication for similar definitions (#19731)
}

Expand Down
25 changes: 25 additions & 0 deletions tests/neg/i23474.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Error: tests/neg/i23474.scala:5:11 ----------------------------------------------------------------------------------
5 |case class Y(val comment: String) extends Comment // error
| ^
| class Y needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
| (Note that an abstract var requires a setter in addition to the getter)
-- Error: tests/neg/i23474.scala:7:6 -----------------------------------------------------------------------------------
7 |class Z extends Comment: // error
| ^
| class Z needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
| (Note that an abstract var requires a setter in addition to the getter)
-- [E164] Declaration Error: tests/neg/i23474.scala:11:15 --------------------------------------------------------------
11 | override def comment: String = "" // error
| ^
| error overriding variable comment in trait Comment of type String;
| method comment of type => String cannot override a mutable variable
-- Error: tests/neg/i23474.scala:10:6 ----------------------------------------------------------------------------------
10 |class X extends Comment: // error
| ^
| class X needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
| (Note that an abstract var requires a setter in addition to the getter)
-- Error: tests/neg/i23474.scala:13:6 ----------------------------------------------------------------------------------
13 |class W extends Comment // error
| ^
| class W needs to be abstract, since var comment: String in trait Comment is not defined
| (Note that variables need to be initialized to be defined)
19 changes: 19 additions & 0 deletions tests/neg/i23474.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
trait Comment {
var comment: String
}

case class Y(val comment: String) extends Comment // error

class Z extends Comment: // error
val comment: String = ""

class X extends Comment: // error
override def comment: String = "" // error

class W extends Comment // error


class OK:
val comment: String = ""
def comment_=(x: String): Unit = ()

Loading