Skip to content

inline if error suggests it expects a literal expression #10107

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
deusaquilus opened this issue Oct 28, 2020 · 4 comments · Fixed by #10114
Closed

inline if error suggests it expects a literal expression #10107

deusaquilus opened this issue Oct 28, 2020 · 4 comments · Fixed by #10114
Milestone

Comments

@deusaquilus
Copy link
Contributor

deusaquilus commented Oct 28, 2020

Minimized code

Say we make a simple macro that just returns the value true.

object Proc {
  inline def isTrue: Boolean = ${ isTrueImpl }
  def isTrueImpl(using qctx: QuoteContext) = {
    Expr(true) // even tried: Literal(Constant(true).seal.asExprOf[Boolean])
  }
}

Now if we use that with an inline if statement:

  inline def oneOf(): String = {
    inline if (isTrue)
      "foo"
    else
      "bar"
  }

Then when I run that:

oneOf()

The following error happens...

Output

[error]    |    cannot reduce inline if
[error]    |     its condition   miniquill.ListProc.isTrue
[error]    |     is not a constant value

Expectation

I think this should be more intelligent about looking at the AST and detecting if there is a constant value. I even tried making oneOf return Literal(Constant(true)) but it still did not work.

@bishabosha
Copy link
Member

this does not work because inline if looks at the static type of the expression - if you make isTrue a transparent inline def it works

@bishabosha
Copy link
Member

perhaps the error is misleading

@deusaquilus
Copy link
Contributor Author

deusaquilus commented Oct 28, 2020

Thanks @bishabosha! The message “it needs to be a constant” conveys the correct high-level understanding, adding “(and this expects a literal type)” in parenthesis would be a good addition though.

@bishabosha bishabosha changed the title inline if does not play well with macros inline if error suggests it expects a literal expression Oct 28, 2020
@nicolasstucki
Copy link
Contributor

Actually, it is a shame to fail on a true: Boolean. We can easily handle those cases.

odersky added a commit that referenced this issue Oct 30, 2020
Fix #10107: Improve `inline if` constant condition detraction and reporting
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 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.

4 participants