Skip to content

Reference to wrongly declared static value #10817

@prolativ

Description

@prolativ

Minimized code

import annotation.static

class T:
  @static val foo = 10

val x = (new T).foo

Output (click arrow to expand)

-- [E109] Syntax Error: /var/folders/rp/f9y80jvs54lcy26rxnt2y2kw0000gn/T/test.scala:4:14 
4 |  @static val foo = 10
  |  ^^^^^^^^^^^^^^^^^^^^
  |  @static value foo in class T must be defined inside an object.

longer explanation available when compiling with `-explain`
exception occurred while compiling /var/folders/rp/f9y80jvs54lcy26rxnt2y2kw0000gn/T/test.scala
scala.MatchError: Apply(Select(New(TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class T)]),<init>),List()) (of class dotty.tools.dotc.ast.Trees$Apply) while compiling /var/folders/rp/f9y80jvs54lcy26rxnt2y2kw0000gn/T/test.scala
Exception in thread "main" scala.MatchError: Apply(Select(New(TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class T)]),<init>),List()) (of class dotty.tools.dotc.ast.Trees$Apply)
        at dotty.tools.dotc.transform.CheckStatic.isSafeQual$1(CheckStatic.scala:70)
        at dotty.tools.dotc.transform.CheckStatic.transformSelect(CheckStatic.scala:72)
        at dotty.tools.dotc.transform.MegaPhase.goSelect(MegaPhase.scala:599)
        at dotty.tools.dotc.transform.MegaPhase.goSelect(MegaPhase.scala:600)
        at dotty.tools.dotc.transform.MegaPhase.goSelect(MegaPhase.scala:600)
        at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:229)
        at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
        at dotty.tools.dotc.transform.MegaPhase.mapValDef$1(MegaPhase.scala:235)
        at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:240)
        at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
        at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:1061)
        at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
        at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:362)
        at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
        at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:256)
        at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:427)
        at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:437)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:442)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:1061)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:1061)
        at dotty.tools.dotc.transform.MegaPhase.recur$1(MegaPhase.scala:1061)
        at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:442)
        at dotty.tools.dotc.transform.MegaPhase.mapPackage$1(MegaPhase.scala:382)
        at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:385)
        at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:429)
        at dotty.tools.dotc.transform.MegaPhase.transformUnit(MegaPhase.scala:448)
        at dotty.tools.dotc.transform.MegaPhase.run(MegaPhase.scala:460)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:296)
        at dotty.tools.dotc.core.Phases$Phase$$Lambda$677/000000000000000000.apply(Unknown Source)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:297)
        at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:185)
        at dotty.tools.dotc.Run$$Lambda$357/000000000000000000.applyVoid(Unknown Source)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
        at dotty.tools.dotc.Run.runPhases$5(Run.scala:195)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:203)
        at dotty.tools.dotc.Run$$Lambda$317/000000000000000000.apply$mcV$sp(Unknown Source)
        at scala.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:210)
        at dotty.tools.dotc.Run.compileSources(Run.scala:146)
        at dotty.tools.dotc.Run.compile(Run.scala:130)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:39)
        at dotty.tools.dotc.Driver.process(Driver.scala:186)
        at dotty.tools.dotc.Driver.process(Driver.scala:155)
        at dotty.tools.dotc.Driver.process(Driver.scala:167)
        at dotty.tools.dotc.Driver.main(Driver.scala:194)
        at dotty.tools.dotc.Main.main(Main.scala)

The error on the declaration is correct but an attempt to refer to the value shouldn't crash the compiler.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions