Skip to content

Commit 6da4d4a

Browse files
committed
Align unpickled Scala 2 accessors encoding with Scala 3
Adapt the flags of getters so they become like vals/vars instead. The getters flags and info are modified. The private fields for case accessors are not unpickled anymore. We need these getters to be aligned with Scala 3 if we want to be able to use the Scala 2 library TASTy. Otherwise library classfiles would not behave in the same way as their TASTy counterpart. This change may cause some macros to fail if they relied on the old style accessors. This should be adapted to work with the old and new representation. We observed that such a change is needed in `verify`, other might be detected with the open community build.
1 parent 032f6cd commit 6da4d4a

File tree

10 files changed

+32
-20
lines changed

10 files changed

+32
-20
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ jobs:
143143
./project/scripts/sbt ";sjsSandbox/run ;sjsSandbox/test ;sjsJUnitTests/test ;set sjsJUnitTests/scalaJSLinkerConfig ~= switchToESModules ;sjsJUnitTests/test ;sjsCompilerTests/test"
144144
145145
- name: Test with Scala 2 library TASTy
146-
run: ./project/scripts/sbt ";set ThisBuild/Build.useScala2LibraryTasty := true ;scala3-bootstrapped/testCompilation i5" # only test a subset of test to avoid doubling the CI execution time
146+
run: ./project/scripts/sbt ";set ThisBuild/Build.useScala2LibraryTasty := true ;scala3-bootstrapped/testCompilation i5; scala3-bootstrapped/testCompilation tests/run/typelevel-peano.scala" # only test a subset of test to avoid doubling the CI execution time
147147

148148
test_windows_fast:
149149
runs-on: [self-hosted, Windows]

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,17 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
450450
// Scala 2 sometimes pickle the same type parameter symbol multiple times
451451
// (see i11173 for an example), but we should only unpickle it once.
452452
|| tag == TYPEsym && flags.is(TypeParam) && symScope(owner).lookup(name.asTypeName).exists
453+
// We discard the private val representing a case accessor. We only load the case accessor def.
454+
|| flags.isAllOf(CaseAccessor| PrivateLocal, butNot = Method)
453455
then
454456
// skip this member
455457
return NoSymbol
456458

459+
// Adapt the flags of getters so they become like vals/vars instead
460+
if flags.isAllOf(Method | Accessor) && !name.toString().endsWith("_$eq") then
461+
flags &~= Method | Accessor
462+
if !flags.is(StableRealizable) then flags |= Mutable
463+
457464
name = name.adjustIfModuleClass(flags)
458465
if (flags.is(Method))
459466
name =
@@ -618,7 +625,12 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
618625
setClassInfo(denot, tp, fromScala2 = true, selfInfo)
619626
NamerOps.addConstructorProxies(denot.classSymbol)
620627
case denot =>
621-
val tp1 = translateTempPoly(tp)
628+
val tp0 = translateTempPoly(tp)
629+
val tp1 =
630+
if !denot.is(Param) && !denot.is(Method) && tp0.isInstanceOf[ExprType] then
631+
tp0.asInstanceOf[ExprType].resultType
632+
else tp0
633+
622634
denot.info =
623635
if (tag == ALIASsym) TypeAlias(tp1)
624636
else if (denot.isType) checkNonCyclic(denot.symbol, tp1, reportErrors = false)

compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class InlineReducer(inliner: Inliner)(using Context):
329329
val paramCls = paramType.classSymbol
330330
if (paramCls.is(Case) && unapp.symbol.is(Synthetic) && scrut <:< paramType) {
331331
val caseAccessors =
332-
if (paramCls.is(Scala2x)) paramCls.caseAccessors.filter(_.is(Method))
332+
if paramCls.is(Scala2x) then paramCls.caseAccessors
333333
else paramCls.asClass.paramAccessors
334334
val selectors =
335335
for (accessor <- caseAccessors)

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ object PatternMatcher {
327327
/** Plan for matching the result of an unapply against argument patterns `args` */
328328
def unapplyPlan(unapp: Tree, args: List[Tree]): Plan = {
329329
def caseClass = unapp.symbol.owner.linkedClass
330-
lazy val caseAccessors = caseClass.caseAccessors.filter(_.is(Method))
330+
lazy val caseAccessors = caseClass.caseAccessors
331331

332332
def isSyntheticScala2Unapply(sym: Symbol) =
333333
sym.isAllOf(SyntheticCase) && sym.owner.is(Scala2x)

compiler/src/dotty/tools/dotc/typer/Synthesizer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
409409
New(defn.RuntimeTupleMirrorTypeRef, Literal(Constant(arity)) :: Nil)
410410

411411
def makeProductMirror(pre: Type, cls: Symbol, tps: Option[List[Type]]): TreeWithErrors =
412-
val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal))
412+
val accessors = cls.caseAccessors
413413
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
414414
val typeElems = tps.getOrElse(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
415415
val nestedPairs = TypeOps.nestedPairs(typeElems)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionDocSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class CompletionDocSuite extends BaseCompletionSuite:
214214
""".stripMargin,
215215
"""
216216
|> Found documentation for scala/package.Vector.
217-
|Vector scala.collection.immutable
217+
|Vector: scala.collection.immutable
218218
|""".stripMargin,
219219
includeDocs = true
220220
)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CompletionSuite extends BaseCompletionSuite:
2525
| Lis@@
2626
|}""".stripMargin,
2727
"""
28-
|List scala.collection.immutable
28+
|List: scala.collection.immutable
2929
|List - java.awt
3030
|List - java.util
3131
|List - scala.collection.immutable
@@ -647,7 +647,7 @@ class CompletionSuite extends BaseCompletionSuite:
647647
|}
648648
|""".stripMargin,
649649
"""|None scala
650-
|NoManifest scala.reflect
650+
|NoManifest: scala.reflect
651651
|""".stripMargin,
652652
topLines = Some(2)
653653
)
@@ -660,8 +660,8 @@ class CompletionSuite extends BaseCompletionSuite:
660660
|}
661661
|""".stripMargin,
662662
"""|Some(value) scala
663-
|Seq scala.collection.immutable
664-
|Set scala.collection.immutable
663+
|Seq: scala.collection.immutable
664+
|Set: scala.collection.immutable
665665
|""".stripMargin,
666666
topLines = Some(3)
667667
)

presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
194194
s"""|package <<example>>/*namespace*/
195195
|
196196
|object <<A>>/*class*/ {
197-
| val <<x>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
197+
| val <<x>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,2,3)
198198
| val <<s>>/*variable,definition,readonly*/ = <<Some>>/*class*/(1)
199199
| val <<Some>>/*class*/(<<s1>>/*variable,definition,readonly*/) = <<s>>/*variable,readonly*/
200200
| val <<Some>>/*class*/(<<s2>>/*variable,definition,readonly*/) = <<s>>/*variable,readonly*/
@@ -269,7 +269,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
269269
|object <<A>>/*class*/ {
270270
| val <<a>>/*variable,definition,readonly*/ = 1
271271
| var <<b>>/*variable,definition*/ = 2
272-
| val <<c>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,<<a>>/*variable,readonly*/,<<b>>/*variable*/)
272+
| val <<c>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,<<a>>/*variable,readonly*/,<<b>>/*variable*/)
273273
| <<b>>/*variable*/ = <<a>>/*variable,readonly*/
274274
|""".stripMargin
275275
)
@@ -278,10 +278,10 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
278278
check(
279279
"""
280280
|object <<Main>>/*class*/ {
281-
| val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
282-
| val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*class*/(1,2)
283-
| val <<z>>/*variable,definition,readonly*/ = <<Set>>/*class*/(1,2,3)
284-
| val <<w>>/*variable,definition,readonly*/ = <<Right>>/*class*/(1)
281+
|val <<a>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,2,3)
282+
|val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*variable,readonly*/(1,2)
283+
|val <<z>>/*variable,definition,readonly*/ = <<Set>>/*variable,readonly*/(1,2,3)
284+
|val <<w>>/*variable,definition,readonly*/ = <<Right>>/*variable,readonly*/(1)
285285
|}""".stripMargin
286286
)
287287

@@ -326,7 +326,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
326326
|
327327
|object <<B>>/*class*/ {
328328
| val <<a>>/*variable,definition,readonly*/ = for {
329-
| <<foo>>/*variable,definition,readonly*/ <- <<List>>/*class*/("a", "b", "c")
329+
| <<foo>>/*variable,definition,readonly*/ <- <<List>>/*variable,readonly*/("a", "b", "c")
330330
| <<_>>/*class,abstract*/ = <<println>>/*method*/("print!")
331331
| } yield <<foo>>/*variable,readonly*/
332332
|}

tests/neg/constructor-proxy-shadowing.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
17 |val x = Seq(3) // error: shadowing
5353
| ^^^
5454
| Reference to constructor proxy for class Seq
55-
| shadows outer reference to getter Seq in package scala
55+
| shadows outer reference to value Seq in package scala
5656
|
5757
| The instance needs to be created with an explicit `new`.
5858
|--------------------------------------------------------------------------------------------------------------------
@@ -66,7 +66,7 @@
6666
|
6767
| new Seq(...)
6868
|
69-
| Or it could mean calling the apply method of getter Seq in package scala as in
69+
| Or it could mean calling the apply method of value Seq in package scala as in
7070
|
7171
| Seq.apply(...)
7272
|

0 commit comments

Comments
 (0)