@@ -437,20 +437,6 @@ object Implicits {
437
437
em " ${err.refStr(ref)} does not $qualify"
438
438
}
439
439
440
- class ShadowedImplicit (ref : TermRef ,
441
- shadowing : Type ,
442
- val expectedType : Type ,
443
- val argument : Tree ) extends SearchFailureType {
444
- /** same as err.refStr but always prints owner even if it is a term */
445
- def show (ref : Type )(implicit ctx : Context ): String = ref match {
446
- case ref : NamedType if ref.symbol.maybeOwner.isTerm =>
447
- i " ${ref.symbol} in ${ref.symbol.owner}"
448
- case _ => err.refStr(ref)
449
- }
450
- def explanation (implicit ctx : Context ): String =
451
- em " ${show(ref)} does $qualify but it is shadowed by ${show(shadowing)}"
452
- }
453
-
454
440
class DivergingImplicit (ref : TermRef ,
455
441
val expectedType : Type ,
456
442
val argument : Tree ) extends SearchFailureType {
@@ -534,27 +520,32 @@ trait ImplicitRunInfo { self: Run =>
534
520
val comps = new TermRefSet
535
521
tp match {
536
522
case tp : NamedType =>
537
- val pre = tp.prefix
538
- comps ++= iscopeRefs(pre)
539
- def addRef (companion : TermRef ): Unit = {
540
- val compSym = companion.symbol
541
- if (compSym is Package )
542
- addRef(companion.select(nme.PACKAGE ))
543
- else if (compSym.exists)
544
- comps += companion.asSeenFrom(pre, compSym.owner).asInstanceOf [TermRef ]
545
- }
546
- def addCompanionOf (sym : Symbol ) = {
547
- val companion = sym.companionModule
548
- if (companion.exists) addRef(companion.termRef)
549
- }
550
- def addClassScope (cls : ClassSymbol ): Unit = {
551
- addCompanionOf(cls)
552
- for (parent <- cls.classParents; ref <- iscopeRefs(tp.baseType(parent.classSymbol)))
553
- addRef(ref)
523
+ if (! tp.symbol.is(Package ) || ctx.scala2Mode) {
524
+ // Don't consider implicits in package prefixes unless under -language:Scala2
525
+ val pre = tp.prefix
526
+ comps ++= iscopeRefs(pre)
527
+ def addRef (companion : TermRef ): Unit = {
528
+ val compSym = companion.symbol
529
+ if (compSym is Package ) {
530
+ assert(ctx.scala2Mode)
531
+ addRef(companion.select(nme.PACKAGE ))
532
+ }
533
+ else if (compSym.exists)
534
+ comps += companion.asSeenFrom(pre, compSym.owner).asInstanceOf [TermRef ]
535
+ }
536
+ def addCompanionOf (sym : Symbol ) = {
537
+ val companion = sym.companionModule
538
+ if (companion.exists) addRef(companion.termRef)
539
+ }
540
+ def addClassScope (cls : ClassSymbol ): Unit = {
541
+ addCompanionOf(cls)
542
+ for (parent <- cls.classParents; ref <- iscopeRefs(tp.baseType(parent.classSymbol)))
543
+ addRef(ref)
544
+ }
545
+ val underlyingTypeSym = tp.widen.typeSymbol
546
+ if (underlyingTypeSym.isOpaqueAlias) addCompanionOf(underlyingTypeSym)
547
+ else tp.classSymbols(liftingCtx).foreach(addClassScope)
554
548
}
555
- val underlyingTypeSym = tp.widen.typeSymbol
556
- if (underlyingTypeSym.isOpaqueAlias) addCompanionOf(underlyingTypeSym)
557
- else tp.classSymbols(liftingCtx).foreach(addClassScope)
558
549
case _ =>
559
550
for (part <- tp.namedPartsWith(_.isType)) comps ++= iscopeRefs(part)
560
551
}
@@ -836,9 +827,6 @@ trait Implicits { self: Typer =>
836
827
shortForm
837
828
case _ =>
838
829
arg.tpe match {
839
- case tpe : ShadowedImplicit =>
840
- i """ $headline;
841
- | ${tpe.explanation}. """
842
830
case tpe : SearchFailureType =>
843
831
i """ $headline.
844
832
|I found:
@@ -1034,9 +1022,6 @@ trait Implicits { self: Typer =>
1034
1022
1035
1023
private def isCoherent = pt.isRef(defn.EqlClass )
1036
1024
1037
- private val cmpContext = nestedContext()
1038
- private val cmpCandidates = (c1 : Candidate , c2 : Candidate ) => compare(c1.ref, c2.ref, c1.level, c2.level)(cmpContext)
1039
-
1040
1025
/** The expected type for the searched implicit */
1041
1026
lazy val fullProto : Type = implicitProto(pt, identity)
1042
1027
@@ -1050,9 +1035,9 @@ trait Implicits { self: Typer =>
1050
1035
/** Try to typecheck an implicit reference */
1051
1036
def typedImplicit (cand : Candidate , contextual : Boolean )(implicit ctx : Context ): SearchResult = track(" typedImplicit" ) { trace(i " typed implicit ${cand.ref}, pt = $pt, implicitsEnabled == ${ctx.mode is ImplicitsEnabled }" , implicits, show = true ) {
1052
1037
val ref = cand.ref
1053
- var generated : Tree = tpd.ref(ref).withSpan(span.startPos)
1038
+ val generated : Tree = tpd.ref(ref).withSpan(span.startPos)
1054
1039
val locked = ctx.typerState.ownedVars
1055
- val generated1 =
1040
+ val adapted =
1056
1041
if (argument.isEmpty)
1057
1042
adapt(generated, pt, locked)
1058
1043
else {
@@ -1074,51 +1059,20 @@ trait Implicits { self: Typer =>
1074
1059
}
1075
1060
else tryConversion
1076
1061
}
1077
- lazy val shadowing =
1078
- typedUnadapted(untpd.Ident (cand.implicitRef.implicitName).withSpan(span.toSynthetic))(
1079
- nestedContext().addMode(Mode .ImplicitShadowing ).setExploreTyperState())
1080
-
1081
- /** Is candidate reference the same as the `shadowing` reference? (i.e.
1082
- * no actual shadowing occured). This is the case if the
1083
- * underlying symbol of the shadowing reference is the same as the
1084
- * symbol of the candidate reference, or if they have a common type owner.
1085
- *
1086
- * The second condition (same owner) is needed because the candidate reference
1087
- * and the potential shadowing reference are typechecked with different prototypes.
1088
- * so might yield different overloaded symbols. E.g. if the candidate reference
1089
- * is to an implicit conversion generated from an implicit class, the shadowing
1090
- * reference could go to the companion object of that class instead.
1091
- */
1092
- def refSameAs (shadowing : Tree ): Boolean = {
1093
- def symMatches (sym : Symbol ): Boolean =
1094
- sym == ref.symbol || sym.owner.isType && sym.owner == ref.symbol.owner
1095
- def denotMatches (d : Denotation ): Boolean = d match {
1096
- case d : SingleDenotation => symMatches(d.symbol)
1097
- case d => d.hasAltWith(denotMatches(_))
1098
- }
1099
- denotMatches(closureBody(shadowing).denot)
1100
- }
1101
-
1102
1062
if (ctx.reporter.hasErrors) {
1103
1063
ctx.reporter.removeBufferedMessages
1104
1064
SearchFailure {
1105
- generated1 .tpe match {
1106
- case _ : SearchFailureType => generated1
1107
- case _ => generated1 .withType(new MismatchedImplicit (ref, pt, argument))
1065
+ adapted .tpe match {
1066
+ case _ : SearchFailureType => adapted
1067
+ case _ => adapted .withType(new MismatchedImplicit (ref, pt, argument))
1108
1068
}
1109
1069
}
1110
1070
}
1111
- else if (contextual && ! ctx.mode.is(Mode .ImplicitShadowing ) &&
1112
- ! shadowing.tpe.isError && ! refSameAs(shadowing)) {
1113
- implicits.println(i " SHADOWING $ref in ${ref.termSymbol.maybeOwner} is shadowed by $shadowing in ${shadowing.symbol.maybeOwner}" )
1114
- SearchFailure (generated1.withTypeUnchecked(
1115
- new ShadowedImplicit (ref, methPart(shadowing).tpe, pt, argument)))
1116
- }
1117
1071
else {
1118
- val generated2 =
1119
- if (cand.isExtension) Applications .ExtMethodApply (generated1 ).withType(generated1 .tpe)
1120
- else generated1
1121
- SearchSuccess (generated2 , ref, cand.level)(ctx.typerState, ctx.gadt)
1072
+ val returned =
1073
+ if (cand.isExtension) Applications .ExtMethodApply (adapted ).withType(adapted .tpe)
1074
+ else adapted
1075
+ SearchSuccess (returned , ref, cand.level)(ctx.typerState, ctx.gadt)
1122
1076
}
1123
1077
}}
1124
1078
@@ -1143,12 +1097,14 @@ trait Implicits { self: Typer =>
1143
1097
1144
1098
/** Search a list of eligible implicit references */
1145
1099
def searchImplicits (eligible : List [Candidate ], contextual : Boolean ): SearchResult = {
1100
+
1146
1101
/** Compare previous success with reference and level to determine which one would be chosen, if
1147
1102
* an implicit starting with the reference was found.
1148
1103
*/
1149
1104
def compareCandidate (prev : SearchSuccess , ref : TermRef , level : Int ): Int =
1150
1105
if (prev.ref eq ref) 0
1151
- else nestedContext().test(implicit ctx => compare(prev.ref, ref, prev.level, level))
1106
+ else if (prev.level != level) prev.level - level
1107
+ else nestedContext().test(implicit ctx => compare(prev.ref, ref))
1152
1108
1153
1109
/** If `alt1` is also a search success, try to disambiguate as follows:
1154
1110
* - If alt2 is preferred over alt1, pick alt2, otherwise return an
@@ -1326,7 +1282,7 @@ trait Implicits { self: Typer =>
1326
1282
case _ : AmbiguousImplicits => failure2
1327
1283
case _ =>
1328
1284
reason match {
1329
- case (_ : DivergingImplicit ) | ( _ : ShadowedImplicit ) => failure
1285
+ case (_ : DivergingImplicit ) => failure
1330
1286
case _ => List (failure, failure2).maxBy(_.tree.treeSize)
1331
1287
}
1332
1288
}
0 commit comments