Skip to content

Type pattern not widening scrutinee type #20992

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
EugeneFlesselle opened this issue Jul 3, 2024 · 4 comments
Closed

Type pattern not widening scrutinee type #20992

EugeneFlesselle opened this issue Jul 3, 2024 · 4 comments

Comments

@EugeneFlesselle
Copy link
Contributor

Compiler version

3.5.1-RC1-bin-SNAPSHOT-nonbootstrapped-git-cd8c5ed

Minimized code

Minimized from i7294.scala with @hamzaremmal

object Test:

  trait Foo:
    val x: Any

  ??? match
    case f: Foo =>
      (f: Foo).x // ok
      f.x // error

Output

-- [E007] Type Mismatch Error: tests/playground/example.scala:11:6 -------------
11 |      f.x // error
   |      ^
   |Found:    (f : Nothing)
   |Required: ?{ x: ? }
   |Note that implicit conversions were not tried because the result of an implicit conversion
   |must be more specific than ?{ x: <?> }

Expectation

no error

@EugeneFlesselle
Copy link
Contributor Author

Actually not sure this is an error since the following is accepted:

(??? : Option[Int]) match
    case a: Any => a.get

@hamzaremmal
Copy link
Member

Note that this is the behavior under the -source 3.6

@odersky
Copy link
Contributor

odersky commented Sep 14, 2024

It's because ??? has type Nothing, so the pattern has type Nothing as well. We use what we know from the scrutinee and from the pattern to type the pattern, so this looks as expected.

@dwijnand
Copy link
Member

I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants