Skip to content

No static check for pattern match on Identifiers #9166

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

Closed
bishabosha opened this issue Jun 11, 2020 · 6 comments · Fixed by #11784
Closed

No static check for pattern match on Identifiers #9166

bishabosha opened this issue Jun 11, 2020 · 6 comments · Fixed by #11784
Assignees
Milestone

Comments

@bishabosha
Copy link
Member

Minimized code

object UnitTest extends App {
  def foo(m: Unit) = m match {
    case runtime.BoxedUnit.UNIT => println("ok")
  }
  foo(())
}

Output

compiles and prints "ok"

Expectation

error like in Scala 2

UnitTest.scala:4: error: type mismatch;
 found   : scala.runtime.BoxedUnit
 required: Unit
    case runtime.BoxedUnit.UNIT => println("ok")
                           ^
1 error
@nicolasstucki
Copy link
Contributor

The code seems to be technically correct. runtime.BoxedUnit.UNIT is the only value of type Unit. Not sure if we should disallow this.

@nicolasstucki
Copy link
Contributor

Scala 2 compact issues are for those that scala 2 supports but scala 3.0 does not. Here it is the opposite.

@bishabosha
Copy link
Member Author

bishabosha commented Jun 11, 2020

ok I guess it is a philosophical debate whether or not to allow a case that is a supertype because potentially it could match but maybe its a useless branch

trait Trait

object UnitTest extends App {
  val Foo: AnyRef = ""
  def foo(m: Trait) = m match {
    case Foo => println("ok")
  }
  foo(Foo)
}

Scala 2 has the same behaviour here.
This is a silly example Im thinking now anyway because its the same argument when you match on Any

@nicolasstucki
Copy link
Contributor

That should not compile. There should be an error in foo(Foo).

@bishabosha
Copy link
Member Author

bishabosha commented Jun 11, 2020

Sorry yeah the confusion comes from if it is a type error to pass the value Foo to expected type Trait then why can you match on the value Foo, and then the answer would be that the runtime class of Foo could be, unless its not even a concern because == is defined on Any

@dwijnand
Copy link
Member

I think this can be closed. The BoxedUnit.Unit specific example isn't worth considering, as it's scala.runtime. The more general trait Trait; val Foo: AnyVal; (m: Trait) match { case Foo => } example is fine as there's nothing to suggest that m isn't <: Trait & Foo.type. I learnt this week that that check is called isPopulated in nsc.

liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 17, 2021
smarter added a commit that referenced this issue Mar 17, 2021
Fix #9166: Harden check for values in patterns based on class inheritance info
michelou pushed a commit to michelou/scala3 that referenced this issue Mar 22, 2021
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants