-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #876: Allow implicit conversions from singleton types #4738
Changes from 5 commits
bd04177
4721da5
d3cd401
69bfb34
5728b19
c8d9b0d
673af1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,15 @@ object Implicits { | |
/** The implicit references */ | ||
def refs: List[ImplicitRef] | ||
|
||
private var SingletonClass: ClassSymbol = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add a cache for SingletonClass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reason as for the caches in TypeComparer. It's used a lot and as a lazy val access is not super fast. It was one of the performance tweaks I applied after we saw a slip. Not sure it was the one that made the difference. |
||
|
||
/** Widen type so that it is neither a singleton type nor it inherits from scala.Singleton. */ | ||
private def widenSingleton(tp: Type)(implicit ctx: Context): Type = { | ||
if (SingletonClass == null) SingletonClass = defn.SingletonClass | ||
val wtp = tp.widenSingleton | ||
if (wtp.derivesFrom(SingletonClass)) defn.AnyType else wtp | ||
} | ||
|
||
/** Return those references in `refs` that are compatible with type `pt`. */ | ||
protected def filterMatching(pt: Type)(implicit ctx: Context): List[Candidate] = track("filterMatching") { | ||
|
||
|
@@ -80,15 +89,17 @@ object Implicits { | |
case mt: MethodType => | ||
mt.isImplicitMethod || | ||
mt.paramInfos.lengthCompare(1) != 0 || | ||
!ctx.test(implicit ctx => argType relaxed_<:< mt.paramInfos.head) | ||
!ctx.test(implicit ctx => | ||
argType relaxed_<:< widenSingleton(mt.paramInfos.head)) | ||
case poly: PolyType => | ||
// We do not need to call ProtoTypes#constrained on `poly` because | ||
// `refMatches` is always called with mode TypevarsMissContext enabled. | ||
poly.resultType match { | ||
case mt: MethodType => | ||
mt.isImplicitMethod || | ||
mt.paramInfos.length != 1 || | ||
!ctx.test(implicit ctx => argType relaxed_<:< wildApprox(mt.paramInfos.head, null, Set.empty)) | ||
!ctx.test(implicit ctx => | ||
argType relaxed_<:< wildApprox(widenSingleton(mt.paramInfos.head), null, Set.empty)) | ||
case rtp => | ||
discardForView(wildApprox(rtp, null, Set.empty), argType) | ||
} | ||
|
@@ -132,14 +143,32 @@ object Implicits { | |
case _ => false | ||
} | ||
|
||
/** Widen singleton arguments of implicit conversions to their underlying type. | ||
* This is necessary so that they can be found eligible for the argument type. | ||
* Note that we always take the underlying type of a singleton type as the argument | ||
* type, so that we get a reasonable implicit cache hit ratio. | ||
*/ | ||
def adjustSingletonArg(tp: Type): Type = tp match { | ||
case tp: PolyType => | ||
val res = adjustSingletonArg(tp.resType) | ||
if (res `eq` tp.resType) tp else tp.derivedLambdaType(resType = res) | ||
case tp: MethodType => | ||
tp.derivedLambdaType(paramInfos = tp.paramInfos.mapConserve(widenSingleton)) | ||
case _ => tp | ||
} | ||
|
||
(ref.symbol isAccessibleFrom ref.prefix) && { | ||
if (discard) { | ||
record("discarded eligible") | ||
false | ||
} | ||
else { | ||
val ptNorm = normalize(pt, pt) // `pt` could be implicit function types, check i2749 | ||
NoViewsAllowed.isCompatible(normalize(ref, pt), ptNorm) | ||
val refAdjusted = | ||
if (pt.isInstanceOf[ViewProto]) adjustSingletonArg(ref.widenSingleton) | ||
else ref | ||
val refNorm = normalize(refAdjusted, pt) | ||
NoViewsAllowed.isCompatible(refNorm, ptNorm) | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
object Test extends App { | ||
object O | ||
implicit def foo(x: O.type): String = "hello" | ||
val s: String = O | ||
implicit def bar(x: s.type): Int = s.length | ||
//implicit def bar2(x: String): Int = s.length | ||
val l: Int = s | ||
assert(s == "hello") | ||
assert(l == 5) | ||
} | ||
|
||
object Test3781 { | ||
class Foo[T](val value : T) | ||
object Foo { | ||
implicit def fromXInt[T <: Int with Singleton](i : T): Foo[T] = new Foo[T](i) | ||
} | ||
class FooUser[T] { | ||
def op[T2](that : Foo[T2]) : FooUser[T2] = new FooUser[T2] | ||
} | ||
val f = new FooUser[1] | ||
val f2 = f op 2 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private[this]
to avoid generating accessors?