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

Fix isEffectivelySingleton #20486

Merged
merged 1 commit into from
May 28, 2024
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
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,12 @@ object Types extends TypeUtils {
def isSingleton(using Context): Boolean = dealias.isInstanceOf[SingletonType]

/** Is this type a (possibly aliased) singleton type or a type proxy
* or Or/And type known to be a singleton type?
* or an AndType where one operand is effectively a singleton?
*/
def isEffectivelySingleton(using Context): Boolean = dealias match
case tp: SingletonType => true
case tp: TypeProxy => tp.superType.isEffectivelySingleton
case AndType(tpL, tpR) => tpL.isEffectivelySingleton || tpR.isEffectivelySingleton
case OrType(tpL, tpR) => tpL.isEffectivelySingleton && tpR.isEffectivelySingleton
case _ => false

/** Is this upper-bounded by a (possibly aliased) singleton type?
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i20474.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class A
class B extends A

def f(a: A, c: A) =
val b1: a.type = a
val b2: a.type & B = a.asInstanceOf[a.type & B]
val b3: c.type & A = c
val b4: a.type | c.type = c
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the test below for OrType as well?

val b5: a.type | a.type = a

val d5: b5.type = a // ok

Copy link
Contributor Author

@odersky odersky May 28, 2024

Choose a reason for hiding this comment

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

I don't think that will work. Have you tried it? I see no strong reason to make it work.

Copy link
Member

Choose a reason for hiding this comment

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

It works in 3.4.2: https://scastie.scala-lang.org/A6PehvQvRhW2sUPVVtHiuA
I think the atoms is doing all these checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yes, atoms would do it. Bit it's an extreme corner case. Not sure we want to make sure it works in the general case. Because in this case we'd also have to accept

val b6: (a.type & A) | a.type
val d6: b6.type = a // ok

and I am not sure atoms would help us here.


val d1: b1.type = a
val d2: b2.type = a // ok
val d3: b3.type = a // error
val d4: b4.type = a // error