diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 389cd306fc02..80381f42ac1a 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -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 + 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 @@ -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") diff --git a/tests/neg/i24681.check b/tests/neg/i24681.check new file mode 100644 index 000000000000..8eebab0c421c --- /dev/null +++ b/tests/neg/i24681.check @@ -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 diff --git a/tests/neg/i24681.scala b/tests/neg/i24681.scala new file mode 100644 index 000000000000..a5d6d05b4bb5 --- /dev/null +++ b/tests/neg/i24681.scala @@ -0,0 +1,3 @@ +import scala.annotation.targetName + +class Foo @targetName("bla") () // error