Skip to content

Commit 60d04ca

Browse files
Merge pull request #3522 from dotty-staging/fix-#3518
Fix #3518: Do not generate calls to BoxesRunTime for Unit box/unbox
2 parents 6f68143 + 85fb578 commit 60d04ca

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
169169
s eq defn.newArrayMethod
170170
}
171171

172-
def isBox(sym: Symbol): Boolean = Erasure.Boxing.isBox(sym)
173-
def isUnbox(sym: Symbol): Boolean = Erasure.Boxing.isUnbox(sym)
172+
def isBox(sym: Symbol): Boolean =
173+
Erasure.Boxing.isBox(sym) && sym.denot.owner != defn.UnitModuleClass
174+
def isUnbox(sym: Symbol): Boolean =
175+
Erasure.Boxing.isUnbox(sym) && sym.denot.owner != defn.UnitModuleClass
174176

175177
val primitives: Primitives = new Primitives {
176178
val primitives = new DottyPrimitives(ctx)

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ class Definitions {
397397

398398
lazy val UnitType: TypeRef = valueTypeRef("scala.Unit", BoxedUnitType, java.lang.Void.TYPE, UnitEnc, nme.specializedTypeNames.Void)
399399
def UnitClass(implicit ctx: Context) = UnitType.symbol.asClass
400+
def UnitModuleClass(implicit ctx: Context) = UnitType.symbol.asClass.linkedClass
400401
lazy val BooleanType = valueTypeRef("scala.Boolean", BoxedBooleanType, java.lang.Boolean.TYPE, BooleanEnc, nme.specializedTypeNames.Boolean)
401402
def BooleanClass(implicit ctx: Context) = BooleanType.symbol.asClass
402403
lazy val Boolean_notR = BooleanClass.requiredMethodRef(nme.UNARY_!)

tests/run/i3518.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
()
2+
()
3+
A
4+
()

tests/run/i3518.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
Unit.box(())
4+
Unit.unbox(Unit.box(()))
5+
println(Unit.box(()))
6+
println(Unit.unbox(Unit.box(())))
7+
println(Unit.box({
8+
println("A")
9+
()
10+
}))
11+
}
12+
}

0 commit comments

Comments
 (0)