Skip to content

Commit f72b800

Browse files
committed
Do singleton widening when migrating to Scala 3.
The previous changes failed the community build for squants. What happened was that an implicit in the package object of squants was not found in motion/AngularAcceleration. Normally, the context object would be found in the normal implicit context of the use site. But AngularAcceleration was defined as ```scala package squants.motion ``` which meant that `squants` by itself was not in the implicit scope. If it had been defined like this: ```scala package squants package motion ``` it would have worked fine. So the implicit hit it relied on the fact that under -source:3.0-migration we also search package prefixes of types. Only, I believe there were no such prefixes normally since the types in question started with `this[motion]` so no mention of `squants`. But if we widen the term refs on the paths then we mention something that mentions in turn `scala` and things are OK. I did not track down what this was, but that's what must have happened. So, to keep compatibility with old code, we should still enable this under -source:3.0-migration.
1 parent 6b7b9d2 commit f72b800

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import transform.TypeUtils._
3131
import Hashable._
3232
import util.{SourceFile, NoSource}
3333
import config.{Config, Feature}
34+
import Feature.migrateTo3
3435
import config.Printers.{implicits, implicitsDetailed}
3536
import collection.mutable
3637
import reporting.trace
@@ -153,7 +154,7 @@ object Implicits {
153154
//
154155
// We keep the old behavior under -source 3.0-migration.
155156
val isFunctionInS2 =
156-
Feature.migrateTo3
157+
migrateTo3
157158
&& tpw.derivesFrom(defn.FunctionClass(1))
158159
&& ref.symbol != defn.Predef_conforms
159160
val isImplicitConversion = tpw.derivesFrom(defn.ConversionClass)
@@ -278,7 +279,7 @@ object Implicits {
278279
*/
279280
override val level: Int =
280281
if outerImplicits == null then 1
281-
else if Feature.migrateTo3(using irefCtx)
282+
else if migrateTo3(using irefCtx)
282283
|| (irefCtx.owner eq outerImplicits.irefCtx.owner)
283284
&& (irefCtx.scope eq outerImplicits.irefCtx.scope)
284285
&& !refs.head.implicitName.is(LazyImplicitName)
@@ -512,7 +513,7 @@ trait ImplicitRunInfo {
512513
* opaque type aliases, and abstract types, but not type parameters or package objects.
513514
*/
514515
def isAnchor(sym: Symbol) =
515-
sym.isClass && !sym.is(Package) && (!sym.isPackageObject || Feature.migrateTo3)
516+
sym.isClass && !sym.is(Package) && (!sym.isPackageObject || migrateTo3)
516517
|| sym.isOpaqueAlias
517518
|| sym.is(Deferred, butNot = Param)
518519

@@ -581,10 +582,10 @@ trait ImplicitRunInfo {
581582
addPath(pre.cls.sourceModule.termRef)
582583
case pre: TermRef =>
583584
if pre.symbol.is(Package) then
584-
if Feature.migrateTo3 then
585+
if migrateTo3 then
585586
addCompanion(pre, pre.member(nme.PACKAGE).symbol)
586587
addPath(pre.prefix)
587-
else if !pre.symbol.isPackageObject || Feature.migrateTo3 then
588+
else if !pre.symbol.isPackageObject || migrateTo3 then
588589
comps += pre
589590
addPath(pre.prefix)
590591
case _ =>
@@ -604,7 +605,8 @@ trait ImplicitRunInfo {
604605
val superAnchors = if (sym.isClass) tp.parents else anchors(tp.superType)
605606
for (anchor <- superAnchors) comps ++= iscopeRefs(anchor)
606607
case tp =>
607-
for part <- tp.namedPartsWith(_.isType) do comps ++= iscopeRefs(part)
608+
for part <- tp.namedPartsWith(_.isType, widenSingletons = migrateTo3) do
609+
comps ++= iscopeRefs(part)
608610
}
609611
comps
610612
}
@@ -908,7 +910,7 @@ trait Implicits { self: Typer =>
908910
case result: SearchFailure if result.isAmbiguous =>
909911
val deepPt = pt.deepenProto
910912
if (deepPt ne pt) inferImplicit(deepPt, argument, span)
911-
else if (Feature.migrateTo3 && !ctx.mode.is(Mode.OldOverloadingResolution))
913+
else if (migrateTo3 && !ctx.mode.is(Mode.OldOverloadingResolution))
912914
inferImplicit(pt, argument, span)(using ctx.addMode(Mode.OldOverloadingResolution)) match {
913915
case altResult: SearchSuccess =>
914916
ctx.migrationWarning(
@@ -1104,7 +1106,7 @@ trait Implicits { self: Typer =>
11041106
negateIfNot(tryImplicit(cand, contextual)) match {
11051107
case fail: SearchFailure =>
11061108
if (fail.isAmbiguous)
1107-
if Feature.migrateTo3 then
1109+
if migrateTo3 then
11081110
val result = rank(remaining, found, NoMatchingImplicitsFailure :: rfailures)
11091111
if (result.isSuccess)
11101112
warnAmbiguousNegation(fail.reason.asInstanceOf[AmbiguousImplicits])

0 commit comments

Comments
 (0)