Skip to content

Commit f9080e0

Browse files
authored
fix: treat static vals as enclosures in lambdalift (#22452)
closes #22408
2 parents 391da3d + f52782e commit f9080e0

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+9
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,15 @@ object SymDenotations {
12321232
else if (this.exists) owner.enclosingMethod
12331233
else NoSymbol
12341234

1235+
/** The closest enclosing method or static symbol containing this definition.
1236+
* A local dummy owner is mapped to the primary constructor of the class.
1237+
*/
1238+
final def enclosingMethodOrStatic(using Context): Symbol =
1239+
if this.is(Method) || this.hasAnnotation(defn.ScalaStaticAnnot) then symbol
1240+
else if this.isClass then primaryConstructor
1241+
else if this.exists then owner.enclosingMethodOrStatic
1242+
else NoSymbol
1243+
12351244
/** The closest enclosing extension method containing this definition,
12361245
* including methods outside the current class.
12371246
*/

Diff for: compiler/src/dotty/tools/dotc/transform/LambdaLift.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ object LambdaLift:
3636
val liftedDefs: HashMap[Symbol, ListBuffer[Tree]] = new HashMap
3737

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

4242
override def process(tree: Tree)(using Context): Unit =
4343
super.process(tree)

Diff for: tests/pos/i22408.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Obj:
2+
@scala.annotation.static
3+
val some_static_value: Int = {
4+
val some_local_value: Int = {
5+
val some_local_value_1 = ???
6+
some_local_value_1
7+
}
8+
some_local_value
9+
}
10+
11+
class Obj

0 commit comments

Comments
 (0)