Skip to content

Commit 1f0da4e

Browse files
committed
Fix missing members reporting for var setters
Suppress reporting a missing setter only if the corresponding getter was also reported as missing. Fixes #23474
1 parent 60864b3 commit 1f0da4e

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,16 @@ object RefChecks {
703703
// to consolidate getters and setters.
704704
val grouped = missing.groupBy(_.underlyingSymbol.name)
705705

706+
def isDuplicateSetter(sym: Symbol): Boolean =
707+
sym.isSetter && {
708+
val field = sym.accessedFieldOrGetter
709+
grouped.getOrElse(field.name, Nil).contains(field)
710+
}
711+
706712
val missingMethods = grouped.toList flatMap {
707713
case (name, syms) =>
708714
lastOverrides(syms)
709-
.filterConserve(!_.isSetter)
715+
.filterConserve(!isDuplicateSetter(_)) // Avoid reporting override error for both `x` and setter `x_=`
710716
.distinctBy(_.signature) // Avoid duplication for similar definitions (#19731)
711717
}
712718

tests/neg/i23474.check

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Error: tests/neg/i23474.scala:5:11 ----------------------------------------------------------------------------------
2+
5 |case class Y(val comment: String) extends Comment // error
3+
| ^
4+
| class Y needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
5+
| (Note that an abstract var requires a setter in addition to the getter)
6+
-- Error: tests/neg/i23474.scala:7:6 -----------------------------------------------------------------------------------
7+
7 |class Z extends Comment: // error
8+
| ^
9+
| class Z needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
10+
| (Note that an abstract var requires a setter in addition to the getter)
11+
-- [E164] Declaration Error: tests/neg/i23474.scala:11:15 --------------------------------------------------------------
12+
11 | override def comment: String = "" // error
13+
| ^
14+
| error overriding variable comment in trait Comment of type String;
15+
| method comment of type => String cannot override a mutable variable
16+
-- Error: tests/neg/i23474.scala:10:6 ----------------------------------------------------------------------------------
17+
10 |class X extends Comment: // error
18+
| ^
19+
| class X needs to be abstract, since var comment_=(x$1: String): Unit in trait Comment is not defined
20+
| (Note that an abstract var requires a setter in addition to the getter)
21+
-- Error: tests/neg/i23474.scala:13:6 ----------------------------------------------------------------------------------
22+
13 |class W extends Comment // error
23+
| ^
24+
| class W needs to be abstract, since var comment: String in trait Comment is not defined
25+
| (Note that variables need to be initialized to be defined)

tests/neg/i23474.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Comment {
2+
var comment: String
3+
}
4+
5+
case class Y(val comment: String) extends Comment // error
6+
7+
class Z extends Comment: // error
8+
val comment: String = ""
9+
10+
class X extends Comment: // error
11+
override def comment: String = "" // error
12+
13+
class W extends Comment // error
14+
15+
16+
class OK:
17+
val comment: String = ""
18+
def comment_=(x: String): Unit = ()
19+

0 commit comments

Comments
 (0)