-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #9740: harden type checking for pattern match on objects #11327
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
trait Is[A] | ||
case object IsInt extends Is[Int] | ||
case object IsString extends Is[String] | ||
case class C[A](is: Is[A], value: A) | ||
|
||
@main | ||
def Test = { | ||
val c_string: C[String] = C(IsString, "name") | ||
val c_any: C[_] = c_string | ||
val any: Any = c_string | ||
|
||
// Case 1: error | ||
c_string match { | ||
case C(IsInt, _) => println(s"An Int") // error | ||
case C(IsString, s) => println(s"A String with length ${s.length}") | ||
case _ => println("No match") | ||
} | ||
|
||
// Case 2: Should match the second case and print the length of the string | ||
c_any match { | ||
case C(IsInt, i) if i < 10 => println(s"An Int less than 10") | ||
case C(IsString, s) => println(s"A String with length ${s.length}") | ||
case _ => println("No match") | ||
} | ||
|
||
// Case 3: Same as above; should match the second case and print the length of the string | ||
any match { | ||
case C(IsInt, i) if i < 10 => println(s"An Int less than 10") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no checkfile for neg tests?? Sounds very unstable to not verify the failure reasons... I wanted to see on what grounds this now fails to compile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to avoid check files as they incur maintenance overhead. In contrast, we check the line numbers of reported errors agree with the comments |
||
case C(IsString, s) => println(s"A String with length ${s.length}") | ||
case _ => println("No match") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
abstract class RecoveryCompleted | ||
object RecoveryCompleted extends RecoveryCompleted | ||
|
||
abstract class TypedRecoveryCompleted | ||
object TypedRecoveryCompleted extends TypedRecoveryCompleted | ||
|
||
class Test { | ||
TypedRecoveryCompleted match { | ||
case RecoveryCompleted => println("Recovery completed") // error | ||
case TypedRecoveryCompleted => println("Typed recovery completed") | ||
} | ||
|
||
def foo(x: TypedRecoveryCompleted) = x match | ||
case RecoveryCompleted => // error | ||
case TypedRecoveryCompleted => | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
val foo: Int => Int = Some(7) match | ||
val foo: Int => Int = Option(7) match | ||
case Some(y) => x => y | ||
case None => identity[Int] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
sealed trait Exp[T] | ||
case class IntExp(x: Int) extends Exp[Int] | ||
case class StrExp(x: String) extends Exp[String] | ||
object UnitExp extends Exp[Unit] | ||
|
||
class Foo[U <: Int, T <: U] { | ||
def bar[A <: T](x: Exp[A]): Unit = x match | ||
case IntExp(x) => | ||
case StrExp(x) => | ||
case UnitExp => | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sealed trait Exp[T] | ||
case class IntExp(x: Int) extends Exp[Int] | ||
case class StrExp(x: String) extends Exp[String] | ||
object UnitExp extends Exp[Unit] | ||
|
||
trait Txn[T <: Txn[T]] | ||
case class Obj(o: AnyRef) extends Txn[Obj] with Exp[AnyRef] | ||
|
||
|
||
class Foo { | ||
def bar[A <: Txn[A]](x: Exp[A]): Unit = x match | ||
case IntExp(x) => | ||
case StrExp(x) => | ||
case UnitExp => | ||
case Obj(o) => | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
A String with length 4 | ||
A String with length 4 | ||
A String with length 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to check this also for other values that are not modules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot check non-module values due to aliasing.