You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@main deftest:Unit= {
classFooclassBartraitUpBnd[+A]
traitPextendsUpBnd[Foo]
defpmatch[A, T<:UpBnd[A]](s: T):A= s match {
casep: P=>newFoo
}
classUpBndAndBextendsUpBnd[Bar] withP// ClassCastException: Foo cannot be cast to Barvalx= pmatch(newUpBndAndB)
}
Output
Click to expand
java.lang.ClassCastException: main$package$Foo$1 cannot be cast to main$package$Bar$1
at main$package$.test(main.scala:14)
at test.main(main.scala:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sbt.Run.invokeMain(Run.scala:115)
at sbt.Run.execute$1(Run.scala:79)
at sbt.Run.$anonfun$runWithLoader$4(Run.scala:92)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
at sbt.TrapExit$App.run(TrapExit.scala:257)
at java.lang.Thread.run(Thread.java:748)
Expectation
The code should be rejected because we do not have enough information about A.
I think that having PatternTypeConstrainer#constrainSimplePatternType widen the type parameters of type constructors appearing in bounds should do the trick.
The text was updated successfully, but these errors were encountered:
Actually, I think that the widening should be done recursively, as just exploring the bounds may not be enough:
@main deftest:Unit= {
traitFootraitBarclassFooBarextendsFoowithBartraitS[F[_]]
traitTyCon[+A, B]
defnested[XX<:Foo]:XX= {
// Note: P[+XX <: Foo] extends S[[T] =>> TyCon[XX, T]] does not compile, so we nest it within this dummy functiontraitPextendsS[[T] =>>TyCon[XX, T]]
defpatmat[F<: [A] =>>TyCon[Foo, A]](s: S[F]):XX= s match {
casep: P=>// Inferred: Foo <:< XX (so Foo =:= XX), though it is not necessarly the case newFoo{}
}
classDynextendsP
patmat(newDyn)
}
// ClassCastException: main$package$$anon$1 cannot be cast to main$package$FooBar$1valgot= nested[FooBar]
}
Minimized code
Output
Click to expand
Expectation
The code should be rejected because we do not have enough information about
A
.I think that having
PatternTypeConstrainer#constrainSimplePatternType
widen the type parameters of type constructors appearing in bounds should do the trick.The text was updated successfully, but these errors were encountered: