Skip to content

Overloaded assignment operators defined as extension methods across different objects fail to compile #18713

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
ntnj opened this issue Oct 18, 2023 · 3 comments · Fixed by #18714

Comments

@ntnj
Copy link

ntnj commented Oct 18, 2023

Compiler version

3.4.0-RC1-bin-20231006-b1ccc6a-NIGHTLY

Minimized code

Scastie: https://scastie.scala-lang.org/cYHDxwGgQzm3VBWrIHYQhA

a.scala

import language.experimental.relaxedExtensionImports
class A(var x: String = "a")
object AA:
    extension (a: A)
        def f = a.x
        def f_=(x: String) = a.x = x

b.scala

import language.experimental.relaxedExtensionImports
class B(var x: String = "b")
object BB:
    extension (b: B)
        def f = b.x
        def f_=(x: String) = b.x = x

main.scala

import language.experimental.relaxedExtensionImports
import AA.*
import BB.* 
@main def main() =
    val a = A()
    a.f = "aa"
    println(a.f)

Output

sbt:tmp> run
[info] compiling 1 Scala source to /workspaces/tmp/target/scala-3.4.0-RC1-bin-20231006-b1ccc6a-NIGHTLY/classes ...
[error] -- [E052] Type Error: /workspaces/tmp/main.scala:7:8 --------
[error] 6 |    a.f = "aa"
[error]   |    ^^^^^^^^^^
[error]   |    Reassignment to val f

If I remove def f_=(x: String) = b.x = x from B extension, it runs successfully with run in sbt console.

When I then add it back, it runs successfully with run in sbt console, but fails again on run after clean, which is potentially a partial compilation bug.

Expectation

aa

@ntnj ntnj added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 18, 2023
@nicolasstucki
Copy link
Contributor

nicolasstucki commented Oct 18, 2023

Minimization

import language.experimental.relaxedExtensionImports

class A
object AA:
  extension (a: A)
    def f = ???
    def f_=(x: String) = ???

object BB:
  extension (b: Long)
    def f = ???
    def f_=(x: String) = ???

def test(a: A) =
  import AA.*
  import BB.*
  a.f = "aa"
  f_=(a)("aa")
-- [E052] Type Error: t/Test.scala:17:6 ----------------------------------------
17 |  a.f = "aa"
   |  ^^^^^^^^^^
   |  Reassignment to val f
   |
   | longer explanation available when compiling with `-explain`
-- [E049] Reference Error: t/Test.scala:18:2 -----------------------------------
18 |  f_=(a)("bb")
   |  ^^^
   |Reference to f_= is ambiguous.
   |It is both imported by import AA._
   |and imported subsequently by import BB._
   |
   | Hint: This error may arise if extension method `f_=` is called as a normal method.
   |
   | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: t/Test.scala:18:6 -------------------------------
18 |  f_=(a)("bb")
   |      ^
   |      Found:    (a : A)
   |      Required: Long
   |
   | longer explanation available when compiling with `-explain`

@nicolasstucki nicolasstucki added area:typer area:extension-methods and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 18, 2023
@nicolasstucki
Copy link
Contributor

It looks like the implementation of #9552 defaults to this error message here when the reference is ambiguous.

@nicolasstucki
Copy link
Contributor

When resolving the setter we get first type the prefix to AA.f, but then when we desugar it into the setter we use the Ident f_= which gets typed as BB.f_= instead of AA.f_=. We seem to lose the typed qualifier of the extension method.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 18, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes scala#18713
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 18, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes scala#18713
@nicolasstucki nicolasstucki self-assigned this Oct 18, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 24, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes scala#18713
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Nov 9, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes scala#18713
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Nov 9, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes scala#18713
nicolasstucki added a commit that referenced this issue Nov 13, 2023
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes #18713
WojciechMazur pushed a commit that referenced this issue Jun 22, 2024
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes #18713

[Cherry-picked 789145c]
WojciechMazur pushed a commit that referenced this issue Jun 23, 2024
We already keep the qualifier as a typed splice if the prefix is an
explicit Select.

Fixes #18713

[Cherry-picked 789145c]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants