Skip to content

Commit 845a1b6

Browse files
committed
Enforce restriction that constructor proxies cannot be values
1 parent 19651ff commit 845a1b6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,14 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
250250
}
251251
}
252252

253+
def checkNoConstructorProxy(tree: Tree)(using Context): Unit =
254+
if tree.symbol.is(ConstructorProxy) then
255+
report.error(em"constructor proxy ${tree.symbol} cannot be used as a value", tree.srcPos)
256+
253257
override def transform(tree: Tree)(using Context): Tree =
254258
try tree match {
255259
case tree: Ident if !tree.isType =>
260+
checkNoConstructorProxy(tree)
256261
tree.tpe match {
257262
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
258263
case _ => tree
@@ -263,6 +268,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
263268
withMode(Mode.Type)(super.transform(tree))
264269
}
265270
else
271+
checkNoConstructorProxy(tree)
266272
transformSelect(tree, Nil)
267273
case tree: Apply =>
268274
val methType = tree.fun.tpe.widen
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
object Test extends App {
3+
class A {
4+
class A22(s: String) {
5+
def run = s
6+
}
7+
}
8+
val a = A()
9+
val x = a.A22("x") // OK
10+
val x2 = a.A22.apply("X") // OK
11+
val x3 = a.A22.apply(_) // OK
12+
val y = a.A22 // error: Cannot be used as value
13+
val z = a.A22.toString // error: Cannot be used as value
14+
val u = A // error: Cannot be used as value
15+
println(y)
16+
}

0 commit comments

Comments
 (0)