Skip to content

Can't branch on compiletime.testing.typeChecks #7040

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
bishabosha opened this issue Aug 14, 2019 · 2 comments
Closed

Can't branch on compiletime.testing.typeChecks #7040

bishabosha opened this issue Aug 14, 2019 · 2 comments

Comments

@bishabosha
Copy link
Member

minimized code

import scala.compiletime.testing.typeChecks
import scala.compiletime.error

inline def assertDoesNotCompile(inline code: String): Unit = {
  if (typeChecks(code)) {
    error("Type-checking succeeded unexpectedly.")
  } else {
  }
}

val test1 = assertDoesNotCompile("1")
val test2 = assertDoesNotCompile("1.noSuchMethod")

test1 behaves correctly.

expectation

test2 should be unit, but it is a compiletime error.

To get the correct behaviour, this macro works:

def typeChecksOrErrImpl(code: String) given (qctx: QuoteContext): Expr[Unit] = {
  import qctx.tasty._
  if (typing.typeChecks(code)) {
    error("Type-checking succeeded unexpectedly.", rootPosition)
    '{}
  } else {
    '{}
  }
}
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Aug 29, 2019
@nicolasstucki
Copy link
Contributor

In general you should use inline if if you what to make sure the condition of the if is constant folded.

  inline if (typeChecks(code)) {
    error("Type-checking succeeded unexpectedly.")
  } else {
  }

@nicolasstucki
Copy link
Contributor

But #7129 makes the original code work.

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

2 participants