Skip to content

Commit

Permalink
Merge pull request #12712 from dotty-staging/fix-12401
Browse files Browse the repository at this point in the history
fix #12401: mangle ctor names in ExtractAPI
  • Loading branch information
smarter authored Jun 4, 2021
2 parents 3390d70 + 9e159db commit a9a7e70
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private class ExtractAPICollector(using Context) extends ThunkHolder {
val vparamss = paramLists(sym.info, sym.paramSymss)
val retTp = sym.info.finalResultType.widenExpr

api.Def.of(sym.name.toString, apiAccess(sym), apiModifiers(sym),
api.Def.of(sym.zincMangledName.toString, apiAccess(sym), apiModifiers(sym),
apiAnnotations(sym).toArray, tparams.toArray, vparamss.toArray, apiType(retTp))
}

Expand Down
12 changes: 2 additions & 10 deletions compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,6 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
}
}

/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
private def mangledName(sym: Symbol)(using Context): Name = {
def constructorName = sym.owner.fullName ++ ";init;"

if (sym.isConstructor) constructorName
else sym.name.stripModuleClassSuffix
}

private def addMemberRefDependency(sym: Symbol)(using Context): Unit =
if (!ignoreDependency(sym)) {
val enclOrModuleClass = if (sym.is(ModuleVal)) sym.moduleClass else sym.enclosingClass
Expand All @@ -327,7 +319,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
if (fromClass.exists) { // can happen when visiting imports
assert(fromClass.isClass)

addUsedName(fromClass, mangledName(sym), UseScope.Default)
addUsedName(fromClass, sym.zincMangledName, UseScope.Default)
// packages have class symbol. Only record them as used names but not dependency
if (!sym.is(Package)) {
_dependencies += ClassDependency(fromClass, enclOrModuleClass, DependencyByMemberRef)
Expand Down Expand Up @@ -490,7 +482,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
val traverser = new TypeDependencyTraverser {
def addDependency(symbol: Symbol) =
if (!ignoreDependency(symbol) && symbol.is(Sealed)) {
val usedName = mangledName(symbol)
val usedName = symbol.zincMangledName
addUsedName(usedName, UseScope.PatMatTarget)
}
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/sbt/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dotty.tools.dotc.sbt

import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Symbols.Symbol
import dotty.tools.dotc.core.NameOps.stripModuleClassSuffix
import dotty.tools.dotc.core.Names.Name

extension (sym: Symbol)

def constructorName(using Context) =
sym.owner.fullName ++ ";init;"

/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
def zincMangledName(using Context): Name =
if (sym.isConstructor) constructorName
else sym.name.stripModuleClassSuffix
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors-curried/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A(a: Int)
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors-curried/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class B { val y = new A(2) }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A(a: Int)(b: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class B { val y = new A(2)("a") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sbt._
import Keys._

object DottyInjectedPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements

override val projectSettings = Seq(
scalaVersion := sys.props("plugin.scalaVersion"),
scalacOptions += "-source:3.0-migration"
)
}
10 changes: 10 additions & 0 deletions sbt-test/source-dependencies/constructors-curried/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
> compile
$ copy-file changes/A2.scala A.scala
# we should detect that A has changed,
# this should trigger recompilation of B,
# which should fail due to missing curried argument
-> compile
$ copy-file changes/B2.scala B.scala
# B is updated so that it passes an extra
# argument to A's constructor, it should compile
> compile
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A(a: Int)
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class B { val y = new A(2) }
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors/changes/A2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A(a: String)
1 change: 1 addition & 0 deletions sbt-test/source-dependencies/constructors/changes/B2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class B { val y = new A("a") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sbt._
import Keys._

object DottyInjectedPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements

override val projectSettings = Seq(
scalaVersion := sys.props("plugin.scalaVersion"),
scalacOptions += "-source:3.0-migration"
)
}
10 changes: 10 additions & 0 deletions sbt-test/source-dependencies/constructors/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
> compile
$ copy-file changes/A2.scala A.scala
# we should detect that A has changed,
# this should trigger recompilation of B,
# which should fail due to mismatched arguments
-> compile
$ copy-file changes/B2.scala B.scala
# B is updated so that it passes a correct
# argument to A's constructor, it should compile
> compile

0 comments on commit a9a7e70

Please sign in to comment.