Skip to content

Commit 97ebba0

Browse files
committed
fix #12401: mangle ctor names in ExtractAPI
1 parent cf86788 commit 97ebba0

File tree

16 files changed

+108
-11
lines changed

16 files changed

+108
-11
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ private class ExtractAPICollector(using Context) extends ThunkHolder {
366366
val vparamss = paramLists(sym.info, sym.paramSymss)
367367
val retTp = sym.info.finalResultType.widenExpr
368368

369-
api.Def.of(sym.name.toString, apiAccess(sym), apiModifiers(sym),
369+
api.Def.of(sym.zincMangledName.toString, apiAccess(sym), apiModifiers(sym),
370370
apiAnnotations(sym).toArray, tparams.toArray, vparamss.toArray, apiType(retTp))
371371
}
372372

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,6 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
310310
}
311311
}
312312

313-
/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
314-
private def mangledName(sym: Symbol)(using Context): Name = {
315-
def constructorName = sym.owner.fullName ++ ";init;"
316-
317-
if (sym.isConstructor) constructorName
318-
else sym.name.stripModuleClassSuffix
319-
}
320-
321313
private def addMemberRefDependency(sym: Symbol)(using Context): Unit =
322314
if (!ignoreDependency(sym)) {
323315
val enclOrModuleClass = if (sym.is(ModuleVal)) sym.moduleClass else sym.enclosingClass
@@ -327,7 +319,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
327319
if (fromClass.exists) { // can happen when visiting imports
328320
assert(fromClass.isClass)
329321

330-
addUsedName(fromClass, mangledName(sym), UseScope.Default)
322+
addUsedName(fromClass, sym.zincMangledName, UseScope.Default)
331323
// packages have class symbol. Only record them as used names but not dependency
332324
if (!sym.is(Package)) {
333325
_dependencies += ClassDependency(fromClass, enclOrModuleClass, DependencyByMemberRef)
@@ -490,7 +482,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
490482
val traverser = new TypeDependencyTraverser {
491483
def addDependency(symbol: Symbol) =
492484
if (!ignoreDependency(symbol) && symbol.is(Sealed)) {
493-
val usedName = mangledName(symbol)
485+
val usedName = symbol.zincMangledName
494486
addUsedName(usedName, UseScope.PatMatTarget)
495487
}
496488
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dotty.tools.dotc.sbt
2+
3+
import dotty.tools.dotc.core.Contexts.Context
4+
import dotty.tools.dotc.core.Symbols.Symbol
5+
import dotty.tools.dotc.core.NameOps.stripModuleClassSuffix
6+
import dotty.tools.dotc.core.Names.Name
7+
8+
extension (sym: Symbol)
9+
10+
def constructorName(using Context) =
11+
sym.owner.fullName ++ ";init;"
12+
13+
/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
14+
def zincMangledName(using Context): Name =
15+
if (sym.isConstructor) constructorName
16+
else sym.name.stripModuleClassSuffix
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A(a: Int)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B { val y = new A(2) }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A(a: Int)(b: String)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B { val y = new A(2)("a") }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion"),
10+
scalacOptions += "-source:3.0-migration"
11+
)
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> compile
2+
$ copy-file changes/A2.scala A.scala
3+
# we should detect that A has changed,
4+
# this should trigger recompilation of B,
5+
# which should fail due to missing curried argument
6+
-> compile
7+
$ copy-file changes/B2.scala B.scala
8+
# B is updated so that it passes an extra
9+
# argument to A's constructor, it should compile
10+
> compile
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Used Names:
2+
===========
3+
class A: Object in [Default], Int in [Default], Unit in [Default], java.lang.Object$u003Binit$u003B in [Default],
4+
5+
Dependencies:
6+
=============
7+
ClassDependency(class A,class Int,DependencyByMemberRef)
8+
ClassDependency(class A,class Object,DependencyByInheritance)
9+
ClassDependency(class A,class Object,DependencyByMemberRef)
10+
ClassDependency(class A,class Unit,DependencyByMemberRef)
11+
@scala.annotation.internal.this#SourceFile(TREE_HASH = 1656543685) class A extends java.lang.this#Object with scala.this#Matchable with scala.this#Any {
12+
^inherited^ def java.lang.Object;init;(): java.lang.this#Object
13+
^inherited^ final def eq(x$0: java.lang.this#Object): scala.this#Boolean
14+
^inherited^ final def ne(x$0: java.lang.this#Object): scala.this#Boolean
15+
^inherited^ final def synchronized[ X0 >: scala.this#Nothing <: scala.this#Any](x$0: <X0>): <X0>
16+
^inherited^ protected def clone(): java.lang.this#Object
17+
^inherited^ protected def finalize(): scala.this#Unit
18+
^inherited^ final def notify(): scala.this#Unit
19+
^inherited^ final def notifyAll(): scala.this#Unit
20+
^inherited^ final def wait(): scala.this#Unit
21+
^inherited^ final def wait(x$0: scala.this#Long): scala.this#Unit
22+
^inherited^ final def wait(x$0: scala.this#Long, x$1: scala.this#Int): scala.this#Unit
23+
^inherited^ final def ==(x$0: scala.this#Any): scala.this#Boolean
24+
^inherited^ final def !=(x$0: scala.this#Any): scala.this#Boolean
25+
^inherited^ def equals(x$0: scala.this#Any): scala.this#Boolean
26+
^inherited^ def hashCode(): scala.this#Int
27+
^inherited^ def toString(): java.lang.this#String
28+
^inherited^ final def ##: scala.this#Int
29+
^inherited^ final def getClass[ X0 >: scala.Any.this <: scala.this#Any](): java.lang.this#Class[<_> forSome { type _ >: scala.this#Nothing <: <X0> }]
30+
^inherited^ final def isInstanceOf[ X0 >: scala.this#Nothing <: scala.this#Any]: scala.this#Boolean
31+
^inherited^ final def asInstanceOf[ X0 >: scala.this#Nothing <: scala.this#Any]: <X0>
32+
^inherited^ final def $isInstanceOf[ X0 >: scala.this#Nothing <: scala.this#Any]: scala.this#Boolean
33+
^inherited^ final def $asInstanceOf[ X0 >: scala.this#Nothing <: scala.this#Any]: <X0>
34+
35+
private[this] val a: scala.this#Int
36+
def A;init;(a: scala.this#Int): this#A
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A(a: Int)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B { val y = new A(2) }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A(a: String)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B { val y = new A("a") }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion"),
10+
scalacOptions += "-source:3.0-migration"
11+
)
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> compile
2+
$ copy-file changes/A2.scala A.scala
3+
# we should detect that A has changed,
4+
# this should trigger recompilation of B,
5+
# which should fail due to mismatched arguments
6+
-> compile
7+
$ copy-file changes/B2.scala B.scala
8+
# B is updated so that it passes a correct
9+
# argument to A's constructor, it should compile
10+
> compile

0 commit comments

Comments
 (0)