Skip to content

Commit

Permalink
Support type variables in type regions (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaelmBleidd authored Nov 15, 2023
1 parent 837bd13 commit 39f1845
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
12 changes: 9 additions & 3 deletions usvm-jvm/src/main/kotlin/org/usvm/machine/JcTypeSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ class JcTypeSystem(
private val hierarchy = HierarchyExtensionImpl(cp)

override fun isSupertype(supertype: JcType, type: JcType): Boolean =
type.isAssignable(supertype) ||
// It is possible when, for example, the returning type of a method is a type variable
(supertype is JcTypeVariable && type.isAssignable(cp.objectType) && supertype.bounds.all { type.isAssignable(it) })
when {
supertype == type -> true
supertype is JcTypeVariable ->
isSupertype(cp.objectType, type) && supertype.bounds.all { isSupertype(it, type) }

type is JcTypeVariable -> supertype == cp.objectType || type.bounds.any { isSupertype(supertype, it) }
else -> type.isAssignable(supertype)
}


private fun isInterface(type: JcType): Boolean =
(type as? JcClassType)?.jcClass?.isInterface ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.usvm.samples.types;

public class GenericWithUpperBound<C extends Comparable<C>> {
@SuppressWarnings({"DataFlowIssue"})
public int excludeComparable(C value) {
if (!(value instanceof Comparable<?>)) {
return 0;
}

return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.usvm.samples.types

import org.junit.jupiter.api.Disabled
import org.usvm.samples.JavaMethodTestRunner
import org.usvm.test.util.checkers.eq
import kotlin.test.Test

internal class GenericWithUpperBoundTest : JavaMethodTestRunner() {
@Test
@Disabled("We do not support generics, so we may generate `GenericWithUpperBound<String>` here that leads to fail")
fun testExcludeComparable() {
checkDiscoveredProperties(
GenericWithUpperBound<Int>::excludeComparable,
eq(2),
{ _, value, r -> value == null && r == 0 },
{ _, value, r -> value != null && r == 1 },
)
}
}

0 comments on commit 39f1845

Please sign in to comment.