Skip to content

Commit 5153062

Browse files
committed
Some refinements
1 parent 9c67ee4 commit 5153062

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ class Definitions {
210210
}
211211
@tu lazy val ScalaPackageObject: Symbol = ctx.requiredModule("scala.package")
212212
@tu lazy val JavaPackageVal: TermSymbol = ctx.requiredPackage(nme.java)
213+
@tu lazy val JavaPackageClass: ClassSymbol = JavaPackageVal.moduleClass.asClass
213214
@tu lazy val JavaLangPackageVal: TermSymbol = ctx.requiredPackage(jnme.JavaLang)
215+
@tu lazy val JavaLangPackageClass: ClassSymbol = JavaLangPackageVal.moduleClass.asClass
214216

215217
// fundamental modules
216218
@tu lazy val SysPackage : Symbol = ctx.requiredModule("scala.sys.package")

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -490,57 +490,51 @@ object Implicits {
490490
* the package was accessed in some way previously.
491491
*/
492492
class suggestions(qualifies: TermRef => Boolean) with
493-
private type RootRef = TermRef | ThisType
494-
495-
private def symbolOf(ref: RootRef)(given Context) = ref match
496-
case ref: TermRef => ref.symbol
497-
case ref: ThisType => ref.cls
498-
499493
private val seen = mutable.Set[TermRef]()
500494

501-
private def lookInside(root: Symbol)(given ctx: Context): Boolean =
495+
private def lookInside(root: Symbol)(given Context): Boolean =
502496
if root.is(Package) then root.isTerm && root.isCompleted
503497
else !root.name.is(FlatName)
504498
&& !root.name.lastPart.contains('$')
505499
&& root.is(ModuleVal, butNot = JavaDefined)
506500

507-
def nestedRoots(ref: RootRef)(given Context): List[Symbol] =
501+
def nestedRoots(site: Type)(given Context): List[Symbol] =
508502
val seenNames = mutable.Set[Name]()
509-
ref.widen.baseClasses.flatMap { bc =>
503+
site.baseClasses.flatMap { bc =>
510504
bc.info.decls.filter { dcl =>
511505
lookInside(dcl)
512506
&& !seenNames.contains(dcl.name)
513507
&& { seenNames += dcl.name; true }
514508
}
515509
}
516510

517-
private def rootsStrictlyIn(ref: RootRef)(given ctx: Context): List[TermRef] =
518-
val refSym = symbolOf(ref)
511+
private def rootsStrictlyIn(ref: Type)(given Context): List[TermRef] =
512+
val site = ref.widen
513+
val refSym = site.typeSymbol
519514
val nested =
520-
if refSym == defn.EmptyPackageClass // Don't search the empty package, either as enclosing package ...
521-
|| refSym == defn.EmptyPackageVal // ... or as a member of _root_.
522-
|| refSym == defn.JavaPackageVal // As an optimization, don't search java...
523-
|| refSym == defn.JavaLangPackageVal // ... or java.lang.
524-
then Nil
525-
else if refSym.is(Package) || refSym.isPackageObject then
526-
refSym.info.decls.filter(lookInside)
515+
if refSym.is(Package) then
516+
if refSym == defn.EmptyPackageClass // Don't search the empty package
517+
|| refSym == defn.JavaPackageClass // As an optimization, don't search java...
518+
|| refSym == defn.JavaLangPackageClass // ... or java.lang.
519+
then Nil
520+
else refSym.info.decls.filter(lookInside)
527521
else
528522
if !refSym.is(Touched) then refSym.ensureCompleted() // JavaDefined is reliably known only after completion
529523
if refSym.is(JavaDefined) then Nil
530-
else nestedRoots(ref)
524+
else nestedRoots(site)
531525
nested
532526
.map(mbr => TermRef(ref, mbr.asTerm))
533527
.flatMap(rootsIn)
534528
.toList
535529

536-
private def rootsIn(ref: TermRef)(given ctx: Context): List[TermRef] =
530+
private def rootsIn(ref: TermRef)(given Context): List[TermRef] =
537531
if seen.contains(ref) then Nil
538532
else
539533
implicits.println(i"search for suggestions in ${ref.symbol.fullName}")
540534
seen += ref
541535
ref :: rootsStrictlyIn(ref)
542536

543-
private def rootsOnPath(tp: Type)(given ctx: Context): List[TermRef] = tp match
537+
private def rootsOnPath(tp: Type)(given Context): List[TermRef] = tp match
544538
case ref: TermRef => rootsIn(ref) ::: rootsOnPath(ref.prefix)
545539
case _ => Nil
546540

@@ -549,15 +543,12 @@ object Implicits {
549543
val defined =
550544
if ctx.owner.isClass then
551545
if ctx.owner eq ctx.outer.owner then Nil
552-
else ctx.owner.thisType match
553-
case ref: TermRef => rootsStrictlyIn(ref)
554-
case ref: ThisType => rootsStrictlyIn(ref)
555-
case _ => Nil
556-
else if ctx.scope eq ctx.outer.scope then Nil
546+
else rootsStrictlyIn(ctx.owner.thisType)
557547
else
558-
ctx.scope
559-
.filter(lookInside(_))
560-
.flatMap(sym => rootsIn(sym.termRef))
548+
if ctx.scope eq ctx.outer.scope then Nil
549+
else ctx.scope
550+
.filter(lookInside(_))
551+
.flatMap(sym => rootsIn(sym.termRef))
561552
val imported =
562553
if ctx.importInfo eq ctx.outer.importInfo then Nil
563554
else ctx.importInfo.sym.info match
@@ -593,7 +584,7 @@ object Implicits {
593584
catch
594585
case ex: Throwable =>
595586
if ctx.settings.Ydebug.value then
596-
println("caught exceptioon when searching for suggestions")
587+
println("caught exception when searching for suggestions")
597588
ex.printStackTrace()
598589
Nil
599590
finally timer.cancel()

0 commit comments

Comments
 (0)