Skip to content

Commit 13023aa

Browse files
committed
wip
1 parent 553b90e commit 13023aa

2 files changed

Lines changed: 105 additions & 51 deletions

File tree

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

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ private predicate isPanicMacroCall(MacroExpr me) {
268268
me.getMacroCall().resolveMacro().(MacroRules).getName().getText() = "panic"
269269
}
270270

271-
Type inferCertainStructExprType(StructExpr se, TypePath path) {
271+
private Type inferStructExprType(StructExpr se, TypePath path) {
272272
result = se.getPath().(TypeMention).getTypeAt(path)
273273
}
274274

275-
Type inferCertainStructPatType(StructPat sp, TypePath path) {
275+
private Type inferStructPatType(StructPat sp, TypePath path) {
276276
result = sp.getPath().(TypeMention).getTypeAt(path)
277277
}
278278

@@ -1904,18 +1904,6 @@ private Type inferRefPatType(AstNode ref) {
19041904
)
19051905
}
19061906

1907-
pragma[nomagic]
1908-
private Type inferTryExprType(TryExpr te, TypePath path) {
1909-
exists(TypeParam tp, TypePath path0 |
1910-
result = inferType(te.getExpr(), path0) and
1911-
path0.isCons(TTypeParamTypeParameter(tp), path)
1912-
|
1913-
tp = any(ResultEnum r).getGenericParamList().getGenericParam(0)
1914-
or
1915-
tp = any(OptionEnum o).getGenericParamList().getGenericParam(0)
1916-
)
1917-
}
1918-
19191907
pragma[nomagic]
19201908
private StructType getStrStruct() { result = TDataType(any(Builtins::Str s)) }
19211909

@@ -2241,7 +2229,6 @@ TupleField resolveTupleFieldExpr(FieldExpr fe, DerefChain derefChain) {
22412229

22422230
private module Input3 implements InputSig3 {
22432231
private import rust as Rust
2244-
private import codeql.rust.elements.internal.OperationImpl::Impl as OperationImpl
22452232

22462233
predicate cacheRevRef() {
22472234
(implicitDerefChainBorrow(_, _, _) implies any())
@@ -2305,9 +2292,11 @@ private module Input3 implements InputSig3 {
23052292

23062293
class LogicalOrExpr extends BinaryExpr, Rust::LogicalOrExpr { }
23072294

2308-
abstract class Assignment extends BinaryExpr { }
2295+
final class Assignment = AssignmentImpl;
23092296

2310-
class AssignExpr extends Assignment, Rust::AssignmentExpr { }
2297+
abstract private class AssignmentImpl extends BinaryExpr { }
2298+
2299+
class AssignExpr extends AssignmentImpl, Rust::AssignmentExpr { }
23112300

23122301
class ParenExpr extends Expr instanceof Rust::ParenExpr {
23132302
Expr getExpr() { result = super.getExpr() }
@@ -2353,7 +2342,9 @@ private module Input3 implements InputSig3 {
23532342
Location getLocation() { result = this.getDefiningNode().getLocation() }
23542343
}
23552344

2356-
abstract class VariableDeclaration extends DeclarationImpl {
2345+
final class VariableDeclaration = VariableDeclarationImpl;
2346+
2347+
abstract private class VariableDeclarationImpl extends DeclarationImpl {
23572348
abstract predicate isCoercionSite();
23582349

23592350
abstract AstNode getPattern();
@@ -2363,7 +2354,7 @@ private module Input3 implements InputSig3 {
23632354
override TypeMention getDeclaringType() { none() }
23642355
}
23652356

2366-
private class LetExprDeclaration extends VariableDeclaration instanceof LetExpr {
2357+
private class LetExprDeclaration extends VariableDeclarationImpl instanceof LetExpr {
23672358
override predicate isCoercionSite() { not super.getPat() instanceof IdentPat }
23682359

23692360
override TypeMention getType() { none() }
@@ -2373,15 +2364,15 @@ private module Input3 implements InputSig3 {
23732364
override AstNode getInitializer() { result = super.getScrutinee() }
23742365
}
23752366

2376-
private class LetStmtDeclaration extends VariableDeclaration instanceof LetStmt {
2367+
private class LetStmtDeclaration extends VariableDeclarationImpl instanceof LetStmt {
23772368
override predicate isCoercionSite() {
23782369
super.hasTypeRepr()
23792370
or
23802371
// Due to "binding modes" the type of the pattern is not necessarily the
23812372
// same as the type of the initializer. However, when the pattern is an
23822373
// identifier pattern, its type is guaranteed to be the same as the type of the
23832374
// initializer.
2384-
not this.getPattern() instanceof IdentPat
2375+
not super.getPat() instanceof IdentPat
23852376
}
23862377

23872378
override TypeMention getType() { result = super.getTypeRepr() }
@@ -2391,7 +2382,7 @@ private module Input3 implements InputSig3 {
23912382
override AstNode getInitializer() { result = LetStmt.super.getInitializer() }
23922383
}
23932384

2394-
private class ConstDeclaration extends VariableDeclaration instanceof Const {
2385+
private class ConstDeclaration extends VariableDeclarationImpl instanceof Const {
23952386
override predicate isCoercionSite() { any() }
23962387

23972388
override TypeMention getType() { result = super.getTypeRepr() }
@@ -2401,7 +2392,7 @@ private module Input3 implements InputSig3 {
24012392
override AstNode getInitializer() { result = super.getBody() }
24022393
}
24032394

2404-
private class StaticDeclaration extends VariableDeclaration instanceof Static {
2395+
private class StaticDeclaration extends VariableDeclarationImpl instanceof Static {
24052396
override predicate isCoercionSite() { any() }
24062397

24072398
override TypeMention getType() { result = super.getTypeRepr() }
@@ -2411,20 +2402,22 @@ private module Input3 implements InputSig3 {
24112402
override AstNode getInitializer() { result = super.getBody() }
24122403
}
24132404

2414-
abstract class Field extends DeclarationImpl {
2405+
final class Field = FieldImpl;
2406+
2407+
abstract private class FieldImpl extends DeclarationImpl {
24152408
// no case for variants as those can only be destructured using pattern matching
24162409
abstract Struct getStruct();
24172410

24182411
override TypeMention getDeclaringType() { result = this.getStruct() }
24192412
}
24202413

2421-
private class StructFieldDecl extends Field instanceof StructField {
2414+
private class StructFieldDecl extends FieldImpl instanceof StructField {
24222415
override Struct getStruct() { this = result.getAStructField() }
24232416

24242417
override TypeMention getType() { result = StructField.super.getTypeRepr() }
24252418
}
24262419

2427-
private class TupleFieldDecl extends Field instanceof TupleField {
2420+
private class TupleFieldDecl extends FieldImpl instanceof TupleField {
24282421
override Struct getStruct() { this = result.getATupleField() }
24292422

24302423
override TypeMention getType() { result = TupleField.super.getTypeRepr() }
@@ -2470,9 +2463,8 @@ private module Input3 implements InputSig3 {
24702463

24712464
final class Parameter = ParameterImpl;
24722465

2473-
/** A parameter, including enum/struct constructor fields. */
2474-
abstract private class ParameterImpl extends VariableDeclaration {
2475-
override predicate isCoercionSite() { any() } // doesn't really matter, since there are no initializers
2466+
abstract private class ParameterImpl extends VariableDeclarationImpl {
2467+
override predicate isCoercionSite() { any() } // doesn't really matter, since there are no initializers/default values
24762468

24772469
override AstNode getInitializer() { none() }
24782470
}
@@ -2552,8 +2544,7 @@ private module Input3 implements InputSig3 {
25522544
override TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) { none() }
25532545

25542546
override TypeParameter getTypeParameter(int pos) {
2555-
result.(TypeParamTypeParameter).getTypeParam() =
2556-
this.getTypeItem().getGenericParamList().getTypeParam(pos)
2547+
result = TTypeParamTypeParameter(this.getTypeItem().getGenericParamList().getTypeParam(pos))
25572548
}
25582549

25592550
override TypeMention getType() { result = this.getTypeItem() }
@@ -2644,10 +2635,10 @@ private module Input3 implements InputSig3 {
26442635

26452636
pragma[nomagic]
26462637
override Type getTypeArgument(int pos, TypePath path) {
2638+
result = getCallExprTypeArgument(this, pos, path)
2639+
or
26472640
result =
26482641
this.(MethodCallExpr).getGenericArgList().getTypeArg(pos).(TypeMention).getTypeAt(path)
2649-
or
2650-
result = getCallExprTypeArgument(this, pos, path)
26512642
}
26522643

26532644
override Expr getArgument(int i) {
@@ -2769,14 +2760,7 @@ private module Input3 implements InputSig3 {
27692760
}
27702761

27712762
pragma[nomagic]
2772-
private Type inferInvocationArgumentType0(Invocation invocation, int pos, TypePath path) {
2773-
exists(FunctionPosition fpos |
2774-
pos = fpos.asPosition() and
2775-
result =
2776-
invocation.(AssocFunctionResolution::OperationAssocFunctionCall).getTypeAt(fpos, path)
2777-
)
2778-
or
2779-
not invocation instanceof Operation and
2763+
private Type inferNonAssocFunctionCallArgumentType(Invocation invocation, int pos, TypePath path) {
27802764
not invocation instanceof AssocFunctionCall and
27812765
result = inferType(invocation.getArgument(pos), path)
27822766
}
@@ -2785,7 +2769,7 @@ private module Input3 implements InputSig3 {
27852769
Type inferInvocationArgumentType(
27862770
Invocation invocation, string derefChainBorrow, int pos, TypePath path
27872771
) {
2788-
result = inferInvocationArgumentType0(invocation, pos, path)
2772+
result = inferNonAssocFunctionCallArgumentType(invocation, pos, path)
27892773
or
27902774
invocation =
27912775
any(AssocFunctionCall afc |
@@ -2892,7 +2876,7 @@ private module Input3 implements InputSig3 {
28922876
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
28932877
// the implicit deref
28942878
if invocation instanceof IndexExpr or invocation instanceof DerefExpr
2895-
then path0.isCons(getRefTypeParameter(_), path)
2879+
then path = TypePath::cons(getRefTypeParameter(_), path0)
28962880
else path = path0
28972881
)
28982882
}
@@ -2988,9 +2972,11 @@ private module Input3 implements InputSig3 {
29882972
result = inferRefExprType(n) and
29892973
path.isEmpty()
29902974
or
2991-
result = inferCertainStructExprType(n, path)
2975+
result = inferStructExprType(n, path)
29922976
or
2993-
result = inferCertainStructPatType(n, path)
2977+
result = inferStructPatType(n, path)
2978+
or
2979+
result = inferAssignmentOperationType(n, path)
29942980
or
29952981
result = inferRangeExprType(n) and
29962982
path.isEmpty()
@@ -3003,6 +2989,8 @@ private module Input3 implements InputSig3 {
30032989
result = inferArrayExprType(n) and
30042990
path.isEmpty()
30052991
or
2992+
result = inferArgList(n, path)
2993+
or
30062994
exprHasUnitType(n) and
30072995
path.isEmpty() and
30082996
result instanceof UnitType
@@ -3095,6 +3083,17 @@ private module Input3 implements InputSig3 {
30953083
prefix2.isEmpty() and
30963084
prefix1 = path
30973085
)
3086+
or
3087+
exists(TypeParam tp, Enum e |
3088+
n1 = n2.(TryExpr).getExpr() and
3089+
tp = e.getGenericParamList().getGenericParam(0) and
3090+
prefix1 = TypePath::singleton(TTypeParamTypeParameter(tp)) and
3091+
prefix2.isEmpty()
3092+
|
3093+
e instanceof ResultEnum
3094+
or
3095+
e instanceof OptionEnum
3096+
)
30983097
}
30993098

31003099
pragma[nomagic]
@@ -3110,18 +3109,12 @@ private module Input3 implements InputSig3 {
31103109

31113110
pragma[nomagic]
31123111
Type inferTypeSpecific(AstNode n, TypePath path) {
3113-
result = inferAssignmentOperationType(n, path)
3114-
or
3115-
result = inferTryExprType(n, path)
3116-
or
31173112
result = inferLiteralType(n, path, false)
31183113
or
31193114
result = inferAwaitExprType(n, path)
31203115
or
31213116
result = inferForLoopExprType(n, path)
31223117
or
3123-
result = inferArgList(n, path)
3124-
or
31253118
result = inferDeconstructionPatType(n, path)
31263119
or
31273120
result = inferUnknownType(n, path)

0 commit comments

Comments
 (0)