Skip to content

Failure to refine @-bound variables (x @ <pattern>) when pattern is a literal #22954

Open
@TomasMikula

Description

@TomasMikula

Compiler version

3.6.4

Minimized code

def bad[S <: String](s: S): Unit =
  s match
    case x @ "x" =>
      x: S       // OK
      x: "x"     // Error: Found: (x : S), Required: ("x" : String)
      x: S & "x" // Error: Found: (x : S), Required: S & ("x" : String)

Output

-- [E007] Type Mismatch Error: test.scala:5:6 ----------------------------------
5 |      x: "x"     // Error: Found: (x : S), Required: ("x" : String)
  |      ^
  |      Found:    (x : S)
  |      Required: ("x" : String)
  |
  |      where:    S is a type in method bad with bounds <: String
  |
  | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: test.scala:6:6 ----------------------------------
6 |      x: S & "x" // Error: Found: (x : S), Required: S & ("x" : String)
  |      ^
  |      Found:    (x : S)
  |      Required: S & ("x" : String)
  |
  |      where:    S is a type in method bad with bounds <: String
  |
  | longer explanation available when compiling with `-explain`
-- [E129] Potential Issue Warning: test.scala:4:6 ------------------------------
4 |      x: S       // OK
  |      ^^^^
  |      A pure expression does nothing in statement position
  |
  | longer explanation available when compiling with `-explain`
1 warning found
2 errors found

Expectation

The type of variable x in x @ "x" should be refined to S & "x", analogous to how it works with non-literal patterns:

sealed trait Foo
case class Bar() extends Foo

def good[S <: Foo](s: S): Unit =
  s match
    case b @ Bar() =>
      b: S       // OK
      b: Bar     // OK
      b: S & Bar // OK

Note

I believe this issue will be easier to fix than #22887, as this one is about assigning a more precise type to a newly bound variable, rather than locally (within a pattern case) refining type of an existing variable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions