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

Warn for named tuples that look like assignments #21823

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented Oct 21, 2024

This PR adds a warning for named tuples that look like assignment, such as (x = 1).

This is the first half to implement #21681. The second will be to add warnings for named arguments to infix method calls (as a separate PR?).

Started during the Spree of October 21st.

Co-Authored-By: Nicolas Stucki <3648029+nicolasstucki@users.noreply.github.com>
Co-Authored-By: Oliver Bračevac <bracevac@users.noreply.github.com>
@som-snytt
Copy link
Contributor

My quick test shows an unexpected error

Compilation failed for: tests/warn/infix-named-args.scala
-> following the diagnostics:
 at 5: None of the overloaded alternatives of method + in class Int with types
 (x: Double): Double
 (x: Float): Float
 (x: Long): Long
 (x: Int): Int
 (x: Char): Int
 (x: Short): Int
 (x: Byte): Int
 (x: String): String
match arguments ((x : Int))

for the test in

https://github.com/scala/scala3/pull/21565/files

@mbovel
Copy link
Member Author

mbovel commented Oct 21, 2024

My quick test shows an unexpected error

def f = 42 + (x = 1)
-- [E134] Type Error: playground.scala:1:11 ------------------------------------
1 |def f = 42 + (x = 1)
  |        ^^^^
  |   None of the overloaded alternatives of method + in class Int with types
  |    (x: Double): Double
  |    (x: Float): Float
  |    (x: Long): Long
  |    (x: Int): Int
  |    (x: Char): Int
  |    (x: Short): Int
  |    (x: Byte): Int
  |    (x: String): String
  |   match arguments ((x : Int))

It is expected to have an error in this case, isn't it? I also get in on main.

We should add a warning to tell the user that (x = 1) is interpreted as a tuple in this case as suggested in #21681, but we haven't had the time to implement it yet.

@mbovel
Copy link
Member Author

mbovel commented Oct 21, 2024

Also, we found that our naive use of ctx.scope.lookup it not complete enough. It seems it only looks in the current scope.

It doesn't work for example for:

object Test:
  var age: Int = 28
  (age = 29) // warn

@nicolasstucki suggests that we move our check to Typer and type the identifier there to resolve the symbol properly.

@mbovel mbovel linked an issue Oct 21, 2024 that may be closed by this pull request
Co-Authored-By: Nicolas Stucki <3648029+nicolasstucki@users.noreply.github.com>
Co-Authored-By: Oliver Bračevac <bracevac@users.noreply.github.com>
@mbovel
Copy link
Member Author

mbovel commented Oct 21, 2024

@nicolasstucki @bracevac I moved the check to Typer and tried to type the key as an Ident as discussed: ca5f467. However Symbol.setter is still not defined. Maybe it only is once the whole class is type-checked?

I then tried to type the whole thing as Assign, with a temporary context: dd22105. It seems to work locally, let's see what the CI says.

@mbovel mbovel marked this pull request as ready for review October 21, 2024 21:16
@SethTisue
Copy link
Member

I hit this issue in my own code just this morning, during a 3.6.1 upgrade — locating the cause took me a while, so I’m glad to see a warning being added

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

Successfully merging this pull request may close these issues.

Add migration helpers for named tuples
3 participants