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

Overridden inline in class instances does not propogate correctly #8144

Open
deusaquilus opened this issue Jan 30, 2020 · 3 comments
Open

Overridden inline in class instances does not propogate correctly #8144

deusaquilus opened this issue Jan 30, 2020 · 3 comments
Labels

Comments

@deusaquilus
Copy link

@deusaquilus deusaquilus commented Jan 30, 2020

minimized code

Say that we create a class with an inline member and a macro that prints it during compile-time.

package example
import scala.quoted._
import scala.quoted.matching._
import scala.compiletime._

// Create a class
class DualTestClass {
  inline def v = "foo"
  inline def compiletimeValue: Unit = ${ DualTestClass.compiletimeValueImpl('v) }
}
// Add a macro that prints an inline paramter
object DualTestClass {
  def compiletimeValueImpl(input: Expr[String])(given qctx: QuoteContext): Expr[Unit] = {
    val v = input match { case Value(v) => v }
    println("*********** Compile-Time Value is: " + v + " *******************")
    '{ () }
  }
}
// Then create a instance of that class (using a factory-pattern just because...)
object DualTestClassMaker {
  def make: DualTestClass =
    new DualTestClass {
      override inline def v = "bar"
    }
}

Then let's run this:

package example

@main def dualTestClassTester() = {
  DualTestClassMaker.make.compiletimeValue
}

When the code is compiling, it will print the value:

*********** Compile-Time Value is: foo *******************

As opposed to this which is the expectation.

*********** Compile-Time Value is: bar *******************
@deusaquilus

This comment has been minimized.

Copy link
Author

@deusaquilus deusaquilus commented Jan 30, 2020

Also note that when I properly extend DualTestClass like this:

class DualTestClassExt extends DualTestClass {
  override inline def v = "bar"
}

and then run it:

@main def dualTestClassTester() = {
  new DualTestClassExt().compiletimeValue
}

Then the correct value is printed:

*********** Compile-Time Value is: bar *******************
@deusaquilus

This comment has been minimized.

Copy link
Author

@deusaquilus deusaquilus commented Jan 30, 2020

It is also interesting to note that if you try to create the subclass DualTestClassExt inside of a macro (I call it DualTestInst) the same error happens.
Here's the macro:

object DualTestClassMaker {
  import scala.quoted._
  inline def makeMacro: DualTestClass = ${ dualTestImpl }
  def dualTestImpl(given qctx: QuoteContext): Expr[DualTestClass] = {
    '{
      class DualTestInst extends DualTestClass {
        override inline def v = "bar"
      }
      new DualTestInst
    }
  }
}

Then I run it:

@main def dualTestClassTester() = {
  DualTestClassMaker.makeMacro.compiletimeValue
}

It prints out:

*********** Compile-Time Value is: foo *******************

Which is also incorrect.

@AleksanderBG

This comment has been minimized.

Copy link
Contributor

@AleksanderBG AleksanderBG commented Feb 6, 2020

As far as I can tell, the inline mechanism functions correctly in the examples you gave. You seem to need to take into account that what matters for inline are the static arguments to calls (even more specifically: the types of the static arguments), not the runtime ones.

For instance, in your first example, the static type of the object you call compiletimeValue on is the actual DualTestClass, not the anonymous class that you create inside the body of make. If you made make an inline method, I believe it would work according to your expectations.

If this clears up your doubts, I'll close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.