Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: treat static vals as enclosures in lambdalift #22452

Merged
merged 1 commit into from
Feb 17, 2025
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
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,15 @@ object SymDenotations {
else if (this.exists) owner.enclosingMethod
else NoSymbol

/** The closest enclosing method or static symbol containing this definition.
* A local dummy owner is mapped to the primary constructor of the class.
*/
final def enclosingMethodOrStatic(using Context): Symbol =
if this.is(Method) || this.hasAnnotation(defn.ScalaStaticAnnot) then symbol
else if this.isClass then primaryConstructor
else if this.exists then owner.enclosingMethodOrStatic
else NoSymbol

/** The closest enclosing extension method containing this definition,
* including methods outside the current class.
*/
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ object LambdaLift:
val liftedDefs: HashMap[Symbol, ListBuffer[Tree]] = new HashMap

val deps = new Dependencies(ctx.compilationUnit.tpdTree, ctx.withPhase(thisPhase)):
def isExpr(sym: Symbol)(using Context): Boolean = sym.is(Method)
def enclosure(using Context) = ctx.owner.enclosingMethod
def isExpr(sym: Symbol)(using Context): Boolean = sym.is(Method) || sym.hasAnnotation(defn.ScalaStaticAnnot)
def enclosure(using Context) = ctx.owner.enclosingMethodOrStatic

override def process(tree: Tree)(using Context): Unit =
super.process(tree)
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i22408.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Obj:
@scala.annotation.static
val some_static_value: Int = {
val some_local_value: Int = {
val some_local_value_1 = ???
some_local_value_1
}
some_local_value
}

class Obj