Skip to content

Commit 399b4af

Browse files
authored
Merge pull request #15307 from gagandeepkalra/9795
Fix warning with leaking `this` into Scala2 synthetic apply
2 parents 78dbc59 + 3f60bfc commit 399b4af

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,11 +615,11 @@ object Semantic {
615615

616616
def checkArgsWithParametricity() =
617617
val methodType = atPhaseBeforeTransforms { meth.info.stripPoly }
618-
var allArgsPromote = true
618+
var allArgsHot = true
619619
val allParamTypes = methodType.paramInfoss.flatten.map(_.repeatedToSingle)
620620
val errors = allParamTypes.zip(args).flatMap { (info, arg) =>
621621
val errors = Reporter.errorsIn { arg.promote }
622-
allArgsPromote = allArgsPromote && errors.isEmpty
622+
allArgsHot = allArgsHot && errors.isEmpty
623623
info match
624624
case typeParamRef: TypeParamRef =>
625625
val bounds = typeParamRef.underlying.bounds
@@ -632,21 +632,21 @@ object Semantic {
632632
if isWithinBounds && !otherParamContains then Nil else errors
633633
case _ => errors
634634
}
635-
(errors, allArgsPromote)
635+
(errors, allArgsHot)
636636

637637
// fast track if the current object is already initialized
638638
if promoted.isCurrentObjectPromoted then Hot
639639
else if isAlwaysSafe(meth) then Hot
640640
else if meth eq defn.Any_asInstanceOf then value
641641
else value match {
642642
case Hot =>
643-
if isSyntheticApply(meth) then
643+
if isSyntheticApply(meth) && meth.hasSource then
644644
val klass = meth.owner.companionClass.asClass
645645
instantiate(klass, klass.primaryConstructor, args)
646646
else
647647
if receiver.typeSymbol.isStaticOwner then
648-
val (errors, allArgsPromote) = checkArgsWithParametricity()
649-
if allArgsPromote then
648+
val (errors, allArgsHot) = checkArgsWithParametricity()
649+
if allArgsHot then
650650
Hot: Value
651651
else if errors.nonEmpty then
652652
reporter.reportAll(errors)

tests/init/neg/some-this.scala

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/init/pos/i9795/A.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class A:
2+
// Safe initialization check only allows capturing `this` either through primary constructor or synthetic `apply`
3+
// `Some` case class comes from Scala 2 stdlib, which is not visible, hence the warning
4+
// For reference:
5+
// https://github.com/lampepfl/dotty/pull/12711
6+
// https://github.com/lampepfl/dotty/pull/14283
7+
val some = Some(this)

0 commit comments

Comments
 (0)