@@ -340,7 +340,19 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre
340
340
else (true , adaptToType(tree, expectedTpe))
341
341
}
342
342
343
+ def adaptAndPostErase (tree : Tree , pt : Type ): (Boolean , Tree ) = {
344
+ val (needsAdapt, adaptedTree) = adapt(tree, pt)
345
+ val trans = postErasure.newTransformer(unit)
346
+ val postErasedTree = trans.atOwner(currentOwner)(trans.transform(adaptedTree)) // SI-8017 elimnates ErasedValueTypes
347
+ (needsAdapt, postErasedTree)
348
+ }
349
+
343
350
enteringPhase(currentRun.posterasurePhase) {
351
+ // e.g, in:
352
+ // class C(val a: Int) extends AnyVal; (x: Int) => new C(x)
353
+ //
354
+ // This type is:
355
+ // (x: Int)ErasedValueType(class C, Int)
344
356
val liftedBodyDefTpe : MethodType = {
345
357
val liftedBodySymbol = {
346
358
val Apply (method, _) = originalFunction.body
@@ -349,8 +361,14 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre
349
361
liftedBodySymbol.info.asInstanceOf [MethodType ]
350
362
}
351
363
val (paramNeedsAdaptation, adaptedParams) = (bridgeSyms zip liftedBodyDefTpe.params map {case (bridgeSym, param) => adapt(Ident (bridgeSym) setType bridgeSym.tpe, param.tpe)}).unzip
352
- val body = Apply (gen.mkAttributedSelect(gen.mkAttributedThis(newClass), applyMethod.symbol), adaptedParams) setType applyMethod.symbol.tpe.resultType
353
- val (needsReturnAdaptation, adaptedBody) = adapt(typer.typed(body), ObjectTpe )
364
+ // SI-8017 Before, this code used `applyMethod.symbol.info.resultType`.
365
+ // But that symbol doesn't have a type history that goes back before `delambdafy`,
366
+ // so we just see a plain `Int`, rather than `ErasedValueType(C, Int)`.
367
+ // This triggered primitive boxing, rather than value class boxing.
368
+ val resTp = liftedBodyDefTpe.finalResultType
369
+ val body = Apply (gen.mkAttributedSelect(gen.mkAttributedThis(newClass), applyMethod.symbol), adaptedParams) setType resTp
370
+ val (needsReturnAdaptation, adaptedBody) = adaptAndPostErase(body, ObjectTpe )
371
+
354
372
val needsBridge = (paramNeedsAdaptation contains true ) || needsReturnAdaptation
355
373
if (needsBridge) {
356
374
val methDef = DefDef (bridgeMethSym, List (bridgeParams), adaptedBody)
0 commit comments