-
Notifications
You must be signed in to change notification settings - Fork 1.1k
"case BarWrapper(foo
)" should fail if the type of foo is unrelated to the type of BarWrapper argument
#1942
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
Comments
unrelated
)" should fail if the type of foo is unrelated to the type of BarWrapper argumentfoo
)" should fail if the type of foo is unrelated to the type of BarWrapper argument
the pattern matcher will use |
I think this is more related the universal-equlality than the pattern matching itself. |
I don't think so, we are typing a value of type
|
Well, the pattern matching&typer doesn't implement it this way in neither dotty nor scalac: def test(barWrapper: BarWrapper): Unit = {
val foo: Foo = new Foo();
{
case <synthetic> val x1: BarWrapper = barWrapper;
case6(){
if (x1.ne(null))
{
<synthetic> val p2: Bar = x1.bar;
if (foo.==(p2))
matchEnd5(())
else
case7()
}
else
case7()
};
case7(){
matchEnd5(())
};
matchEnd5(x: Unit){
x
}
}
} |
The spec says what should or should not typecheck, no matter how we implement it at runtime. |
I'm fine with this being a warning, but I don't think we should fail compilation. |
This can be simplified to: class Foo
class Bar
object Test {
def test(bar: Bar): Unit = {
val foo = new Foo
bar match {
case `foo` =>
case _ =>
}
}
}
I would be very curious of what kind of code could take advantage of this, and could not be easily rewritten to not use this. |
https://issues.scala-lang.org/browse/SI-4577 - seems related |
in https://issues.scala-lang.org/browse/SI-4577 the behavior for singleton types was changed to use reference equality, but the ping @adriaanm |
I think it's fine for stable identifiers match to use |
I tried to faithfully implement the spec in scalac in scala/scala#2742 but backed out. There is some discussion in http://issues.scala-lang.org/browse/SI-7655 |
We now don't get an error in the
case. We get a warning instead:
For the case of
we don't get a warning. But if we declare
I think this is as it should be. Let multiversal equality handle non-sensical equality tests. |
The other case is still a warning in 2.13.10 but errors in 3.2.1
|
This fails to compile, as expected:
But if you replace
case BarWrapper(_: Foo) =>
bycase BarWrapper(`foo`) =>
, the file will compile without any error or warning in both scalac and dotty. I find this very concerning because it means that you cannot safely refactor any case class by changing the type of one of its parameter, for example in #1941 I replacedcase class PostfixOp(od: Tree, op: Name)
bycase class PostfixOp(od: Tree, op: Ident)
, fixed the compilation errors, and expected everything to be fine. But the compiler failed to warn me about the now impossible pattern matches likecase PostfixOp(_, nme.raw.STAR)
, so tests failed.@odersky WDYT?
The text was updated successfully, but these errors were encountered: