Skip to content

Survive TypeErrors in isMatchedBy #15675

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

Merged
merged 1 commit into from
Jul 15, 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
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ object Types {
* ThisType of `symd`'s owner, or a reference to `symd`'s owner.'
*/
def isArgPrefixOf(symd: SymDenotation)(using Context): Boolean =
symd.exists && !symd.owner.is(Package) && // Early exit if possible because the next check would force SymbolLoaders
symd.isAllOf(ClassTypeParam) && {
this match {
symd.exists
&& !symd.owner.is(Package) // Early exit if possible because the next check would force SymbolLoaders
&& symd.isAllOf(ClassTypeParam)
&& { this match
case tp: ThisType => tp.cls ne symd.owner
case tp: TypeRef => tp.symbol ne symd.owner
case _ => true
}
}

/** Is this type a (possibly aliased) singleton type? */
Expand Down Expand Up @@ -2337,7 +2337,8 @@ object Types {
i"""bad parameter reference $this at ${ctx.phase}
|the parameter is ${param.showLocated} but the prefix $prefix
|does not define any corresponding arguments.
|idx = $idx, args = $args""")
|idx = $idx, args = $args%, %,
|constraint = ${ctx.typerState.constraint}""")
NoDenotation
}
}
Expand Down
41 changes: 27 additions & 14 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,33 @@ object ProtoTypes {
case _ => false

override def isMatchedBy(tp1: Type, keepConstraint: Boolean)(using Context): Boolean =
name == nme.WILDCARD || hasUnknownMembers(tp1) ||
{
val mbr = if (privateOK) tp1.member(name) else tp1.nonPrivateMember(name)
def qualifies(m: SingleDenotation) =
val isAccessible = !m.symbol.exists || m.symbol.isAccessibleFrom(tp1, superAccess = true)
isAccessible
&& (memberProto.isRef(defn.UnitClass)
|| tp1.isValueType && compat.normalizedCompatible(NamedType(tp1, name, m), memberProto, keepConstraint))
// Note: can't use `m.info` here because if `m` is a method, `m.info`
// loses knowledge about `m`'s default arguments.
mbr match { // hasAltWith inlined for performance
case mbr: SingleDenotation => mbr.exists && qualifies(mbr)
case _ => mbr hasAltWith qualifies
}
name == nme.WILDCARD
|| hasUnknownMembers(tp1)
|| {
try
val mbr = if privateOK then tp1.member(name) else tp1.nonPrivateMember(name)
def qualifies(m: SingleDenotation) =
val isAccessible = !m.symbol.exists || m.symbol.isAccessibleFrom(tp1, superAccess = true)
isAccessible
&& (memberProto.isRef(defn.UnitClass)
|| tp1.isValueType && compat.normalizedCompatible(NamedType(tp1, name, m), memberProto, keepConstraint))
// Note: can't use `m.info` here because if `m` is a method, `m.info`
// loses knowledge about `m`'s default arguments.
mbr match // hasAltWith inlined for performance
case mbr: SingleDenotation => mbr.exists && qualifies(mbr)
case _ => mbr hasAltWith qualifies
catch case ex: TypeError =>
// A scenario where this can happen is in pos/15673.scala:
// We have a type `CC[A]#C` where `CC`'s upper bound is `[X] => Any`, but
// the current constraint stipulates CC <: SeqOps[...], where `SeqOps` defines
// the `C` parameter. We try to resolve this using `argDenot` but `argDenot`
// consults the base type of `CC`, which is not `SeqOps`, so it does not
// find a corresponding argument. In fact, `argDenot` is not allowed to
// consult short-lived things like the current constraint, so it has no other
// choice. The problem will be healed later, when normal selection fails
// and we try to instantiate type variables to compensate. But we have to make
// sure we do not issue a type error before we get there.
false
}

def underlying(using Context): Type = WildcardType
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i15673.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait SeqOps[+A, +CC[_], +C]: // scala.collection.SeqOps
def reverse: C

extension[A, CC[B] <: SeqOps[B, CC, CC[B]]](ring: CC[A])
def startAt(i: Int): CC[A] = ???
def reflectAt(i: Int): CC[A] =
startAt(i).reverse