Skip to content

Commit 68396ca

Browse files
authored
fix: do not allow @targetName on primary constructor too (#24682)
Closes #24681
2 parents c6051b4 + 6b2b09f commit 68396ca

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
31263126
if sym.isConstructor then
31273127
if sym.is(Inline) then
31283128
report.error("constructors cannot be `inline`", ddef)
3129+
3130+
if sym.targetName != sym.name then
3131+
report.error(em"@targetName annotation may not be used on a constructor", ddef.srcPos)
3132+
31293133
if sym.isPrimaryConstructor then
31303134
if sym.owner.is(Case) then
31313135
for
@@ -3135,9 +3139,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
31353139
if defn.isContextFunctionType(param.tpt.tpe) then
31363140
report.error("case class element cannot be a context function", param.srcPos)
31373141
else
3138-
if sym.targetName != sym.name then
3139-
report.error(em"@targetName annotation may not be used on a constructor", ddef.srcPos)
3140-
31413142
for params <- paramss1; param <- params do
31423143
checkRefsLegal(param, sym.owner, (name, sym) => sym.is(TypeParam), "secondary constructor")
31433144

tests/neg/i24681.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i24681.scala:3:9 -----------------------------------------------------------------------------------
2+
3 |class Foo @targetName("bla") () // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^
4+
| @targetName annotation may not be used on a constructor

tests/neg/i24681.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import scala.annotation.targetName
2+
3+
class Foo @targetName("bla") () // error

0 commit comments

Comments
 (0)