Skip to content

Commit ce35990

Browse files
committed
wip2
1 parent 72475c3 commit ce35990

1 file changed

Lines changed: 85 additions & 80 deletions

File tree

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

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,17 +1847,6 @@ private module AssocFunctionResolution {
18471847
}
18481848
}
18491849

1850-
pragma[nomagic]
1851-
private Type inferUnknownType(AstNode n, TypePath path) {
1852-
result = TUnknownType() and
1853-
path.isEmpty() and
1854-
(
1855-
n.(AssocFunctionResolution::AssocFunctionCall).resolutionDependsOnReturnType()
1856-
or
1857-
isPanicMacroCall(n)
1858-
)
1859-
}
1860-
18611850
pragma[nomagic]
18621851
private Type getFieldExprLookupType(FieldExpr fe, string name, DerefChain derefChain) {
18631852
exists(TypePath path |
@@ -1877,20 +1866,6 @@ private Type getFieldExprLookupType(FieldExpr fe, string name, DerefChain derefC
18771866
)
18781867
}
18791868

1880-
/**
1881-
* Gets the struct field that the field expression `fe` resolves to, if any.
1882-
*/
1883-
cached
1884-
StructField resolveStructFieldExpr(FieldExpr fe, DerefChain derefChain) {
1885-
M3::CachedStage::ref() and
1886-
exists(string name, DataType ty |
1887-
ty = getFieldExprLookupType(fe, pragma[only_bind_into](name), derefChain)
1888-
|
1889-
result = ty.(StructType).getTypeItem().getStructField(pragma[only_bind_into](name)) or
1890-
result = ty.(UnionType).getTypeItem().getStructField(pragma[only_bind_into](name))
1891-
)
1892-
}
1893-
18941869
pragma[nomagic]
18951870
private Type getTupleFieldExprLookupType(FieldExpr fe, int pos, DerefChain derefChain) {
18961871
exists(string name |
@@ -1899,21 +1874,6 @@ private Type getTupleFieldExprLookupType(FieldExpr fe, int pos, DerefChain deref
18991874
)
19001875
}
19011876

1902-
/**
1903-
* Gets the tuple field that the field expression `fe` resolves to, if any.
1904-
*/
1905-
cached
1906-
TupleField resolveTupleFieldExpr(FieldExpr fe, DerefChain derefChain) {
1907-
M3::CachedStage::ref() and
1908-
exists(int i |
1909-
result =
1910-
getTupleFieldExprLookupType(fe, pragma[only_bind_into](i), derefChain)
1911-
.(StructType)
1912-
.getTypeItem()
1913-
.getTupleField(pragma[only_bind_into](i))
1914-
)
1915-
}
1916-
19171877
/** Gets the root type of the reference expression `ref`. */
19181878
pragma[nomagic]
19191879
private Type inferRefExprType(RefExpr ref) {
@@ -2205,51 +2165,85 @@ private TupleType inferArgList(ArgList args, TypePath path) {
22052165
)
22062166
}
22072167

2208-
/** Holds if `n` is implicitly dereferenced and/or borrowed. */
22092168
cached
2210-
predicate implicitDerefChainBorrow(Expr e, DerefChain derefChain, boolean borrow) {
2211-
M3::CachedStage::ref() and
2212-
exists(BorrowKind bk |
2213-
any(AssocFunctionResolution::AssocFunctionCall afc)
2214-
.argumentHasImplicitDerefChainBorrow(e, derefChain, bk) and
2215-
if bk.isNoBorrow() then borrow = false else borrow = true
2216-
)
2217-
or
2218-
e =
2219-
any(FieldExpr fe |
2220-
exists(resolveStructFieldExpr(fe, derefChain))
2221-
or
2222-
exists(resolveTupleFieldExpr(fe, derefChain))
2223-
).getContainer() and
2224-
not derefChain.isEmpty() and
2225-
borrow = false
2226-
}
2169+
private module Cached {
2170+
/** Holds if `n` is implicitly dereferenced and/or borrowed. */
2171+
cached
2172+
predicate implicitDerefChainBorrow(Expr e, DerefChain derefChain, boolean borrow) {
2173+
M3::CachedStage::ref() and
2174+
exists(BorrowKind bk |
2175+
any(AssocFunctionResolution::AssocFunctionCall afc)
2176+
.argumentHasImplicitDerefChainBorrow(e, derefChain, bk) and
2177+
if bk.isNoBorrow() then borrow = false else borrow = true
2178+
)
2179+
or
2180+
e =
2181+
any(FieldExpr fe |
2182+
exists(resolveStructFieldExpr(fe, derefChain))
2183+
or
2184+
exists(resolveTupleFieldExpr(fe, derefChain))
2185+
).getContainer() and
2186+
not derefChain.isEmpty() and
2187+
borrow = false
2188+
}
22272189

2228-
/**
2229-
* Gets an item (function or tuple struct/variant) that `call` resolves to, if
2230-
* any.
2231-
*
2232-
* The parameter `dispatch` is `true` if and only if the resolved target is a
2233-
* trait item because a precise target could not be determined from the
2234-
* types (for instance in the presence of generics or `dyn` types)
2235-
*/
2236-
cached
2237-
Addressable resolveCallTarget(InvocationExpr call, boolean dispatch) {
2238-
M3::CachedStage::ref() and
2239-
dispatch = false and
2240-
result = call.(NonAssocCallExpr).resolveCallTargetViaPathResolution()
2241-
or
2242-
exists(ImplOrTraitItemNode i |
2243-
i instanceof TraitItemNode and dispatch = true
2190+
/**
2191+
* Gets an item (function or tuple struct/variant) that `call` resolves to, if
2192+
* any.
2193+
*
2194+
* The parameter `dispatch` is `true` if and only if the resolved target is a
2195+
* trait item because a precise target could not be determined from the
2196+
* types (for instance in the presence of generics or `dyn` types)
2197+
*/
2198+
cached
2199+
Addressable resolveCallTarget(InvocationExpr call, boolean dispatch) {
2200+
M3::CachedStage::ref() and
2201+
dispatch = false and
2202+
result = call.(NonAssocCallExpr).resolveCallTargetViaPathResolution()
22442203
or
2245-
i instanceof ImplItemNode and dispatch = false
2246-
|
2247-
result = call.(AssocFunctionResolution::AssocFunctionCall).resolveCallTarget(i, _, _, _) and
2248-
not call instanceof CallExprImpl::DynamicCallExpr and
2249-
not i instanceof Builtins::BuiltinImpl
2250-
)
2204+
exists(ImplOrTraitItemNode i |
2205+
i instanceof TraitItemNode and dispatch = true
2206+
or
2207+
i instanceof ImplItemNode and dispatch = false
2208+
|
2209+
result = call.(AssocFunctionResolution::AssocFunctionCall).resolveCallTarget(i, _, _, _) and
2210+
not call instanceof CallExprImpl::DynamicCallExpr and
2211+
not i instanceof Builtins::BuiltinImpl
2212+
)
2213+
}
2214+
2215+
/**
2216+
* Gets the struct field that the field expression `fe` resolves to, if any.
2217+
*/
2218+
cached
2219+
StructField resolveStructFieldExpr(FieldExpr fe, DerefChain derefChain) {
2220+
M3::CachedStage::ref() and
2221+
exists(string name, DataType ty |
2222+
ty = getFieldExprLookupType(fe, pragma[only_bind_into](name), derefChain)
2223+
|
2224+
result = ty.(StructType).getTypeItem().getStructField(pragma[only_bind_into](name)) or
2225+
result = ty.(UnionType).getTypeItem().getStructField(pragma[only_bind_into](name))
2226+
)
2227+
}
2228+
2229+
/**
2230+
* Gets the tuple field that the field expression `fe` resolves to, if any.
2231+
*/
2232+
cached
2233+
TupleField resolveTupleFieldExpr(FieldExpr fe, DerefChain derefChain) {
2234+
M3::CachedStage::ref() and
2235+
exists(int i |
2236+
result =
2237+
getTupleFieldExprLookupType(fe, pragma[only_bind_into](i), derefChain)
2238+
.(StructType)
2239+
.getTypeItem()
2240+
.getTupleField(pragma[only_bind_into](i))
2241+
)
2242+
}
22512243
}
22522244

2245+
import Cached
2246+
22532247
private module Input3 implements InputSig3 {
22542248
private import rust as Rust
22552249
private import codeql.rust.elements.internal.OperationImpl::Impl as OperationImpl
@@ -3108,6 +3102,17 @@ private module Input3 implements InputSig3 {
31083102
)
31093103
}
31103104

3105+
pragma[nomagic]
3106+
private Type inferUnknownType(AstNode n, TypePath path) {
3107+
result = TUnknownType() and
3108+
path.isEmpty() and
3109+
(
3110+
n.(AssocFunctionResolution::AssocFunctionCall).resolutionDependsOnReturnType()
3111+
or
3112+
isPanicMacroCall(n)
3113+
)
3114+
}
3115+
31113116
pragma[nomagic]
31123117
Type inferTypeSpecific(AstNode n, TypePath path) {
31133118
result = inferAssignmentOperationType(n, path)

0 commit comments

Comments
 (0)