Skip to content
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

Better infos for dependent class parameter references #15667

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,10 @@ object Denotations {
if !owner.membersNeedAsSeenFrom(pre) && (!ownerIsPrefix || hasOriginalInfo)
|| symbol.is(NonMember)
then this
else if symbol.isAllOf(ClassTypeParam) then
val arg = symbol.typeRef.argForParam(pre, widenAbstract = true)
if arg.exists then derivedSingleDenotation(symbol, arg.bounds, pre)
else derived(symbol.info)
else derived(symbol.info)
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ object TypeOps:
override def apply(tp: Type): Type =
try
tp match
case tp: TermRef
if toAvoid(tp) =>
case tp: TermRef if toAvoid(tp) =>
tp.info.widenExpr.dealias match {
case info: SingletonType => apply(info)
case info => range(defn.NothingType, apply(info))
Expand Down
14 changes: 8 additions & 6 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2459,10 +2459,12 @@ object Types {
ctx.base.underlyingRecursions -= 1

/** The argument corresponding to class type parameter `tparam` as seen from
* prefix `pre`. Can produce a TypeBounds type in case prefix is an & or | type
* and parameter is non-variant.
* prefix `pre`. Can produce a TypeBounds type if `widenAbstract` is true,
* or prefix is an & or | type and parameter is non-variant.
* Otherwise, a typebounds argument is dropped and the original type parameter
* reference is returned.
*/
def argForParam(pre: Type)(using Context): Type = {
def argForParam(pre: Type, widenAbstract: Boolean = false)(using Context): Type = {
val tparam = symbol
val cls = tparam.owner
val base = pre.baseType(cls)
Expand All @@ -2474,7 +2476,7 @@ object Types {
while (tparams.nonEmpty && args.nonEmpty) {
if (tparams.head.eq(tparam))
return args.head match {
case _: TypeBounds => TypeRef(pre, tparam)
case _: TypeBounds if !widenAbstract => TypeRef(pre, tparam)
case arg => arg
}
tparams = tparams.tail
Expand Down Expand Up @@ -5581,10 +5583,10 @@ object Types {
// if H#T = U, then for any x in L..H, x.T =:= U,
// hence we can replace with U under all variances
reapply(alias.rewrapAnnots(tp1))
case tp: TypeBounds =>
case bounds: TypeBounds =>
// If H#T = ? >: S <: U, then for any x in L..H, S <: x.T <: U,
// hence we can replace with S..U under all variances
expandBounds(tp)
expandBounds(bounds)
case info: SingletonType =>
// if H#x: y.type, then for any x in L..H, x.type =:= y.type,
// hence we can replace with y.type under all variances
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i15652.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Node
type NodeParser[T] = Node => T

def child(key: String): Option[Node] = ???

def optionalOneOf[T](in: Map[String, NodeParser[? <: T]]): Option[T] =
val mappings = in flatMap { (key, parser) =>
child(key) map { node =>
key -> (() => parser(node))
}
}
mappings.headOption map (_._2())