Skip to content

fix #12401: mangle ctor names in ExtractAPI #12712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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