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

Child class cannot access exported inline methods from parent class #16469

Closed
mushtaq opened this issue Dec 6, 2022 · 4 comments · Fixed by #16757
Closed

Child class cannot access exported inline methods from parent class #16469

mushtaq opened this issue Dec 6, 2022 · 4 comments · Fixed by #16757
Assignees
Labels
area:export area:inline itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@mushtaq
Copy link

mushtaq commented Dec 6, 2022

Compiler version

3.2.1

Minimized code

//> using scala "3.2.1"

class Context {
  def normalMethod(): String = "normal"
  inline def inlineMethod(): String = "inline"
}

class Script(ctx: Context) {
  export ctx.*
  normalMethod() // works
  inlineMethod() // works
}

class MyScript(context: Context) extends Script(context) {
  normalMethod() // works
  inlineMethod() // error
}

Output

Compiling project (Scala 3.2.1, JVM)
[error] ./scala/inline-export-inherit.sc:16:3: ctx cannot be accessed as a member of (Script_this : (MyScript.this : inline-export-inherit.MyScript)) from class MyScript.
[error]   inlineMethod() // error
[error]   ^^^^^^^^^^^^^^

Expectation

It should compile unless the restriction is intentional. In that case, the reason should be mentioned in the docs.

Making ctx a val in Script constructor makes it compile.

@mushtaq mushtaq added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 6, 2022
@WojciechMazur
Copy link
Contributor

It seemed to work until Scala 3.1.2
Bisect points to b3ab102 - @bishabosha backported there changes made by @odersky in #14051

@WojciechMazur WojciechMazur added regression This worked in a previous version but doesn't anymore area:export and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 6, 2022
@nicolasstucki
Copy link
Contributor

The export generates the inlineMethod in Script

fianl inline def inlineMethod(): String = Script.this.ctx.inlineMethod()

If we define this method manually we see that it becomes

final inline def inlineMethod(): String = this.Script$$inline$ctx.inlineMethod():String
final def Script$$inline$ctx: Context = Script.this.ctx

This implies that when we generate the exports, we are not transforming the contents of the inline export accessor with PrepareInlineable or we are doing it wrong.

@nicolasstucki
Copy link
Contributor

We probably do something wrong here. Maybe we use the wrong context or the transformation is discarded.

@nicolasstucki
Copy link
Contributor

Half of the issue is that we need to call registerInlineInfo setting the forwarder as the context owner. The second thing is to add the inline accessors to the class.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 24, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 31, 2023
odersky added a commit that referenced this issue Feb 12, 2023
* Fix context owner of `PrepareInlineable.registerInlineInfo`. It should
have as owner the inline method (`forwarder` in this case).
* Eagerly compute the inlinable RHS. Because we create the forwarder
already typed, we do not evaluate/force in typer the
`LazyBodyAnnotation` which is what triggers the computation of the
inline accessors. This happened in posttyper which was too late. There
might be a cleaner solution to this problem, maybe forcing the
annotation explicitly.

Fixes #14131
Fixes #16469
@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:export area:inline itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants