Skip to content

Commit 7c08b14

Browse files
committed
Rust: Infer more () types
1 parent 331b98b commit 7c08b14

File tree

3 files changed

+967
-9
lines changed

3 files changed

+967
-9
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private TypeMention getTypeAnnotation(AstNode n) {
278278
)
279279
or
280280
exists(Function f |
281-
result = f.getRetType().getTypeRepr() and
281+
result = getReturnTypeMention(f) and
282282
n = f.getFunctionBody()
283283
)
284284
}
@@ -436,8 +436,7 @@ module CertainTypeInference {
436436
result = inferTupleRootType(n) and
437437
path.isEmpty()
438438
or
439-
result = inferAsyncBlockExprRootType(n) and
440-
path.isEmpty()
439+
result = inferBlockExprType(n, path)
441440
or
442441
result = inferArrayExprType(n) and
443442
path.isEmpty()
@@ -1854,7 +1853,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
18541853
}
18551854

18561855
private Type resolveRetType(TypePath path) {
1857-
result = this.getRetType().getTypeRepr().(TypeMention).resolveTypeAt(path)
1856+
result = getReturnTypeMention(this).resolveTypeAt(path)
18581857
}
18591858

18601859
pragma[nomagic]
@@ -2799,10 +2798,21 @@ private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
27992798
}
28002799

28012800
pragma[nomagic]
2802-
private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
2801+
private Type inferBlockExprType(BlockExpr be, TypePath path) {
28032802
// `typeEquality` handles the non-root case
2804-
exists(abe) and
2805-
result = getFutureTraitType()
2803+
if be instanceof AsyncBlockExpr
2804+
then (
2805+
path.isEmpty() and
2806+
result = getFutureTraitType()
2807+
or
2808+
not be.getStmtList().hasTailExpr() and
2809+
path = TypePath::singleton(getFutureOutputTypeParameter()) and
2810+
result instanceof UnitType
2811+
) else (
2812+
not be.getStmtList().hasTailExpr() and
2813+
path.isEmpty() and
2814+
result instanceof UnitType
2815+
)
28062816
}
28072817

28082818
final private class AwaitTarget extends Expr {

rust/ql/lib/codeql/rust/internal/TypeMention.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,30 @@ TypeMention getSelfParamTypeMention(SelfParam self) {
390390
result = self.getTypeRepr()
391391
}
392392

393+
/**
394+
* An element used to represent the implicit `()` return type of function.
395+
*/
396+
class ShorthandReturnTypeMention extends TypeMention instanceof Name {
397+
private Function f;
398+
399+
ShorthandReturnTypeMention() {
400+
this = f.getName() and
401+
not f.getRetType().hasTypeRepr()
402+
}
403+
404+
override Type resolveTypeAt(TypePath typePath) {
405+
typePath.isEmpty() and
406+
result instanceof UnitType
407+
}
408+
}
409+
410+
pragma[nomagic]
411+
TypeMention getReturnTypeMention(Function f) {
412+
result.(ShorthandReturnTypeMention) = f.getName()
413+
or
414+
result = f.getRetType().getTypeRepr()
415+
}
416+
393417
class DynTraitTypeReprMention extends TypeMention instanceof DynTraitTypeRepr {
394418
private DynTraitType dynType;
395419

0 commit comments

Comments
 (0)