Skip to content
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

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

Open
som-snytt opened this issue Mar 15, 2025 · 0 comments
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug

Comments

@som-snytt
Copy link
Contributor

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.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 15, 2025
@Gedochao Gedochao added area:reporting Error reporting including formatting, implicit suggestions, etc and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants