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 comparisons for type projections #17092

Merged
merged 3 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,28 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
val ctx = comparerContext
given Context = ctx // optimization for performance
val info2 = tp2.info

/** Does `tp2` have a singleton type or NoPrefix as a prefix?
* If that's not the case, following an alias via asSeenFrom could be lossy
* so we should not conclude `false` if comparing aliases fails.
* See pos/i17064.scala for a test case
*/
def hasPrecisePrefix(tp: NamedType) =
tp.prefix.isSingleton || tp.prefix == NoPrefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine, but since asSeenFrom itself uses TypeOps.isLegalPrefix for this purpose, wouldn't it make sense to also use it here to ensure the checks stay aligned?


info2 match
case info2: TypeAlias =>
if recur(tp1, info2.alias) then return true
if tp2.asInstanceOf[TypeRef].canDropAlias then return false
if tp2.asInstanceOf[TypeRef].canDropAlias && hasPrecisePrefix(tp2) then
return false
case _ =>
tp1 match
case tp1: NamedType =>
tp1.info match {
case info1: TypeAlias =>
if recur(info1.alias, tp2) then return true
if tp1.asInstanceOf[TypeRef].canDropAlias then return false
if tp1.asInstanceOf[TypeRef].canDropAlias && hasPrecisePrefix(tp2) then
return false
case _ =>
}
val sym2 = tp2.symbol
Expand All @@ -302,7 +313,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// For convenience we want X$ <:< X.type
// This is safe because X$ self-type is X.type
sym1 = sym1.companionModule
if ((sym1 ne NoSymbol) && (sym1 eq sym2))
if (sym1 ne NoSymbol) && (sym1 eq sym2) then
ctx.erasedTypes ||
sym1.isStaticOwner ||
isSubPrefix(tp1.prefix, tp2.prefix) ||
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i15525.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def element22(
transmittable20.Type / transmittable21.Type
} = ???

def test22 = // error
def test22 =
Resolution(
element22(
Resolution(element0), Resolution(element0), // error // error
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i17064.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class HiddenInner[+O<:Outer](val outer:O){
}

class Outer{
type Inner = HiddenInner[this.type]
}

val o : Outer = new Outer
def a : o.Inner = new o.Inner(o)
val b : Outer#Inner = a // DOES NOT COMPILE