Skip to content

@targetName is only checked in non-primary constructors #24681

@hamzaremmal

Description

@hamzaremmal

Compiler version

abfb3f9

Minimized code

import scala.annotation.targetName

class Foo @targetName("bla") ()

Output

Exception in thread "main" 
  unhandled exception while running erasure on test.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.


     while compiling: test.scala
        during phase: erasure
                mode: Mode(ImplicitsEnabled)
     library version: version 3.8.1-RC1-bin-SNAPSHOT-nonbootstrapped
    compiler version: version 3.8.1-RC1-bin-SNAPSHOT-nonbootstrapped-git-abfb3f9
            settings: -Vprint List(typer) -classpath <redacted>
java.lang.AssertionError: assertion failed

        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:13)
        at dotty.tools.dotc.core.Contexts$FreshContext.setOwner(Contexts.scala:647)
        at dotty.tools.dotc.core.Contexts$Context.superOrThisCallContext(Contexts.scala:446)
        at dotty.tools.dotc.core.Contexts$Context.superCallContext(Contexts.scala:424)
        at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:3219)
        at dotty.tools.dotc.transform.Erasure$Typer.typedClassDef(Erasure.scala:1048)
        at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3753)
        at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3757)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3858)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3946)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3951)

Expectation

Should not compile and obviously not crash the compiler.

Notes

I noticed this while reading the code of Typer that we only do the check for non-primary constructors :

if sym.isConstructor then
if sym.is(Inline) then
report.error("constructors cannot be `inline`", ddef)
if sym.isPrimaryConstructor then
if sym.owner.is(Case) then
for
params <- paramss1.dropWhile(TypeDefs.unapply(_).isDefined).take(1)
case param: ValDef <- params
do
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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions