Skip to content

Commit b782cbf

Browse files
authored
Fix undercompilation upon ctor change (#19911)
Fixes #19910 Fixes sbt/zinc#1334 ## Problem Scala 3 compiler registers special `zincMangledName` for constructors for the purpose of incremental compilation. Currently the `zincMangledName` contains the package name, which does not match the use site tracking, thereby causing undercompilation during incremental compilation after a ctor change, like adding a parameter. There is an existing scripted test, but coincidentally the test class does NOT include packages, so the test ends up passing. ## Solution 1. This PR reproduces the issue by adding package name to the test. 2. This also fixes the problem by changing the `zincMangedName` to `sym.owner.name ++ ";init;"`. ## Note about zincMangedName `zincMangedName` in general is a good idea, which adds the class name into otherwise common name `<init>`. By making the name more unique it prevents overcompilation when one of the ctors changes in a file.
2 parents 9ef24bf + 0f7de67 commit b782cbf

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import dotty.tools.dotc.core.Contexts.Context
44
import dotty.tools.dotc.core.Symbols.Symbol
55
import dotty.tools.dotc.core.NameOps.stripModuleClassSuffix
66
import dotty.tools.dotc.core.Names.Name
7+
import dotty.tools.dotc.core.Names.termName
78

89
inline val TermNameHash = 1987 // 300th prime
910
inline val TypeNameHash = 1993 // 301st prime
1011
inline val InlineParamHash = 1997 // 302nd prime
1112

1213
extension (sym: Symbol)
1314

14-
def constructorName(using Context) =
15-
sym.owner.fullName ++ ";init;"
16-
17-
/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
18-
def zincMangledName(using Context): Name =
19-
if (sym.isConstructor) constructorName
20-
else sym.name.stripModuleClassSuffix
15+
/** Mangle a JVM symbol name in a format better suited for internal uses by sbt.
16+
* WARNING: output must not be written to TASTy, as it is not a valid TASTy name.
17+
*/
18+
private[sbt] def zincMangledName(using Context): Name =
19+
if sym.isConstructor then
20+
// TODO: ideally we should avoid unnecessarily caching these Zinc specific
21+
// names in the global chars array. But we would need to restructure
22+
// ExtractDependencies caches to avoid expensive `toString` on
23+
// each member reference.
24+
termName(sym.owner.fullName.mangledString.replace(".", ";").nn ++ ";init;")
25+
else
26+
sym.name.stripModuleClassSuffix

sbt-bridge/test/xsbt/ExtractUsedNamesSpecification.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class ExtractUsedNamesSpecification {
306306
// All classes extend Object
307307
"Object",
308308
// All classes have a default constructor called <init>
309-
"java.lang.Object;init;",
309+
"java;lang;Object;init;",
310310
// the return type of the default constructor is Unit
311311
"Unit"
312312
)
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
package example
2+
13
class A(a: Int)
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
package example
2+
13
class B { val y = new A(2) }
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
package example
2+
13
class A(a: String)
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
package example
2+
13
class B { val y = new A("a") }

0 commit comments

Comments
 (0)