Java: Fix ClassInstanceExpr::isDiamond not working for anonymous classes - #15429
Marcono1234 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Possibly a bit irritating that the test is under /java7/ but now requires Java 9. But is this fine nonetheless since the diamond syntax was introduced in Java 7 (but extended in Java 9)?
| this.getAnonymousClass().getASupertype() instanceof ParameterizedType and | ||
| // Ignore Kotlin code; otherwise seems to erroneously match `object` expression with explicit | ||
| // type arguments, possibly because extractor is not properly extracting the type args? | ||
| not this.getCompilationUnit().isKotlinSourceFile() |
There was a problem hiding this comment.
@igfoo is there a quick fix for the Kotlin case rather than hack Kotlin source out like this? @Marcono1234 for clarity could you show a Kotlin snippet that otherwise behaves badly?
There was a problem hiding this comment.
If I omit that exclusion of Kotlin source files and then run the following query on the database for https://github.com/Kotlin/kotlinx.coroutines (latest database from GitHub was built with CodeQL CLI 2.16.1)
import java
from ClassInstanceExpr e
where
e.isDiamond() and
e.getCompilationUnit().isKotlinSourceFile()
select eit finds for example this expression with an explicit type argument:
https://github.com/Kotlin/kotlinx.coroutines/blob/8c516f5ab1fcc39629d2838489598135eedd7b80/kotlinx-coroutines-core/common/test/AbstractCoroutineTest.kt#L13C25-L13C59
Though maybe the isDiamond predicate does not make sense for Kotlin in the first place; at least there is (to my knowledge) no <> syntax but instead the type arguments are simply omitted if they can be inferred by the compiler.
Or at least the name isDiamond might be a bit confusing then.
There was a problem hiding this comment.
Yep, that looks like a bug in our Kotlin support at the moment. Excluding like this looks fine for now. I've made an internal ticket to remind us to come back to this.
Previously it was only working for non anonymous classes because it required that
getType() instanceof ParameterizedClasswhich does not hold for anonymous classes.