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

Inner traits get outer pointer only if needed #17148

Open
lrytz opened this issue Mar 24, 2023 · 4 comments
Open

Inner traits get outer pointer only if needed #17148

lrytz opened this issue Mar 24, 2023 · 4 comments
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug

Comments

@lrytz
Copy link
Member

lrytz commented Mar 24, 2023

class O:
  def h = 1
  trait T
    // def g = h

object Test:
  def foo(o: O, x: AnyRef) = x match { case _: o.T => "same"; case _ => "other" }
  def main(args: Array[String]): Unit =
    val o = new O
    assert(foo(new O, new o.T { }) == "other")

This fails because T doesn't get an outer method, so the case _: o.T check cannot compare the outer values. The compiler also doesn't warn about that.

Un-commenting the method def g adds an outer method to trait T, which changes the match evaluation.

@lrytz lrytz added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 24, 2023
@SethTisue
Copy link
Member

SethTisue commented Mar 24, 2023

previous discussions in this area include #2156 (2017) and scala/bug#4440 (2011)

"In dotty, if a subclass needs an outer accessor, it gets one" — Martin (so presumably this exception was inadvertent, rather than a design decision)

@lrytz
Copy link
Member Author

lrytz commented Mar 24, 2023

Changing this (adding the outer to the trait) would break binary compatibility: the outer in an inner trait is an abstract method. If we haveclass C { trait T }, adding the abstract method to T requires re-compiling all subclasses that extend T.

So if an inner trait is re-compiled with a new version of the compiler, it acquires an abstract outer method, which would show up in MiMa.

@SethTisue
Copy link
Member

SethTisue commented Mar 24, 2023

Unless there is some flaw in our reasoning here, seems like we can't fix this until Scala 4 and the best we can do in 3.x is warn at the pattern match site 😕

(“our” = me, Lukas, @sjrd at the Center today)

@dwijnand dwijnand added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 27, 2023
@lrytz
Copy link
Member Author

lrytz commented Aug 22, 2023

Scala 2.13.11 warns at the pattern:

Test.scala:29: warning: The outer reference in this type test cannot be checked at run time.
  def foo(o: O, x: AnyRef) = x match { case _: o.T => "same"; case _ => "other" }
                                             ^
1 warning

@dwijnand dwijnand added area:linting Linting warnings enabled with -W or -Xlint and removed area:typer labels Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Projects
None yet
Development

No branches or pull requests

3 participants