-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Expected "the type test for <blah> cannot be checked at runtime" warning but none were produced #16451
Comments
Dale and I looked at this for a while today. Yes, we agree it's a bug. Unfortunately we had to stop before we reached any very definite conclusions about what's going on, and we won't be able to get back to it until later this month. |
So many parts to this.
// In the invariant case, we also use a stronger notion of disjointness:
// we consider fully instantiated types not equal wrt =:= to be disjoint
// (under any context). This is fine because it matches the runtime
// semantics of pattern matching. To implement a pattern such as
// `case Inv[T] => ...`, one needs a type tag for `T` and the compiler
// is used at runtime to check it the scrutinee's type is =:= to `T`.
// Note that this is currently a theoretical concern since Dotty
// doesn't have type tags, meaning that users cannot write patterns
// that do type tests on higher kinded types.
def invariantDisjoint(tp1: Type, tp2: Type, tparam: TypeParamInfo): Boolean =
To be continued... |
It's part 1 that concerns me by far the most, though the rest is concerning as well. Throwing an Do we need to introduce |
Note that Scala 2 refuses to compile |
Not sure if this is related, but I came to realise that none of these generate any warnings (tested with Scala 3.1.3, 3.2.2 and 3.3.0-RC2) trait Foo // Generates warning if changed to sealed trait
object Foo:
final class Foo1 extends Foo
final class Foo2 extends Foo
def test(str: String): String = str match
case "foo" => ???
def test2(num: Int): String = num match
case 10 => ???
def test3(foo: Foo): String = foo match
case _: Foo.Foo1 => ??? Is this the expected behaviour? I might be getting mixed with Scala 2, but shouldn't there be a "match may not be exhaustive" warning? |
It's not related (as in, outside of being related to pattern matching and warnings in general). Types that are "checkable" in terms of exhaustivity are any "closed" types, which includes sealed hierarchies, intersection and union types, java enums, and boolean, and case classes if its components are "closed" types or case classes (recursively). |
Well I fought hard, but in the end Dale convinced me that it's defensible (and probably desirable, even 😄) to let this compile, as long as we emit an unchecked warning. I'm perhaps excessively cautious about allowing something like this to compile because I am so often in contact with Scala newcomers who write uncheckable matches and ignore the resulting unchecked warnings — they're sadly prone to considering compiler warnings to be annoying pedantry they can just tune out, rather than containing vital information. Regardless, we do have unchecked warnings and we do allow compilation to proceed in their presence, so for consistency we should allow it here too. The valid use case is that the user knows something the input that the compiler doesn't know; even though the type parameter is uncheckable, the user wants the more specific type to be available on the right-hand side of the |
And also, the unreachable warning clearly isn't wanted here, on the same grounds. The unchecked warning covers it. The match isn't fully checkable, but it is reachable. |
fix partially reverted the good news is that we didn't have to revert the part that fixed:
so that's progress. the rest, we're revisiting at #18364 |
Compiler version
3.2.1
from https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.2.1/scala3-compiler_3-3.2.1.jarMinimized code
Given some generic class (in this case
Wrapper
) and some other type (in this caseColor
but I think this doesn't matter too much):The following code doesn't produce any warnings I think but I expect "the type test for <blah> cannot be checked at runtime":
Output
The output is expected but no warnings is not expected.
Expectation
What I expect when pattern matching on the type is:
gives "the type test for <blah> cannot be checked at runtime":
Hopefully this isn't a duplicate! I did some searching but I could always have missed something.
The text was updated successfully, but these errors were encountered: