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

Right associative extension method allow apparent forward reference between parameters #16815

Closed
Sporarum opened this issue Feb 2, 2023 · 1 comment · Fixed by #21641
Closed
Assignees
Labels
area:extension-methods area:reporting Error reporting including formatting, implicit suggestions, etc area:typer better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:bug
Milestone

Comments

@Sporarum
Copy link
Contributor

Sporarum commented Feb 2, 2023

Compiler version

Scala 3.2.2

Minimized code

extension [C1 <: Int](c2: (C1, C2)) // C2 looks like invalid forward reference
  def ra2_:[C2 <: C1](c1: (C1, C2)): C2 = 
    ???

https://scastie.scala-lang.org/4LgQkgomTNijNcj1BjhGPw

More examples here (tests all confusing situations):
https://scastie.scala-lang.org/B4eX394PQbazeycIPqWiXQ

Expectation

Should not compile

Why it happens

Right associative extension methods go through a confusing swap*, for example the method above becomes:

<extension> def ra2_:[C1 <: Int][C2 <: C1](c1: (C1, C2))(c2: (C1, C2))

Which is indeed a valid method (if you ignore the curried type clauses), even though the original is invalid

*At the time of writing, it is described incorrectly in the reference, and a fixed version for it is here

Why it matters

While it is fine to give surprising tricks to language experts, in this case, this can have very confusing repercussions for normal users:

Type C2 = String

extension [C1 <: Int](c2: (C1, C2)) // C2 looks like it is an alias of String, but is actually a subtype of Int !
  def ra2_:[C2 <: C1](c1: (C1, C2)): C2 = 
    ???

How to fix it

My proposal is the following:
In case the swapped groups of clauses (leftParams trailingUsing and rightTyParam rightParam) have any reference to something defined in the other group, throw an error
The error should clearly explain why it is not allowed, and maybe solutions for the problem

@Sporarum Sporarum added itype:bug area:typer area:extension-methods stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 2, 2023
@nicolasstucki nicolasstucki self-assigned this Feb 6, 2023
@mbovel mbovel removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Feb 6, 2023
@nicolasstucki nicolasstucki removed their assignment Jul 20, 2023
@mbovel mbovel added area:reporting Error reporting including formatting, implicit suggestions, etc Spree Suitable for a future Spree better-errors Issues concerned with improving confusing/unhelpful diagnostic messages labels Jun 15, 2024
@mbovel mbovel removed their assignment Sep 23, 2024
@mbovel
Copy link
Member

mbovel commented Sep 23, 2024

This issue was picked for the Scala Issue Spree of tomorrow, September 24th. @dwijnand and @nmcb will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@mbovel mbovel removed the Spree Suitable for a future Spree label Sep 24, 2024
@WojciechMazur WojciechMazur added this to the 3.6.0 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:extension-methods area:reporting Error reporting including formatting, implicit suggestions, etc area:typer better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants