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 @@ -630,10 +630,16 @@ object RefChecks {
630
630
// to consolidate getters and setters.
631
631
val grouped = missing.groupBy(_.underlyingSymbol.name)
632
632
633
+ def isDuplicateSetter (sym : Symbol ): Boolean =
634
+ sym.isSetter && {
635
+ val field = sym.accessedFieldOrGetter
636
+ grouped.getOrElse(field.name, Nil ).contains(field)
637
+ }
638
+
633
639
val missingMethods = grouped.toList flatMap {
634
640
case (name, syms) =>
635
641
lastOverrides(syms)
636
- .filterConserve(! _.isSetter)
642
+ .filterConserve(! isDuplicateSetter(_)) // Avoid reporting override error for both `x` and setter `x_=`
637
643
.distinctBy(_.signature) // Avoid duplication for similar definitions (#19731)
638
644
}
639
645
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