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

[BUG] refined-type-plugin is not getting applied when evaluating dynamic variables at compile time #823

Open
lenguyenthanh opened this issue Jun 12, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@lenguyenthanh
Copy link

Describe the bug
refined-type-plugin is not getting applied when evaluating dynamic variables at compile time

To Reproduce
Update demo code with

data class PositiveIntEven private constructor(val value: Int) {
    companion object : Refined<Int, PositiveIntEven>(::PositiveIntEven, PositiveInt, Even)
}

fun main() {
    val n = -1
    val result = PositiveInt(n).value
}

It is still compiled and throw an exception in runtime.

Output:

Exception in thread "main" java.lang.IllegalArgumentException: -1 should be > 0
	at io.arrowkt.example.ContextKt.main(Context.kt:22)
	at io.arrowkt.example.ContextKt.main(Context.kt)

Expected behavior
The code above should cause a compiler error:

error: Prefer a safe alternative such as PositiveInt.orNull(n) or for explicit use of exceptions `PositiveInt.require(n)`
@lenguyenthanh lenguyenthanh added the bug Something isn't working label Jun 12, 2021
@rachelcarmena
Copy link
Member

Thanks @lenguyenthanh !

I think refined-type-plugin is properly published and it's being applied. It seems the issue comes from the plugin.

Including some cases here.

Example 1

Case 1.1 - Expected result

Input

val result = PositiveInt(-1).value

Compilation output

-1 should be > 0 : arrow.refinement.numbers.PositiveInt.Companion.invoke

It's the expected output. The plugin is being applied.

Case 1.2 - Expected result

Input

fun main() {
    val result = PositiveInt(-1).value
}

Compilation output

-1 should be > 0 : arrow.refinement.numbers.PositiveInt.Companion.invoke

It's the expected output. The plugin is being applied.

Example 2

Case 2.1 - Expected result

Input

val n = -1
val result = PositiveInt(n).value

Compilation output

val n = -1 can't be verified at compile time. Use `Predicate.orNull(val n = -1)` for safe
access or `Predicate.require(val n = -1)` for explicit unsafe instantiation

It's the expected output according to this test of the plugin. The plugin is being applied.

Case 2.2 - NOT expected result

Input

fun main() {
    val n = -1
    val result = PositiveInt(n).value
}

main or any other function.

Compilation output

BUILD SUCCESSFUL

It's not the expected output. The plugin doesn't show the same behavior as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants