Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fe260c

Browse files
smowtonigfoo
authored andcommitted
Convert type-parameter-out-of-scope warning into consistency query
The warning in the extractor is inaccurate due to references to enclosing types' type parameters. A consistency query can check that the type parameter is indeed in scope exploiting broader knowledge of the enclosing types.
1 parent 36356c2 commit 7fe260c

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,7 @@ open class KotlinUsesExtractor(
863863

864864
fun useTypeParameter(param: IrTypeParameter) =
865865
TypeResult(
866-
tw.getLabelFor<DbTypevariable>(getTypeParameterLabel(param)) {
867-
// Any type parameter that is in scope should have been extracted already
868-
// in extractClassSource or extractFunction
869-
logger.error("Missing type parameter label")
870-
},
866+
tw.getLabelFor<DbTypevariable>(getTypeParameterLabel(param)),
871867
useType(eraseTypeParameter(param)).javaResult.signature,
872868
param.name.asString()
873869
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java
2+
3+
Type getAMentionedType(RefType type) {
4+
result = type
5+
or
6+
result = getAMentionedType(type).(Array).getElementType()
7+
or
8+
result = getAMentionedType(type).(ParameterizedType).getATypeArgument()
9+
or
10+
result = getAMentionedType(type).(NestedType).getEnclosingType()
11+
}
12+
13+
Type getATypeUsedInClass(RefType type) {
14+
// Base cases:
15+
result = type.getAField().getType()
16+
or
17+
result = type.getAMethod().getReturnType()
18+
or
19+
result = type.getAMethod().getParameterType(_)
20+
or
21+
result = any(Expr e | e.getEnclosingCallable().getDeclaringType() = type).getType()
22+
// Structural recursion over types:
23+
or
24+
result = getAMentionedType(getATypeUsedInClass(type))
25+
}
26+
27+
TypeVariable getATypeVariableInScope(RefType type) {
28+
result = type.getACallable().(GenericCallable).getATypeParameter()
29+
or
30+
result = type.(GenericType).getATypeParameter()
31+
or
32+
result = getAMentionedType(type.(ParameterizedType).getATypeArgument())
33+
or
34+
result = getATypeVariableInScope(type.getEnclosingType())
35+
}
36+
37+
from ClassOrInterface typeUser, TypeVariable outOfScope
38+
where outOfScope = getAMentionedType(typeUser) and not outOfScope = getATypeVariableInScope(typeUser)
39+
select "Type " + typeUser + " uses out-of-scope type variable " + outOfScope

0 commit comments

Comments
 (0)