Skip to content

Compiler is overly secretive when it fails to infer the type of parameters #18042

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
Sporarum opened this issue Jun 23, 2023 · 9 comments · Fixed by #18190
Closed

Compiler is overly secretive when it fails to infer the type of parameters #18042

Sporarum opened this issue Jun 23, 2023 · 9 comments · Fixed by #18190
Assignees
Labels
area:infer area:reporting Error reporting including formatting, implicit suggestions, etc area:typer Spree Suitable for a future Spree
Milestone

Comments

@Sporarum
Copy link
Contributor

Sporarum commented Jun 23, 2023

Compiler version

Scala 3.3.0

Minimized code & Output

def foo(x: Int) = 0

foo(x => x) // error:
// Missing parameter type
// I could not infer the type of the parameter x.

https://scastie.scala-lang.org/Te6OUPEKTnmMn304qWsPew

Expectation

The fact the inference fails hides a more essential issue:

There is no type such that this succeeds.

(Except if there are implicit conversions, with the following, inferring Int would work:

given Conversion[Int => Int, Int] = f => f(42)

)

Even considering this point, a better error would be something like:

Found: T1 => T1
Expected: Int

Where T1 could not be inferred

It displays more information, naturally leading the user to realize x => x would not work in any case

@Sporarum Sporarum added area:reporting Error reporting including formatting, implicit suggestions, etc area:typer stat:needs triage Every issue needs to have an "area" and "itype" label area:infer labels Jun 23, 2023
@Sporarum
Copy link
Contributor Author

I might tackle this myself, if not, it might be a good candidate for a Spree, as it leads through multiple parts of the compiler with a clear goal in mind ?
(At least for the first exploratory phase)

@Sporarum Sporarum added the Spree Suitable for a future Spree label Jun 23, 2023
@Sporarum Sporarum self-assigned this Jun 23, 2023
@mbovel mbovel removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Jun 23, 2023
@Sporarum Sporarum changed the title Compiler fails to infer lambda parameter type, instead of reporting no such type would help Compiler is overly secretive when it fails to infer the type of parameters Jun 23, 2023
@Sporarum
Copy link
Contributor Author

Thinking more about this, I believe this would be helpful even in cases where the type to be inferred is underdetermined (as opposed to overdetermined like in my example)

(Which is why I renamed this issue)

@Sporarum
Copy link
Contributor Author

This also has the upside of making the solution much simpler: No need to wonder about this question of under- vs over-determination, just always be precise

@som-snytt
Copy link
Contributor

This is a famous tpolecat request. I can't find a Scala 2 ticket, however. I have a memory of reading it recently, because it said that it happens more often than you might think.

@odersky
Copy link
Contributor

odersky commented Jun 24, 2023

This looks very hard, and it's not clear at all the wins are large enough to warrant any complication in the compiler's code base. For me, Missing parameter type is perfectly fine as an error.

@Sporarum
Copy link
Contributor Author

There are actually two things being proposed, which are different in terms of scope:

  1. Detect and warn when a type is over-contrained, and inform the user appropriately (Very hard, possibly impossible)
  2. Add no checks and just include more information in the error message, similar to what is done with "an extension method was tried, but" (Hopefully easy)

From this moment on, this issue is about 2.
(I will not open another issue for 1. but anyone is free to do so)

@scala-center-bot
Copy link

This issue was picked for the Issue Spree No. 33 of 11 July 2023 which takes place in 7 days. @Sporarum, @sankalpgambhir will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@som-snytt
Copy link
Contributor

som-snytt commented Jul 4, 2023

scala/bug#11517

which I apparently opened on tpolecat's behalf.

In 2021, already a long time past, I noted that the message was not improved in Scala 3.

That ticket also mentions the lint which is missing here:

warning: a type was inferred to be `Any`; this may indicate a programming error.

as a backstop.

@mbovel
Copy link
Member

mbovel commented Jul 11, 2023

@sankalpgambhir as this is your first Spree, welcome!

To get started hacking on the Dotty codebase, you will need to clone it, compile it and open it in VS Code using the following commands:

git clone https://github.com/lampepfl/dotty.git
cd dotty
sbt compile
code .

Then you will need to setup Metals to use the SBT build server. To do it, run the "Switch Build Server" VS Code command (by default the shortcut to open the VS Code command palette is control-⇧-P):

image image

To learn more: https://dotty.epfl.ch/docs/contributing/getting-started.html.

@Sporarum Sporarum linked a pull request Jul 11, 2023 that will close this issue
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:infer area:reporting Error reporting including formatting, implicit suggestions, etc area:typer Spree Suitable for a future Spree
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants