Skip to content

Comparison can't be inlined when using inline values but can with litteral #12794

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
Iltotore opened this issue Jun 11, 2021 · 3 comments
Closed
Assignees

Comments

@Iltotore
Copy link
Contributor

Iltotore commented Jun 11, 2021

Compiler version

3.0.0

Minimized code

First sample:

@main def test = {
  compiletime.preAssert(2 > 1)
}

Second sample:

inline def foo: Int = 2

@main def test = {
  compiletime.preAssert(foo > 1)
}

compiletime.scala

import scala.quoted._

object compileTime {

  inline def preAssert(inline value: Boolean): Boolean = ${preAssertImpl('value)}

  private def preAssertImpl(expr: Expr[Boolean])(using quotes: Quotes): Expr[Boolean] = {

    expr.value match {

      case Some(false) => quotes.reflect.report.error(s"Compile-time assertion failed", expr)

      case None => quotes.reflect.report.warning(s"Unable to evaluate assertion at compile time", expr)

      case _ =>
    }

    expr
  }
}

Output

First sample: The code compiles and is inlined to true

Second sample: The code is _partially inlined (2 > 1 in the bytecode) and the macro can't retrieve the value at compiletime

Unable to evaluate assertion at compile time

Expectation

I expect the compiler to fully inline the second example in order to get the same result as the first one:

foo > 1
//to
2 > 1
//to
true
@odersky
Copy link
Contributor

odersky commented Jun 12, 2021

It would probably have to be

transparent inline def foo: Int = 2

Can you try with that variant?

@Iltotore
Copy link
Contributor Author

It works. Thank you.

I don't see any mention of this behaviour in the Transparent Inline method paragraph. If it's not mentionned, maybe this should be added ?

@som-snytt
Copy link
Contributor

Maybe this comment should be a faq: #12700 (comment)

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

No branches or pull requests

6 participants