Skip to content

Commit 2762567

Browse files
authored
Merge pull request #3503 from Duhemm/fix/3476
Fix #3476: Add `Artifact` flag to closures
2 parents 845323e + 41c94f1 commit 2762567

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ object desugar {
804804
* If `inlineable` is true, tag $anonfun with an @inline annotation.
805805
*/
806806
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree = TypeTree(), inlineable: Boolean)(implicit ctx: Context) = {
807-
var mods = synthetic
807+
var mods = synthetic | Artifact
808808
if (inlineable) mods |= Inline
809809
Block(
810810
DefDef(nme.ANON_FUN, Nil, params :: Nil, tpt, body).withMods(mods),

compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import core.TypeErasure.erasure
1212
import core.Types._
1313
import core.classfile.ClassfileConstants
1414
import ast.Trees._
15+
import SymUtils._
1516
import TypeUtils._
1617
import java.lang.StringBuilder
1718

@@ -27,8 +28,11 @@ object GenericSignatures {
2728
* @param info The type of the symbol
2829
* @return The signature if it could be generated, `None` otherwise.
2930
*/
30-
def javaSig(sym0: Symbol, info: Type)(implicit ctx: Context): Option[String] =
31-
javaSig0(sym0, info)(ctx.withPhase(ctx.erasurePhase))
31+
def javaSig(sym0: Symbol, info: Type)(implicit ctx: Context): Option[String] = {
32+
// Avoid generating a signature for local symbols.
33+
if (sym0.isLocal) None
34+
else javaSig0(sym0, info)(ctx.withPhase(ctx.erasurePhase))
35+
}
3236

3337
@noinline
3438
private final def javaSig0(sym0: Symbol, info: Type)(implicit ctx: Context): Option[String] = {

compiler/src/dotty/tools/dotc/transform/LambdaLift.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ object LambdaLift {
341341
local.copySymDenotation(
342342
owner = newOwner,
343343
name = newName(local),
344-
initFlags = local.flags &~ Module &~ Final | Private | maybeStatic,
344+
initFlags = local.flags &~ Module &~ Final | Private | Lifted | maybeStatic,
345345
// drop Module because class is no longer a singleton in the lifted context.
346346
info = liftedInfo(local)).installAfter(thisPhase)
347347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OK
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
object Test {
2+
3+
def hasGenericSignature(cls: Class[_], methName: String): Boolean = {
4+
cls.getDeclaredMethods().find(_.getName.contains(methName)) match {
5+
case None => throw new NoSuchMethodError(s"No $methName in ${cls.getName}")
6+
case Some(meth) => meth.getTypeParameters.nonEmpty
7+
}
8+
}
9+
10+
def checkHasGenericSignature(cls: Class[_], methName: String): Unit =
11+
assert(hasGenericSignature(cls, methName))
12+
13+
def checkDoesntHaveGenericSignature(cls: Class[_], methName: String): Unit =
14+
assert(!hasGenericSignature(cls, methName))
15+
16+
def main(args: Array[String]): Unit = {
17+
18+
checkHasGenericSignature(classOf[TopLevelClass], "meth")
19+
checkHasGenericSignature(classOf[AbstractTopLevelClass], "meth")
20+
checkHasGenericSignature(classOf[TopLevelClass#InsideClass], "meth")
21+
checkHasGenericSignature(classOf[TopLevelClass#AbstractInsideClass], "meth")
22+
checkDoesntHaveGenericSignature(new TopLevelClass().localClass, "meth")
23+
checkDoesntHaveGenericSignature(new TopLevelClass().otherLocalClass, "meth")
24+
25+
checkHasGenericSignature(TopLevelObject.getClass, "meth")
26+
checkHasGenericSignature(classOf[TopLevelObject.InsideObject], "meth")
27+
checkHasGenericSignature(classOf[TopLevelObject.AbstractInsideObject], "meth")
28+
checkDoesntHaveGenericSignature(TopLevelObject.localClass, "meth")
29+
checkDoesntHaveGenericSignature(TopLevelObject.otherLocalClass, "meth")
30+
31+
println("OK")
32+
}
33+
}
34+
35+
object TopLevelObject {
36+
def meth[T](x: T): T = x
37+
38+
def localObject: Class[_] = {
39+
object LocalObject {
40+
def meth[T](x: T): T = x
41+
}
42+
LocalObject.getClass
43+
}
44+
45+
def localClass: Class[_] = {
46+
class LocalClass {
47+
def meth[T](x: T): T = x
48+
}
49+
classOf[LocalClass]
50+
}
51+
52+
val otherLocalClass: Class[_] = {
53+
class LocalClass {
54+
def meth[T](x: T): T = x
55+
}
56+
classOf[LocalClass]
57+
}
58+
59+
class InsideObject {
60+
def meth[T](x: T): T = x
61+
}
62+
63+
abstract class AbstractInsideObject {
64+
def meth[T](x: T): T = x
65+
}
66+
}
67+
68+
class TopLevelClass {
69+
70+
def meth[T](x: T): T = x
71+
72+
def localClass: Class[_] = {
73+
class LocalClass {
74+
def meth[T](x: T): T = x
75+
}
76+
classOf[LocalClass]
77+
}
78+
79+
val otherLocalClass: Class[_] = {
80+
class LocalClass {
81+
def meth[T](x: T): T = x
82+
}
83+
classOf[LocalClass]
84+
}
85+
86+
class InsideClass {
87+
def meth[T](x: T): T = x
88+
}
89+
90+
abstract class AbstractInsideClass {
91+
def meth[T](x: T): T = x
92+
}
93+
}
94+
95+
abstract class AbstractTopLevelClass {
96+
def meth[T](x: T): T = x
97+
}
98+

0 commit comments

Comments
 (0)