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

"unable to read assertion at compile-time" under Scala 2.13 with -Xsource:3 enabled #1085

Closed
alphaho opened this issue Feb 19, 2023 · 3 comments
Assignees

Comments

@alphaho
Copy link
Collaborator

alphaho commented Feb 19, 2023

Have found a weird issue with SmartType in Scala 2.13.10 with -Xsource:3 enabled.

With the below snippet, it would report an error We were unable to read your assertion at compile-time. (attached at the bottom) during compilation.

However, this only happens after the scalac option -Xsource:3 is enabled.

Besides, if I add some calls before declaring object Blah extends Subtype[Int], like the commented code in the snippet, the compilation would succeed. I also tried providing different type parameter into Subtype, like String, the problem persists.

I have also tried both zio-prelude version "1.0.0-RC16" and "1.0.0-RC15". Both would fail.

import zio.prelude.Assertion._
import zio.prelude.Subtype

object AssertionsTest {
  // Blah(1) // uncommenting this line would make it compiled

  object Blah extends Subtype[Int] {
    // noinspection TypeAnnotation
    override def assertion = assert(greaterThanOrEqualTo(1))
  }

  type Blah = Blah.Type

  def foo(): Int =
    Blah(50)
}
[error]  Newtype Assertion Failed
[error] We were unable to read your assertion at compile-time.
[error] This is because you have annotated `def assertion` with its type signature:
[error]
[error]     override def assertion: QuotedAssertion[Int] = assert(...)
[error]
[error] Due to the macro machinery powering this feature, you MUST NOT ANNOTATE this method.
[error] Try deleting the type annotation and recompiling. Something like:
[error]
[error]     override def assertion = assert(...)
[error]
[error]     Blah(50)
[error]         ^
@nartamonov
Copy link

The same trouble, we've upgraded Scala 2.13.8 --> 2.13.10 and all newtypes broke, like those:

package object newtypes {
  type EmailAddress = EmailAddress.Type

  object EmailAddress extends Subtype[String] {
    override def assertion = assert {
      hasLength(lessThan(180)) && matches("""[^@]+@[^@]+""")
    }
  }

  type NonBlankString = NonBlankString.Type

  object NonBlankString extends Subtype[String] {
    override def assertion =
      assertCustom(s =>
        if (s.isBlank)
          Left(AssertionError.Failure("isBlank"))
        else Right({})
      )
  }
}

-Xsource:3 is enabled.

Workaround from @alphaho works fine, thank you 👍 . But it's temporarily solution and rather ugly:

package object newtypes {
  NonEmptyString("_")
  NonBlankString("_")
  EmailAddress("example@domain.tld")
  PhoneNumber("0000000")
  Password("****")

  // ... Definitions of newtypes ...
}

@guizmaii
Copy link
Member

guizmaii commented Apr 28, 2023

Is it the same issue as reported here:

?

If yes, we're waiting for Scala 2.13.11 which contains the fix

@alphaho
Copy link
Collaborator Author

alphaho commented Jun 20, 2023

Confirmed it's fixed with Scala 2.13.11

@alphaho alphaho closed this as completed Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants