Skip to content

Commit 5520643

Browse files
committed
Address review comments
1 parent 86a4d42 commit 5520643

File tree

3 files changed

+141
-125
lines changed

3 files changed

+141
-125
lines changed

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

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -240,70 +240,66 @@ private module ImplOrTraitItemNodeOption = Option<ImplOrTraitItemNode>;
240240
private class ImplOrTraitItemNodeOption = ImplOrTraitItemNodeOption::Option;
241241

242242
private class FunctionDeclaration extends Function {
243+
private ImplOrTraitItemNodeOption i_;
244+
245+
FunctionDeclaration() {
246+
not this = any(ImplOrTraitItemNode i).getAnAssocItem() and i_.isNone()
247+
or
248+
this = i_.asSome().getASuccessor(_)
249+
}
250+
243251
/** Holds if this function is associated with `i`. */
244-
predicate isAssoc(ImplOrTraitItemNode i) { this = i.getASuccessor(_) }
252+
predicate isAssoc(ImplOrTraitItemNode i) { i = i_.asSome() }
245253

246254
/** Holds if this is a free function. */
247-
predicate isFree() { not this = any(ImplOrTraitItemNode i).getAnAssocItem() }
255+
predicate isFree() { i_.isNone() }
248256

249257
/** Holds if this function is valid for `i`. */
250-
predicate isDeclaration(ImplOrTraitItemNodeOption i) {
251-
i.isNone() and
252-
this.isFree()
253-
or
254-
this.isAssoc(i.asSome())
255-
}
258+
predicate isFor(ImplOrTraitItemNodeOption i) { i = i_ }
256259

257260
/**
258261
* Holds if this function is valid for `i`. If `i` is a trait or `impl` block then
259262
* this function must be declared directly inside `i`.
260263
*/
261-
predicate isDeclarationStrict(ImplOrTraitItemNodeOption i) {
264+
predicate isDirectlyFor(ImplOrTraitItemNodeOption i) {
262265
i.isNone() and
263266
this.isFree()
264267
or
265268
this = i.asSome().getAnAssocItem()
266269
}
267270

268271
TypeParameter getTypeParameter(ImplOrTraitItemNodeOption i, TypeParameterPosition ppos) {
269-
this.isDeclaration(i) and
272+
i = i_ and
270273
(
271274
typeParamMatchPosition(this.getGenericParamList().getATypeParam(), result, ppos)
272275
or
273276
typeParamMatchPosition(i.asSome().getTypeParam(_), result, ppos)
274277
or
275278
ppos.isImplicit() and result = TSelfTypeParameter(i.asSome())
276279
or
277-
ppos.isImplicit() and
278-
result.(AssociatedTypeTypeParameter).getTrait() = i.asSome()
280+
ppos.isImplicit() and result.(AssociatedTypeTypeParameter).getTrait() = i.asSome()
279281
or
280-
ppos.isImplicit() and
281-
this = result.(ImplTraitTypeTypeParameter).getFunction()
282+
ppos.isImplicit() and this = result.(ImplTraitTypeTypeParameter).getFunction()
282283
)
283284
}
284285

285286
pragma[nomagic]
286287
Type getParameterType(ImplOrTraitItemNodeOption i, FunctionPosition pos, TypePath path) {
287-
this.isDeclaration(i) and
288+
i = i_ and
288289
(
289290
not pos.isReturn() and
290291
result = getAssocFunctionTypeAt(this, i.asSome(), pos, path)
291292
or
292293
i.isNone() and
293-
exists(Param p |
294-
p = this.getParam(pos.asPosition()) and
295-
result = p.getTypeRepr().(TypeMention).resolveTypeAt(path)
296-
)
294+
result = this.getParam(pos.asPosition()).getTypeRepr().(TypeMention).resolveTypeAt(path)
297295
)
298296
}
299297

300298
private Type resolveRetType(ImplOrTraitItemNodeOption i, TypePath path) {
301-
this.isDeclaration(i) and
299+
i = i_ and
302300
(
303-
exists(FunctionPosition pos |
304-
result = getAssocFunctionTypeAt(this, i.asSome(), pos, path) and
305-
pos.isReturn()
306-
)
301+
result =
302+
getAssocFunctionTypeAt(this, i.asSome(), any(FunctionPosition pos | pos.isReturn()), path)
307303
or
308304
i.isNone() and
309305
result = getReturnTypeMention(this).resolveTypeAt(path)
@@ -313,7 +309,7 @@ private class FunctionDeclaration extends Function {
313309
Type getReturnType(ImplOrTraitItemNodeOption i, TypePath path) {
314310
if this.isAsync()
315311
then
316-
this.isDeclaration(i) and
312+
i = i_ and
317313
path.isEmpty() and
318314
result = getFutureTraitType()
319315
or
@@ -332,6 +328,7 @@ private class FunctionDeclaration extends Function {
332328
}
333329

334330
string toStringExt(ImplOrTraitItemNode i) {
331+
i = i_.asSome() and
335332
if this = i.getAnAssocItem()
336333
then result = this.toString()
337334
else
@@ -417,7 +414,7 @@ module CertainTypeInference {
417414
exists(ImplOrTraitItemNodeOption i |
418415
callResolvesTo(ce, p, f) and
419416
result = f.getReturnType(i, path) and
420-
f.isDeclarationStrict(i)
417+
f.isDirectlyFor(i)
421418
)
422419
}
423420

@@ -2191,19 +2188,21 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
21912188
private class MethodDeclaration extends Method, FunctionDeclaration { }
21922189

21932190
private newtype TDeclaration =
2194-
MkDeclaration(ImplOrTraitItemNode i, MethodDeclaration m) { m.isAssoc(i) }
2191+
TMethodFunctionDeclaration(ImplOrTraitItemNode i, MethodDeclaration m) { m.isAssoc(i) }
21952192

2196-
final class Declaration extends MkDeclaration {
2193+
final class Declaration extends TMethodFunctionDeclaration {
21972194
ImplOrTraitItemNode i_;
21982195
ImplOrTraitItemNodeOption somei;
21992196
MethodDeclaration m;
22002197

22012198
Declaration() {
2202-
this = MkDeclaration(i_, m) and
2199+
this = TMethodFunctionDeclaration(i_, m) and
22032200
somei.asSome() = i_
22042201
}
22052202

2206-
predicate isMethod(ImplOrTraitItemNode i, Method method) { this = MkDeclaration(i, method) }
2203+
predicate isMethod(ImplOrTraitItemNode i, Method method) {
2204+
this = TMethodFunctionDeclaration(i, method)
2205+
}
22072206

22082207
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
22092208
result = m.getTypeParameter(somei, ppos)
@@ -2294,7 +2293,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
22942293
Declaration getTarget(string derefChainBorrow) {
22952294
exists(ImplOrTraitItemNode i, Method m |
22962295
m = this.getTarget(i, derefChainBorrow) and
2297-
result = MkDeclaration(i, m)
2296+
result = TMethodFunctionDeclaration(i, m)
22982297
)
22992298
}
23002299

@@ -2668,7 +2667,7 @@ private module NonMethodResolution {
26682667
ArgsAreInstantiationsOf<NonMethodArgsAreInstantiationsOfInput>;
26692668
}
26702669

2671-
abstract private class TupleConstructor extends Addressable {
2670+
abstract private class TupleLikeConstructor extends Addressable {
26722671
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
26732672

26742673
abstract Type getParameterType(FunctionPosition pos, TypePath path);
@@ -2686,7 +2685,7 @@ abstract private class TupleConstructor extends Addressable {
26862685
}
26872686
}
26882687

2689-
private class TupleStruct extends TupleConstructor, Struct {
2688+
private class TupleStruct extends TupleLikeConstructor, Struct {
26902689
TupleStruct() { this.isTuple() }
26912690

26922691
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
@@ -2709,7 +2708,7 @@ private class TupleStruct extends TupleConstructor, Struct {
27092708
}
27102709
}
27112710

2712-
private class TupleVariant extends TupleConstructor, Variant {
2711+
private class TupleVariant extends TupleLikeConstructor, Variant {
27132712
TupleVariant() { this.isTuple() }
27142713

27152714
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
@@ -2748,9 +2747,9 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
27482747

27492748
private newtype TDeclaration =
27502749
TNonMethodFunctionDeclaration(ImplOrTraitItemNodeOption i, NonMethodFunctionDeclaration f) {
2751-
f.isDeclaration(i)
2750+
f.isFor(i)
27522751
} or
2753-
TTupleConstructorDeclaration(TupleConstructor tc)
2752+
TTupleLikeConstructorDeclaration(TupleLikeConstructor tc)
27542753

27552754
abstract class Declaration extends TDeclaration {
27562755
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
@@ -2816,10 +2815,12 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
28162815
override Location getLocation() { result = f.getLocation() }
28172816
}
28182817

2819-
private class TupleConstructorDeclaration extends Declaration, TTupleConstructorDeclaration {
2820-
TupleConstructor tc;
2818+
private class TupleLikeConstructorDeclaration extends Declaration,
2819+
TTupleLikeConstructorDeclaration
2820+
{
2821+
TupleLikeConstructor tc;
28212822

2822-
TupleConstructorDeclaration() { this = TTupleConstructorDeclaration(tc) }
2823+
TupleLikeConstructorDeclaration() { this = TTupleLikeConstructorDeclaration(tc) }
28232824

28242825
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
28252826
result = tc.getTypeParameter(ppos)
@@ -2860,11 +2861,11 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
28602861
f = this.resolveTraitFunctionViaPathResolution(i.asSome())
28612862
or
28622863
f = this.resolveCallTargetViaPathResolution() and
2863-
f.isDeclarationStrict(i)
2864+
f.isDirectlyFor(i)
28642865
)
28652866
or
28662867
exists(ItemNode i | i = this.resolveCallTargetViaPathResolution() |
2867-
result = TTupleConstructorDeclaration(i)
2868+
result = TTupleLikeConstructorDeclaration(i)
28682869
)
28692870
}
28702871

@@ -2880,7 +2881,7 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
28802881
)
28812882
or
28822883
// Tuple declarations, such as `Result::Ok(...)`, may also be context typed
2883-
exists(TupleConstructor tc, TypeParameter tp |
2884+
exists(TupleLikeConstructor tc, TypeParameter tp |
28842885
tc = this.resolveCallTargetViaPathResolution() and
28852886
pos.isReturn() and
28862887
tp = tc.getReturnType(path) and
@@ -3435,7 +3436,7 @@ private Type inferStructPatType(AstNode n, TypePath path) {
34353436
private module TupleStructPatMatchingInput implements MatchingInputSig {
34363437
import FunctionPositionMatchingInput
34373438

3438-
class Declaration = TupleConstructor;
3439+
class Declaration = TupleLikeConstructor;
34393440

34403441
class Access extends TupleStructPat {
34413442
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,6 +3077,10 @@ mod literal_overlap {
30773077
let x: usize = 0;
30783078
let y = &1;
30793079
let z = x.g(y); // $ target=MyTrait::g
3080+
3081+
let x = 0; // $ SPURIOUS: type=x:i32 $ MISSING: type=x:usize
3082+
let y: usize = 1;
3083+
let z = x.max(y); // $ target=max
30803084
}
30813085
}
30823086

0 commit comments

Comments
 (0)