Skip to content

Unable to call recursive private inline method defined in a trait #11706

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
julienrf opened this issue Mar 11, 2021 · 3 comments · Fixed by #17499
Closed

Unable to call recursive private inline method defined in a trait #11706

julienrf opened this issue Mar 11, 2021 · 3 comments · Fixed by #17499

Comments

@julienrf
Copy link
Contributor

julienrf commented Mar 11, 2021

Compiler version

3.0.0

Minimized code

package bugreport

import scala.compiletime.erasedValue

object Obj:
  
  inline def length[Tuple]: Int = loop[Tuple]

  private inline def loop[Tuple]: Int =
    inline erasedValue[Tuple] match
      case _: EmptyTuple => 0
      case _: (head *: tail) => 1 + loop[tail]

end Obj

// Same code, but in a trait instead of an object
trait Trait:

  inline def length[Tuple]: Int = loop[Tuple]

  private inline final def loop[Tuple]: Int =
    inline erasedValue[Tuple] match
      case _: EmptyTuple => 0
      case _: (head *: tail) => 1 + loop[tail]

end Trait

@main def run() =
  println(Obj.length[(Int, Int, String)]) // OK
  new Trait:
    println(length[(Int, Int, String)]) // ERROR

Output

[error] -- Error: bugreport.scala:18:38 
[error] 18 |  inline def length[Tuple]: Int = loop[Tuple]
[error]    |                                  ^^^^^^^^^^^
[error]    |undefined: Trait_this.loop # -1: TermRef(TermRef(NoPrefix,val Trait_this),loop) at inlining

When I call the variant in the trait, I get this error. But the same code works fine with an object.

Expectation

Both versions work.

@nicolasstucki nicolasstucki self-assigned this Mar 11, 2021
@julienrf julienrf added this to the 3.0.0-RC2 milestone Mar 18, 2021
@heksesang
Copy link

Should you be able to inline a method at a call site which shouldn't be accessible at that call site? If not, the problem here is that the object version in fact works.

@anatoliykmetyuk anatoliykmetyuk modified the milestones: 3.0.0-RC2, 3.0.x Mar 22, 2021
@odersky
Copy link
Contributor

odersky commented Mar 22, 2021

In fact, it seems the version with an object does not work either. I get:

-- Error: i11706.scala:83:2 ----------------------------------------------------
83 |  Obj.caseClass[Foo]
   |  ^^^^^^^^^^^^^^^^^^
   |object creation impossible, since def show(a: A): String in trait Show in package bugreport is not defined 
   |(Note that
   | parameter A in def show(a: A): String in trait Show in package bugreport does not match
   | parameter String *: scala.Tuple$package.EmptyTuple.type in def show(tuple: String *: scala.Tuple$package.EmptyTuple.type): String in anonymous class Object with bugreport.Show[head *: tail] {...}
   | )
   | This location contains code that was inlined from i11706.scala:72
   | This location contains code that was inlined from i11706.scala:70
   | This location contains code that was inlined from i11706.scala:57
1 error found

@anatoliykmetyuk anatoliykmetyuk modified the milestones: 3.0.x, 3.0.1-RC1 May 12, 2021
@julienrf julienrf changed the title Unable to call recursive inline method defined in a trait Unable to call recursive private inline method defined in a trait May 14, 2021
@julienrf
Copy link
Contributor Author

@odersky That error is a bit confusing.

I managed to reproduce and minimize further the error I originally reported: it happens only when we call a private recursive inline method. I have updated the issue description accordingly.

@julienrf julienrf modified the milestone: 3.0.1-RC1 May 14, 2021
@smarter smarter modified the milestones: 3.0.1-RC1, 3.0.2-RC1 May 25, 2021
@dwijnand dwijnand modified the milestones: 3.0.2-RC1, 3.1.0 Jul 14, 2021
@nicolasstucki nicolasstucki modified the milestones: 3.1.0, 3.2.0 Aug 24, 2021
@anatoliykmetyuk anatoliykmetyuk removed this from the 3.2.0 milestone Oct 4, 2021
mleclercq pushed a commit to mleclercq/endpoints that referenced this issue Nov 6, 2022
They can't be made `private` because of scala/scala3#11706

But private[generic] seems to work and is better than public
ckipp01 added a commit to ckipp01/dotty that referenced this issue May 14, 2023
nicolasstucki added a commit that referenced this issue May 16, 2023
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.

7 participants