Skip to content

Commit 84e818a

Browse files
committed
Treat implied imports differently
Implied imports can only import implied definitions. Regular imports can only import non-implied definitions.
1 parent 187f6a2 commit 84e818a

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Scopes._
1313
import Uniques._
1414
import ast.Trees._
1515
import ast.untpd
16+
import Flags.ImplicitOrImplied
1617
import util.{FreshNameCreator, NoSource, SimpleIdentityMap, SourceFile}
1718
import typer.{Implicits, ImportInfo, Inliner, NamerContextOps, SearchHistory, SearchRoot, TypeAssigner, Typer}
1819
import Implicits.ContextualImplicits
@@ -214,7 +215,7 @@ object Contexts {
214215
implicitsCache = {
215216
val implicitRefs: List[ImplicitRef] =
216217
if (isClassDefContext)
217-
try owner.thisType.implicitMembers
218+
try owner.thisType.implicitMembers(ImplicitOrImplied)
218219
catch {
219220
case ex: CyclicReference => Nil
220221
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ object Flags {
130130
*/
131131
case class FlagConjunction(bits: Long) {
132132
override def toString: String = FlagSet(bits).toString
133+
def | (fs: FlagSet): FlagConjunction = FlagConjunction((FlagSet(bits) | fs).bits)
133134
}
134135

135136
def termFlagConjunction(x: Long) = FlagConjunction(TERMS | x)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,13 @@ object Types {
787787
(name, buf) => buf += member(name).asSingleDenotation)
788788
}
789789

790-
/** The set of implicit term members of this type */
791-
final def implicitMembers(implicit ctx: Context): List[TermRef] = track("implicitMembers") {
790+
/** The set of implicit term members of this type
791+
* @param kind A subset of {Implicit, Implied} that sepcifies what kind of implicit should
792+
* be returned
793+
*/
794+
final def implicitMembers(kind: FlagSet)(implicit ctx: Context): List[TermRef] = track("implicitMembers") {
792795
memberDenots(implicitFilter,
793-
(name, buf) => buf ++= member(name).altsWith(_.is(ImplicitOrImpliedTerm)))
796+
(name, buf) => buf ++= member(name).altsWith(_.is(ImplicitOrImpliedTerm & kind)))
794797
.toList.map(d => TermRef(this, d.symbol.asTerm))
795798
}
796799

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ object Implicits {
230230
assert(initctx.typer != null)
231231
lazy val refs: List[ImplicitRef] = {
232232
val buf = new mutable.ListBuffer[TermRef]
233-
for (companion <- companionRefs) buf ++= companion.implicitMembers
233+
for (companion <- companionRefs) buf ++= companion.implicitMembers(ImplicitOrImplied)
234234
buf.toList
235235
}
236236

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ class ImportInfo(symf: Context => Symbol, val selectors: List[untpd.Tree],
9595
recur(selectors)
9696
}
9797

98+
private def implicitFlag = if (impliedOnly) Implied else Implicit
99+
98100
/** The implicit references imported by this import clause */
99101
def importedImplicits(implicit ctx: Context): List[ImplicitRef] = {
100102
val pre = site
101103
if (isWildcardImport) {
102-
val refs = pre.implicitMembers
104+
val refs = pre.implicitMembers(implicitFlag)
103105
if (excluded.isEmpty) refs
104106
else refs filterNot (ref => excluded contains ref.name.toTermName)
105107
} else
106108
for {
107109
renamed <- reverseMapping.keys
108-
denot <- pre.member(reverseMapping(renamed)).altsWith(_ is ImplicitOrImplied)
110+
denot <- pre.member(reverseMapping(renamed)).altsWith(_ is implicitFlag)
109111
} yield {
110112
val original = reverseMapping(renamed)
111113
val ref = TermRef(pre, original, denot)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ class Typer extends Namer
182182
NoType
183183
else {
184184
val pre = imp.site
185-
val denot = pre.memberBasedOnFlags(name, required, EmptyFlags).accessibleFrom(pre)(refctx)
185+
var reqd = required
186+
var excl = EmptyFlags
187+
if (imp.impliedOnly) reqd |= Implied else excl |= Implied
188+
val denot = pre.memberBasedOnFlags(name, reqd, excl).accessibleFrom(pre)(refctx)
186189
// Pass refctx so that any errors are reported in the context of the
187190
// reference instead of the
188191
if (reallyExists(denot)) pre.select(name, denot) else NoType

0 commit comments

Comments
 (0)