Skip to content

Commit 2f0570f

Browse files
committed
wip8
1 parent 15c4c30 commit 2f0570f

2 files changed

Lines changed: 79 additions & 54 deletions

File tree

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

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ private module Input3 implements InputSig3 {
312312

313313
class Expr = Rust::Expr;
314314

315+
class SwitchExpr extends Rust::MatchExpr {
316+
Expr getExpr() { result = this.getScrutinee() }
317+
318+
Case getCase(int index) { result = this.getArm(index) }
319+
}
320+
321+
class Case extends Rust::MatchArm {
322+
AstNode getAPattern() { result = this.getPat() }
323+
324+
Expr getBody() { result = this.getExpr() }
325+
}
326+
315327
class ConditionalExpr extends IfExpr {
316328
Expr getThen() { result = super.getThen() }
317329
}
@@ -447,7 +459,46 @@ private module Input3 implements InputSig3 {
447459
)
448460
}
449461

450-
predicate inferCertainTypeInput = CertainTypeInferenceInput::inferCertainTypeInput/2;
462+
Type inferCertainTypeInput(AstNode n, TypePath path) {
463+
result = inferFunctionBodyType(n, path)
464+
or
465+
result = inferLiteralType(n, path, true)
466+
or
467+
result = inferRefPatType(n) and
468+
path.isEmpty()
469+
or
470+
result = inferRefExprType(n) and
471+
path.isEmpty()
472+
or
473+
result = inferCertainStructExprType(n, path)
474+
or
475+
result = inferCertainStructPatType(n, path)
476+
or
477+
result = inferRangeExprType(n) and
478+
path.isEmpty()
479+
or
480+
result = inferTupleRootType(n) and
481+
path.isEmpty()
482+
or
483+
result = inferBlockExprType(n, path)
484+
or
485+
result = inferArrayExprType(n) and
486+
path.isEmpty()
487+
or
488+
result = inferCastExprType(n, path)
489+
or
490+
exprHasUnitType(n) and
491+
path.isEmpty() and
492+
result instanceof UnitType
493+
or
494+
isPanicMacroCall(n) and
495+
path.isEmpty() and
496+
result instanceof NeverType
497+
or
498+
n instanceof ClosureExpr and
499+
path.isEmpty() and
500+
result = closureRootType()
501+
}
451502

452503
predicate typeEqualityInput(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
453504
prefix1.isEmpty() and
@@ -828,60 +879,12 @@ private TypePath closureParameterPath(int arity, int index) {
828879
TypePath::singleton(getTupleTypeParameter(arity, index)))
829880
}
830881

831-
/** Module for inferring certain type information. */
832-
private module CertainTypeInferenceInput {
833-
private Type inferCertainStructExprType(StructExpr se, TypePath path) {
834-
result = se.getPath().(TypeMention).getTypeAt(path)
835-
}
836-
837-
private Type inferCertainStructPatType(StructPat sp, TypePath path) {
838-
result = sp.getPath().(TypeMention).getTypeAt(path)
839-
}
882+
private Type inferCertainStructExprType(StructExpr se, TypePath path) {
883+
result = se.getPath().(TypeMention).getTypeAt(path)
884+
}
840885

841-
/**
842-
* Holds if `n` has complete and certain type information and if `n` has the
843-
* resulting type at `path`.
844-
*/
845-
Type inferCertainTypeInput(AstNode n, TypePath path) {
846-
result = inferFunctionBodyType(n, path)
847-
or
848-
result = inferLiteralType(n, path, true)
849-
or
850-
result = inferRefPatType(n) and
851-
path.isEmpty()
852-
or
853-
result = inferRefExprType(n) and
854-
path.isEmpty()
855-
or
856-
result = inferCertainStructExprType(n, path)
857-
or
858-
result = inferCertainStructPatType(n, path)
859-
or
860-
result = inferRangeExprType(n) and
861-
path.isEmpty()
862-
or
863-
result = inferTupleRootType(n) and
864-
path.isEmpty()
865-
or
866-
result = inferBlockExprType(n, path)
867-
or
868-
result = inferArrayExprType(n) and
869-
path.isEmpty()
870-
or
871-
result = inferCastExprType(n, path)
872-
or
873-
exprHasUnitType(n) and
874-
path.isEmpty() and
875-
result instanceof UnitType
876-
or
877-
isPanicMacroCall(n) and
878-
path.isEmpty() and
879-
result instanceof NeverType
880-
or
881-
n instanceof ClosureExpr and
882-
path.isEmpty() and
883-
result = closureRootType()
884-
}
886+
private Type inferCertainStructPatType(StructPat sp, TypePath path) {
887+
result = sp.getPath().(TypeMention).getTypeAt(path)
885888
}
886889

887890
private Type inferAssignmentOperationType(AstNode n, TypePath path) {

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,28 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
21492149
/** An expression. */
21502150
class Expr extends AstNode;
21512151

2152+
/**
2153+
* A switch expression.
2154+
*/
2155+
class SwitchExpr extends Expr {
2156+
/**
2157+
* Gets the expression being switched on.
2158+
*/
2159+
Expr getExpr();
2160+
2161+
/** Gets the case at the specified (zero-based) `index`. */
2162+
Case getCase(int index);
2163+
}
2164+
2165+
/** A case in a switch expression. */
2166+
class Case extends AstNode {
2167+
/** Gets a pattern being matched by this case. */
2168+
AstNode getAPattern();
2169+
2170+
/** Gets the body of this case. */
2171+
Expr getBody();
2172+
}
2173+
21522174
/** A ternary conditional expression. */
21532175
class ConditionalExpr extends Expr {
21542176
/** Gets the condition of this expression. */

0 commit comments

Comments
 (0)