From fc22a8e322e68393650ad59755fa8a05d7a12922 Mon Sep 17 00:00:00 2001 From: friendseeker <66892505+Friendseeker@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:11:28 -0800 Subject: [PATCH] Make $default$ context dependent --- .../src/main/scala/xsbt/ClassName.scala | 3 +++ .../src/main/scala/xsbt/ExtractAPI.scala | 21 +++++++++++++++---- .../constructors-unrelated-2/A.scala | 4 ++++ .../constructors-unrelated-2/B.scala | 7 +++++++ .../constructors-unrelated-2/C.scala | 3 +++ .../constructors-unrelated-2/changes/B.scala | 7 +++++++ .../constructors-unrelated-2/changes/C.scala | 3 +++ .../constructors-unrelated-2/test | 8 +++++++ 8 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/A.scala create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/B.scala create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/C.scala create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/B.scala create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/C.scala create mode 100644 zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/test diff --git a/internal/compiler-bridge/src/main/scala/xsbt/ClassName.scala b/internal/compiler-bridge/src/main/scala/xsbt/ClassName.scala index c6f6ad5c8..9445be1fe 100644 --- a/internal/compiler-bridge/src/main/scala/xsbt/ClassName.scala +++ b/internal/compiler-bridge/src/main/scala/xsbt/ClassName.scala @@ -58,6 +58,9 @@ trait ClassName extends Compat { protected def constructorNameAsString(cls: Symbol): String = cls.fullName(';') ++ ";init;" + protected def constructorNameAsString(cls: Symbol, index: String): String = + cls.fullName(';') ++ s";init;default;$index" + /** * Mangle a JVM symbol name in a format better suited for internal uses by sbt. */ diff --git a/internal/compiler-bridge/src/main/scala/xsbt/ExtractAPI.scala b/internal/compiler-bridge/src/main/scala/xsbt/ExtractAPI.scala index 1cef8b220..fb77ed6c4 100644 --- a/internal/compiler-bridge/src/main/scala/xsbt/ExtractAPI.scala +++ b/internal/compiler-bridge/src/main/scala/xsbt/ExtractAPI.scala @@ -327,7 +327,7 @@ class ExtractAPI[GlobalType <: Global]( case returnType => val retType = processType(in, dropConst(returnType)) xsbti.api.Def.of( - simpleName(s), + simpleNameForMethod(s), getAccess(s), getModifiers(s), annotations(in, s), @@ -827,9 +827,22 @@ class ExtractAPI[GlobalType <: Global]( } private def simpleName(s: Symbol): String = { - val n = s.unexpandedName - val n2 = if (n == nme.CONSTRUCTOR) constructorNameAsString(s.enclClass) else n.decode.toString - n2.trim + s.unexpandedName.decode.trim + } + + private def simpleNameForMethod(s: Symbol): String = { + val name = s.unexpandedName + val untrimmedName = if (name == nme.CONSTRUCTOR) + constructorNameAsString(s.enclClass) + else { + val decoded = name.decode + val constructorWithDefaultArgument = "\\$default\\$(\\d+)".r + decoded match { + case constructorWithDefaultArgument(index) => constructorNameAsString(s.enclClass, index) + case _ => decoded + } + } + untrimmedName.trim } private def staticAnnotations(annotations: List[AnnotationInfo]): List[AnnotationInfo] = diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/A.scala b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/A.scala new file mode 100644 index 000000000..c8c893cb6 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/A.scala @@ -0,0 +1,4 @@ +class A { + var z = B.z // introduce a member ref dependency to B + var c: C = new C(1) +} diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/B.scala b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/B.scala new file mode 100644 index 000000000..f31cb9d54 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/B.scala @@ -0,0 +1,7 @@ +class B(val z: Int) { + def this(x: Int, y: Int = 2) = this(x + y) +} + +object B { + val z = 1 +} \ No newline at end of file diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/C.scala b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/C.scala new file mode 100644 index 000000000..041c24187 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/C.scala @@ -0,0 +1,3 @@ +class C(z: String) { + def this(x: Int, y: Int = 2) = this("") +} diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/B.scala b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/B.scala new file mode 100644 index 000000000..9817bfd82 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/B.scala @@ -0,0 +1,7 @@ +class B(val z: Int) { + def this(x: Int, y: String = "") = this(x + y.toInt) +} + +object B { + val z = 1 +} \ No newline at end of file diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/C.scala b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/C.scala new file mode 100644 index 000000000..7db509bb1 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/changes/C.scala @@ -0,0 +1,3 @@ +class C(z: String) { + def this(x: Boolean, y: Int = 2) = this("") +} diff --git a/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/test b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/test new file mode 100644 index 000000000..6b93362f1 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/constructors-unrelated-2/test @@ -0,0 +1,8 @@ +> compile +$ copy-file changes/B.scala B.scala +# Second compilation round, there should be no third round (we don't need to recompile A.scala) +> compile +# Check that there were only two rounds of compilation +> checkIterations 2 +$ copy-file changes/C.scala C.scala +-> compile