Skip to content

Commit d04b3c7

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 d04b3c7

File tree

23 files changed

+122
-398
lines changed

23 files changed

+122
-398
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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,18 @@ 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+
// The info of this symbol is adapted in the `LocalUnpickler`.
461+
if flags.isAllOf(Method | Accessor) && !name.toString().endsWith("_$eq") then
462+
flags &~= Method | Accessor
463+
if !flags.is(StableRealizable) then flags |= Mutable
464+
457465
name = name.adjustIfModuleClass(flags)
458466
if (flags.is(Method))
459467
name =
@@ -618,7 +626,14 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
618626
setClassInfo(denot, tp, fromScala2 = true, selfInfo)
619627
NamerOps.addConstructorProxies(denot.classSymbol)
620628
case denot =>
621-
val tp1 = translateTempPoly(tp)
629+
val tp1 = translateTempPoly(tp) match
630+
case ExprType(resultType) if !denot.isOneOf(Param | Method) =>
631+
// Adapt the flags of getters so they become like vals/vars instead.
632+
// This is the `def` of an accessor that needs to be transformed into
633+
// a `val`/`var`. Note that the `Method | Accessor` flags were already
634+
// striped away in `readDisambiguatedSymbol`.
635+
resultType
636+
case tp1 => tp1
622637
denot.info =
623638
if (tag == ALIASsym) TypeAlias(tp1)
624639
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)

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CompletionTest {
3939
code"class Foo { val foo: BigD${m1} }"
4040
.completion(
4141
("BigDecimal", Field, "BigDecimal"),
42-
("BigDecimal", Method, "=> scala.math.BigDecimal.type"),
42+
("BigDecimal", Field, "scala.math.BigDecimal"),
4343
)
4444
}
4545

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/coverage/pos/For.scoverage.check

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,6 @@ covtest
262262
For$package$
263263
Object
264264
covtest.For$package$
265-
testForeach
266-
301
267-
304
268-
13
269-
Nil
270-
Ident
271-
false
272-
0
273-
false
274-
Nil
275-
276-
15
277-
For.scala
278-
covtest
279-
For$package$
280-
Object
281-
covtest.For$package$
282265
$anonfun
283266
318
284267
343
@@ -290,7 +273,7 @@ false
290273
false
291274
println("user code here")
292275

293-
16
276+
15
294277
For.scala
295278
covtest
296279
For$package$

tests/coverage/pos/Inlined.scoverage.check

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,6 @@ Object
9494
covtest.Inlined$package$
9595
testInlined
9696
155
97-
159
98-
6
99-
List
100-
Ident
101-
false
102-
0
103-
false
104-
List
105-
106-
5
107-
Inlined.scala
108-
covtest
109-
Inlined$package$
110-
Object
111-
covtest.Inlined$package$
112-
testInlined
113-
155
11497
169
11598
6
11699
length
@@ -120,7 +103,7 @@ false
120103
false
121104
List(l).length
122105

123-
6
106+
5
124107
Inlined.scala
125108
covtest
126109
Inlined$package$
@@ -137,7 +120,7 @@ false
137120
false
138121
scala.runtime.Scala3RunTime.assertFailed()
139122

140-
7
123+
6
141124
Inlined.scala
142125
covtest
143126
Inlined$package$
@@ -154,7 +137,7 @@ true
154137
false
155138
scala.runtime.Scala3RunTime.assertFailed()
156139

157-
8
140+
7
158141
Inlined.scala
159142
covtest
160143
Inlined$package$
@@ -171,7 +154,7 @@ true
171154
false
172155

173156

174-
9
157+
8
175158
Inlined.scala
176159
covtest
177160
Inlined$package$
@@ -188,24 +171,7 @@ false
188171
false
189172
List(l)
190173

191-
10
192-
Inlined.scala
193-
covtest
194-
Inlined$package$
195-
Object
196-
covtest.Inlined$package$
197-
testInlined
198-
180
199-
184
200-
7
201-
List
202-
Ident
203-
false
204-
0
205-
false
206-
List
207-
208-
11
174+
9
209175
Inlined.scala
210176
covtest
211177
Inlined$package$
@@ -222,7 +188,7 @@ false
222188
false
223189
List(l).length
224190

225-
12
191+
10
226192
Inlined.scala
227193
covtest
228194
Inlined$package$
@@ -239,7 +205,7 @@ false
239205
false
240206
scala.runtime.Scala3RunTime.assertFailed()
241207

242-
13
208+
11
243209
Inlined.scala
244210
covtest
245211
Inlined$package$
@@ -256,7 +222,7 @@ true
256222
false
257223
scala.runtime.Scala3RunTime.assertFailed()
258224

259-
14
225+
12
260226
Inlined.scala
261227
covtest
262228
Inlined$package$
@@ -273,7 +239,7 @@ true
273239
false
274240

275241

276-
15
242+
13
277243
Inlined.scala
278244
covtest
279245
Inlined$package$

0 commit comments

Comments
 (0)