Skip to content

Commit bbedb45

Browse files
authored
Tweak parameter accessor scheme (#19719)
To qualify as a super-parameter alias, a parameter of a subclass has to be passed to the primary constructor of the superclass. Fixes #19711
2 parents ca88156 + e6b726b commit bbedb45

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
116116
* This info is used in phase ParamForwarding
117117
*/
118118
private def forwardParamAccessors(impl: Template)(using Context): Unit = impl.parents match
119-
case superCall @ Apply(fn, superArgs) :: _ if superArgs.nonEmpty =>
119+
case superCall @ Apply(fn, superArgs) :: _
120+
if superArgs.nonEmpty && fn.symbol.isPrimaryConstructor =>
120121
fn.tpe.widen match
121122
case MethodType(superParamNames) =>
122123
for case stat: ValDef <- impl.body do

Diff for: tests/run/i19711.scala

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Foo(val s: Any):
2+
def this(s: String) =
3+
this(0)
4+
class Bar(s: String) extends Foo(s):
5+
def foo = s
6+
7+
class Foo2(val s: Any)
8+
class Bar2(s: String) extends Foo2(s):
9+
def foo = s
10+
11+
case class Config(_config: String)
12+
13+
abstract class Foo3(val config: Config) {
14+
def this(config: String) = {
15+
this(Config(config))
16+
}
17+
}
18+
19+
class Bar3(config: String) extends Foo3(config) {
20+
def foo(): Unit = {
21+
config.getClass()
22+
}
23+
}
24+
25+
26+
@main def Test =
27+
Bar("").foo
28+
Bar2("").foo
29+
Bar3("").foo()

0 commit comments

Comments
 (0)