Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3126,6 +3126,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if sym.isConstructor then
if sym.is(Inline) then
report.error("constructors cannot be `inline`", ddef)

if sym.targetName != sym.name then
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record: Technically, if the specification says (for #22372) we are not allowed to use @targetName on constructors, then this check (that was already present) doesn't encode that specification since we can always write this example below and it will work:

import scala.annotation.targetName

class Foo @targetName("<init>") ()

report.error(em"@targetName annotation may not be used on a constructor", ddef.srcPos)

if sym.isPrimaryConstructor then
if sym.owner.is(Case) then
for
Expand All @@ -3135,9 +3139,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if defn.isContextFunctionType(param.tpt.tpe) then
report.error("case class element cannot be a context function", param.srcPos)
else
if sym.targetName != sym.name then
report.error(em"@targetName annotation may not be used on a constructor", ddef.srcPos)

for params <- paramss1; param <- params do
checkRefsLegal(param, sym.owner, (name, sym) => sym.is(TypeParam), "secondary constructor")

Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i24681.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i24681.scala:3:9 -----------------------------------------------------------------------------------
3 |class Foo @targetName("bla") () // error
| ^^^^^^^^^^^^^^^^^^^^^^
| @targetName annotation may not be used on a constructor
3 changes: 3 additions & 0 deletions tests/neg/i24681.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.annotation.targetName

class Foo @targetName("bla") () // error
Loading