File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments