Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning with leaking this into Scala2 synthetic apply #15307

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ object Semantic {

def checkArgsWithParametricity() =
val methodType = atPhaseBeforeTransforms { meth.info.stripPoly }
var allArgsPromote = true
var allArgsHot = true
val allParamTypes = methodType.paramInfoss.flatten.map(_.repeatedToSingle)
val errors = allParamTypes.zip(args).flatMap { (info, arg) =>
val errors = Reporter.errorsIn { arg.promote }
allArgsPromote = allArgsPromote && errors.isEmpty
allArgsHot = allArgsHot && errors.isEmpty
info match
case typeParamRef: TypeParamRef =>
val bounds = typeParamRef.underlying.bounds
Expand All @@ -632,21 +632,21 @@ object Semantic {
if isWithinBounds && !otherParamContains then Nil else errors
case _ => errors
}
(errors, allArgsPromote)
(errors, allArgsHot)

// fast track if the current object is already initialized
if promoted.isCurrentObjectPromoted then Hot
else if isAlwaysSafe(meth) then Hot
else if meth eq defn.Any_asInstanceOf then value
else value match {
case Hot =>
if isSyntheticApply(meth) then
if isSyntheticApply(meth) && meth.hasSource then
val klass = meth.owner.companionClass.asClass
instantiate(klass, klass.primaryConstructor, args)
else
if receiver.typeSymbol.isStaticOwner then
val (errors, allArgsPromote) = checkArgsWithParametricity()
if allArgsPromote then
val (errors, allArgsHot) = checkArgsWithParametricity()
if allArgsHot then
Hot: Value
else if errors.nonEmpty then
reporter.reportAll(errors)
Expand Down
2 changes: 0 additions & 2 deletions tests/init/neg/some-this.scala

This file was deleted.

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