Skip to content

Commit 129c0e3

Browse files
committed
Rust, WIP: Performance tweaks
1 parent 07312a8 commit 129c0e3

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,19 @@ cached
936936
private module Cached {
937937
private import codeql.rust.internal.CachedStages
938938

939-
pragma[inline]
940-
private Type inferTypeDeref(AstNode n, TypePath path) {
941-
exists(Type t |
942-
t = inferType(n) and
943-
if t = TRefType()
944-
then
945-
// for reference types, lookup members in the type being referenced
946-
result = inferType(n, TypePath::cons(TRefTypeParameter(), path))
947-
else result = inferType(n, path)
939+
pragma[nomagic]
940+
private Type receiverRootType(Expr e) {
941+
any(MethodCallExpr mce).getReceiver() = e and
942+
result = inferType(e)
943+
}
944+
945+
pragma[nomagic]
946+
private Type inferReceiverType(Expr e, TypePath path) {
947+
exists(Type root | root = receiverRootType(e) |
948+
// for reference types, lookup members in the type being referenced
949+
if root = TRefType()
950+
then result = inferType(e, TypePath::cons(TRefTypeParameter(), path))
951+
else result = inferType(e, path)
948952
)
949953
}
950954

@@ -955,7 +959,7 @@ private module Cached {
955959

956960
string getField() { result = mce.getIdentifier().getText() }
957961

958-
Type resolveTypeAt(TypePath path) { result = inferTypeDeref(this, path) }
962+
Type resolveTypeAt(TypePath path) { result = inferReceiverType(this, path) }
959963
}
960964

961965
private module IsInstantiationOfInput implements IsInstantiationOfSig<ReceiverExpr> {
@@ -1007,10 +1011,20 @@ private module Cached {
10071011
)
10081012
}
10091013

1014+
pragma[inline]
1015+
private Type inferRootTypeDeref(AstNode n) {
1016+
exists(Type t |
1017+
t = inferType(n) and
1018+
// for reference types, lookup members in the type being referenced
1019+
if t = TRefType()
1020+
then result = inferType(n, TypePath::singleton(TRefTypeParameter()))
1021+
else result = t
1022+
)
1023+
}
1024+
10101025
pragma[nomagic]
10111026
private Type getFieldExprLookupType(FieldExpr fe, string name) {
1012-
result = inferTypeDeref(fe.getContainer(), TypePath::nil()) and
1013-
name = fe.getIdentifier().getText()
1027+
result = inferRootTypeDeref(fe.getContainer()) and name = fe.getIdentifier().getText()
10141028
}
10151029

10161030
/**

0 commit comments

Comments
 (0)