Skip to content

Patvar definition with named constructor pattern emits error but works unchecked #22811

Open
@som-snytt

Description

@som-snytt

Minimized code

Welcome to Scala 3.7.1-RC1-bin-SNAPSHOT-nonbootstrapped-git-f0c050e (23.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> case class C(i: Int, j: Int)
// defined case class C

scala> class D(c: C) { val C(j = y) = c }
-- Error: ----------------------------------------------------------------------
1 |class D(c: C) { val C(j = y) = c }
  |                      ^^^^^
  |                   Illegal combination of named and unnamed tuple elements
-- Warning: --------------------------------------------------------------------
1 |class D(c: C) { val C(j = y) = c }
  |                               ^
  |pattern's type (y : Int) is more specialized than the right hand side expression's type Int
  |
  |If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |which may result in a MatchError at runtime.
1 warning found
1 error found

scala> val c = C(42, 27)
val c: C = C(42,27)

scala> c match { case C(j = y) => y }
val res0: Int = 27

scala> class D(c: C) { val C(j = y) = c.runtimeChecked }
-- Error: ----------------------------------------------------------------------
1 |class D(c: C) { val C(j = y) = c.runtimeChecked }
  |                               ^^^^^^^^^^^^^^^^
  |         method runtimeChecked is marked @experimental
  |
  |         Experimental definition may only be used under experimental mode:
  |           1. in a definition marked as @experimental, or
  |           2. an experimental feature is imported at the package level, or
  |           3. compiling with the -experimental compiler flag.
1 error found

scala> class D(c: C) { val C(j = y) = c: @unchecked }
// defined class D

scala> D(c)
val res1: D = D@7462ba4b

scala> res1.y
val res2: Int = 27

scala>

Different view:

scala> def f = { val C(i, j) = c; j }
def f: Int

scala> def f = { val C(i=i, j=j) = c; j }
1 warning found
-- Warning: --------------------------------------------------------------------
1 |def f = { val C(i=i, j=j) = c; j }
  |                            ^
  |pattern's type (i : Int) is more specialized than the right hand side expression's type Int
  |
  |If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |which may result in a MatchError at runtime.
def f: Int

scala> def f = { val C(j=j) = c; j }
-- Error: ----------------------------------------------------------------------
1 |def f = { val C(j=j) = c; j }
  |                ^^^
  |                Illegal combination of named and unnamed tuple elements
-- Warning: --------------------------------------------------------------------
1 |def f = { val C(j=j) = c; j }
  |                       ^
  |pattern's type (j : Int) is more specialized than the right hand side expression's type Int
  |
  |If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |which may result in a MatchError at runtime.
1 warning found
1 error found

Expectation

It should just quietly do my bidding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions