mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rust: Infer more () types
This commit is contained in:
@@ -278,7 +278,7 @@ private TypeMention getTypeAnnotation(AstNode n) {
|
|||||||
)
|
)
|
||||||
or
|
or
|
||||||
exists(Function f |
|
exists(Function f |
|
||||||
result = f.getRetType().getTypeRepr() and
|
result = getReturnTypeMention(f) and
|
||||||
n = f.getFunctionBody()
|
n = f.getFunctionBody()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -436,8 +436,7 @@ module CertainTypeInference {
|
|||||||
result = inferTupleRootType(n) and
|
result = inferTupleRootType(n) and
|
||||||
path.isEmpty()
|
path.isEmpty()
|
||||||
or
|
or
|
||||||
result = inferAsyncBlockExprRootType(n) and
|
result = inferBlockExprType(n, path)
|
||||||
path.isEmpty()
|
|
||||||
or
|
or
|
||||||
result = inferArrayExprType(n) and
|
result = inferArrayExprType(n) and
|
||||||
path.isEmpty()
|
path.isEmpty()
|
||||||
@@ -1854,7 +1853,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Type resolveRetType(TypePath path) {
|
private Type resolveRetType(TypePath path) {
|
||||||
result = this.getRetType().getTypeRepr().(TypeMention).resolveTypeAt(path)
|
result = getReturnTypeMention(this).resolveTypeAt(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pragma[nomagic]
|
pragma[nomagic]
|
||||||
@@ -2798,11 +2797,38 @@ private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
|
|||||||
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
|
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private predicate isReturnExprCfgAncestor(AstNode n) {
|
||||||
|
n instanceof ReturnExpr
|
||||||
|
or
|
||||||
|
exists(AstNode mid |
|
||||||
|
isReturnExprCfgAncestor(mid) and
|
||||||
|
n = mid.getParentNode() and
|
||||||
|
n.getEnclosingCfgScope() = mid.getEnclosingCfgScope()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pragma[nomagic]
|
pragma[nomagic]
|
||||||
private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
|
predicate isUnitBlockExpr(BlockExpr be) {
|
||||||
|
not be.getStmtList().hasTailExpr() and
|
||||||
|
not isReturnExprCfgAncestor(be)
|
||||||
|
}
|
||||||
|
|
||||||
|
pragma[nomagic]
|
||||||
|
private Type inferBlockExprType(BlockExpr be, TypePath path) {
|
||||||
// `typeEquality` handles the non-root case
|
// `typeEquality` handles the non-root case
|
||||||
exists(abe) and
|
if be instanceof AsyncBlockExpr
|
||||||
result = getFutureTraitType()
|
then (
|
||||||
|
path.isEmpty() and
|
||||||
|
result = getFutureTraitType()
|
||||||
|
or
|
||||||
|
isUnitBlockExpr(be) and
|
||||||
|
path = TypePath::singleton(getFutureOutputTypeParameter()) and
|
||||||
|
result instanceof UnitType
|
||||||
|
) else (
|
||||||
|
isUnitBlockExpr(be) and
|
||||||
|
path.isEmpty() and
|
||||||
|
result instanceof UnitType
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
final private class AwaitTarget extends Expr {
|
final private class AwaitTarget extends Expr {
|
||||||
|
|||||||
@@ -443,6 +443,30 @@ TypeMention getSelfParamTypeMention(SelfParam self) {
|
|||||||
result = self.getTypeRepr()
|
result = self.getTypeRepr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An element used to represent the implicit `()` return type of function.
|
||||||
|
*/
|
||||||
|
class ShorthandReturnTypeMention extends TypeMention instanceof Name {
|
||||||
|
private Function f;
|
||||||
|
|
||||||
|
ShorthandReturnTypeMention() {
|
||||||
|
this = f.getName() and
|
||||||
|
not f.getRetType().hasTypeRepr()
|
||||||
|
}
|
||||||
|
|
||||||
|
override Type resolveTypeAt(TypePath typePath) {
|
||||||
|
typePath.isEmpty() and
|
||||||
|
result instanceof UnitType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pragma[nomagic]
|
||||||
|
TypeMention getReturnTypeMention(Function f) {
|
||||||
|
result.(ShorthandReturnTypeMention) = f.getName()
|
||||||
|
or
|
||||||
|
result = f.getRetType().getTypeRepr()
|
||||||
|
}
|
||||||
|
|
||||||
class DynTraitTypeReprMention extends TypeMention instanceof DynTraitTypeRepr {
|
class DynTraitTypeReprMention extends TypeMention instanceof DynTraitTypeRepr {
|
||||||
private DynTraitType dynType;
|
private DynTraitType dynType;
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user