Skip to content

Commit 7a5a89a

Browse files
committed
Rust: Only infer types from trait bounds when their implementation is unambiguous
1 parent 16613d1 commit 7a5a89a

File tree

9 files changed

+388
-227
lines changed

9 files changed

+388
-227
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module SatisfiesBlanketConstraint<
103103
}
104104

105105
private module SatisfiesBlanketConstraintInput implements
106-
SatisfiesConstraintInputSig<ArgumentTypeAndBlanketOffset>
106+
SatisfiesTypeInputSig<ArgumentTypeAndBlanketOffset>
107107
{
108108
pragma[nomagic]
109109
additional predicate relevantConstraint(
@@ -123,7 +123,7 @@ module SatisfiesBlanketConstraint<
123123
}
124124

125125
private module SatisfiesBlanketConstraint =
126-
SatisfiesConstraint<ArgumentTypeAndBlanketOffset, SatisfiesBlanketConstraintInput>;
126+
SatisfiesType<ArgumentTypeAndBlanketOffset, SatisfiesBlanketConstraintInput>;
127127

128128
/**
129129
* Holds if the argument type `at` satisfies the first non-trivial blanket

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

Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,105 @@ private import TypeMention
1313
private import TypeInference
1414
private import FunctionType
1515

16-
pragma[nomagic]
17-
private Type resolveNonTypeParameterTypeAt(TypeMention tm, TypePath path) {
18-
result = tm.getTypeAt(path) and
19-
not result instanceof TypeParameter
20-
}
16+
private signature Type resolveTypeMentionAtSig(AstNode tm, TypePath path);
2117

22-
bindingset[t1, t2]
23-
private predicate typeMentionEqual(TypeMention t1, TypeMention t2) {
24-
forex(TypePath path, Type type | resolveNonTypeParameterTypeAt(t1, path) = type |
25-
resolveNonTypeParameterTypeAt(t2, path) = type
26-
)
27-
}
18+
/**
19+
* Provides logic for identifying sibling implementations, parameterized over
20+
* how to resolve type mentions (`PreTypeMention` vs. `TypeMention`).
21+
*/
22+
private module MkSiblingImpls<resolveTypeMentionAtSig/2 resolveTypeMentionAt> {
23+
pragma[nomagic]
24+
private Type resolveNonTypeParameterTypeAt(AstNode tm, TypePath path) {
25+
result = resolveTypeMentionAt(tm, path) and
26+
not result instanceof TypeParameter
27+
}
2828

29-
pragma[nomagic]
30-
private predicate implSiblingCandidate(
31-
Impl impl, TraitItemNode trait, Type rootType, TypeMention selfTy
32-
) {
33-
trait = impl.(ImplItemNode).resolveTraitTy() and
34-
selfTy = impl.getSelfTy() and
35-
rootType = selfTy.getType()
29+
bindingset[t1, t2]
30+
private predicate typeMentionEqual(AstNode t1, AstNode t2) {
31+
forex(TypePath path, Type type | resolveNonTypeParameterTypeAt(t1, path) = type |
32+
resolveNonTypeParameterTypeAt(t2, path) = type
33+
)
34+
}
35+
36+
pragma[nomagic]
37+
private predicate implSiblingCandidate(
38+
Impl impl, TraitItemNode trait, Type rootType, AstNode selfTy
39+
) {
40+
trait = impl.(ImplItemNode).resolveTraitTy() and
41+
selfTy = impl.getSelfTy() and
42+
rootType = resolveTypeMentionAt(selfTy, TypePath::nil())
43+
}
44+
45+
pragma[nomagic]
46+
private predicate blanketImplSiblingCandidate(ImplItemNode impl, Trait trait) {
47+
impl.isBlanketImplementation() and
48+
trait = impl.resolveTraitTy()
49+
}
50+
51+
/**
52+
* Holds if `impl1` and `impl2` are a sibling implementations of `trait`. We
53+
* consider implementations to be siblings if they implement the same trait for
54+
* the same type. In that case `Self` is the same type in both implementations,
55+
* and method calls to the implementations cannot be resolved unambiguously
56+
* based only on the receiver type.
57+
*/
58+
pragma[inline]
59+
predicate implSiblings(TraitItemNode trait, Impl impl1, Impl impl2) {
60+
impl1 != impl2 and
61+
(
62+
exists(Type rootType, AstNode selfTy1, AstNode selfTy2 |
63+
implSiblingCandidate(impl1, trait, rootType, selfTy1) and
64+
implSiblingCandidate(impl2, trait, rootType, selfTy2) and
65+
// In principle the second conjunct below should be superflous, but we still
66+
// have ill-formed type mentions for types that we don't understand. For
67+
// those checking both directions restricts further. Note also that we check
68+
// syntactic equality, whereas equality up to renaming would be more
69+
// correct.
70+
typeMentionEqual(selfTy1, selfTy2) and
71+
typeMentionEqual(selfTy2, selfTy1)
72+
)
73+
or
74+
blanketImplSiblingCandidate(impl1, trait) and
75+
blanketImplSiblingCandidate(impl2, trait)
76+
)
77+
}
78+
79+
/**
80+
* Holds if `impl` is an implementation of `trait` and if another implementation
81+
* exists for the same type.
82+
*/
83+
pragma[nomagic]
84+
predicate implHasSibling(ImplItemNode impl, Trait trait) { implSiblings(trait, impl, _) }
85+
86+
pragma[nomagic]
87+
predicate implHasAmbigousSiblingAt(ImplItemNode impl, Trait trait, TypePath path) {
88+
exists(ImplItemNode impl2, Type t1, Type t2 |
89+
implSiblings(trait, impl, impl2) and
90+
t1 = resolveTypeMentionAt(impl.getTraitPath(), path) and
91+
t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) and
92+
t1 != t2
93+
|
94+
not t1 instanceof TypeParameter or
95+
not t2 instanceof TypeParameter
96+
)
97+
}
3698
}
3799

38-
pragma[nomagic]
39-
private predicate blanketImplSiblingCandidate(ImplItemNode impl, Trait trait) {
40-
impl.isBlanketImplementation() and
41-
trait = impl.resolveTraitTy()
100+
private Type resolvePreTypeMention(AstNode tm, TypePath path) {
101+
result = tm.(PreTypeMention).getTypeAt(path)
42102
}
43103

44-
/**
45-
* Holds if `impl1` and `impl2` are a sibling implementations of `trait`. We
46-
* consider implementations to be siblings if they implement the same trait for
47-
* the same type. In that case `Self` is the same type in both implementations,
48-
* and method calls to the implementations cannot be resolved unambiguously
49-
* based only on the receiver type.
50-
*/
51-
pragma[inline]
52-
private predicate implSiblings(TraitItemNode trait, Impl impl1, Impl impl2) {
53-
impl1 != impl2 and
54-
(
55-
exists(Type rootType, TypeMention selfTy1, TypeMention selfTy2 |
56-
implSiblingCandidate(impl1, trait, rootType, selfTy1) and
57-
implSiblingCandidate(impl2, trait, rootType, selfTy2) and
58-
// In principle the second conjunct below should be superflous, but we still
59-
// have ill-formed type mentions for types that we don't understand. For
60-
// those checking both directions restricts further. Note also that we check
61-
// syntactic equality, whereas equality up to renaming would be more
62-
// correct.
63-
typeMentionEqual(selfTy1, selfTy2) and
64-
typeMentionEqual(selfTy2, selfTy1)
65-
)
66-
or
67-
blanketImplSiblingCandidate(impl1, trait) and
68-
blanketImplSiblingCandidate(impl2, trait)
69-
)
104+
private module PreSiblingImpls = MkSiblingImpls<resolvePreTypeMention/2>;
105+
106+
predicate preImplHasAmbigousSiblingAt = PreSiblingImpls::implHasAmbigousSiblingAt/3;
107+
108+
private Type resolveTypeMention(AstNode tm, TypePath path) {
109+
result = tm.(TypeMention).getTypeAt(path)
70110
}
71111

72-
/**
73-
* Holds if `impl` is an implementation of `trait` and if another implementation
74-
* exists for the same type.
75-
*/
76-
pragma[nomagic]
77-
private predicate implHasSibling(ImplItemNode impl, Trait trait) { implSiblings(trait, impl, _) }
112+
private module SiblingImpls = MkSiblingImpls<resolveTypeMention/2>;
113+
114+
import SiblingImpls
78115

79116
/**
80117
* Holds if `f` is a function declared inside `trait`, and the type of `f` at

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,11 @@ class TypeParamTypeParameter extends TypeParameter, TTypeParamTypeParameter {
439439
*/
440440
class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypeParameter {
441441
private Trait trait;
442-
private TypeAlias typeAlias;
442+
private AssocType typeAlias;
443443

444444
AssociatedTypeTypeParameter() { this = TAssociatedTypeTypeParameter(trait, typeAlias) }
445445

446-
TypeAlias getTypeAlias() { result = typeAlias }
446+
AssocType getTypeAlias() { result = typeAlias }
447447

448448
/** Gets the trait that contains this associated type declaration. */
449449
TraitItemNode getTrait() { result = trait }
@@ -457,7 +457,13 @@ class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypePara
457457
override ItemNode getDeclaringItem() { result = trait }
458458

459459
override string toString() {
460-
result = typeAlias.getName().getText() + "[" + trait.getName().toString() + "]"
460+
exists(string fromString, TraitItemNode trait2 |
461+
result = typeAlias.getName().getText() + "[" + trait.getName() + fromString + "]" and
462+
trait2 = typeAlias.getTrait() and
463+
if trait = trait2
464+
then fromString = ""
465+
else fromString = " (inherited from " + trait2.getName() + ")"
466+
)
461467
}
462468

463469
override Location getLocation() { result = typeAlias.getLocation() }

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

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ private module Input implements InputSig1<Location>, InputSig2<PreTypeMention> {
4141

4242
class TypeAbstraction = TA::TypeAbstraction;
4343

44+
predicate typeAbstractionHasAmbigousConstraintAt(
45+
TypeAbstraction abs, Type constraint, TypePath path
46+
) {
47+
FunctionOverloading::implHasAmbigousSiblingAt(abs, constraint.(TraitType).getTrait(), path)
48+
}
49+
4450
class TypeArgumentPosition extends TTypeArgumentPosition {
4551
int asMethodTypeArgumentPosition() { this = TMethodTypeArgumentPosition(result) }
4652

@@ -127,17 +133,15 @@ private module Input implements InputSig1<Location>, InputSig2<PreTypeMention> {
127133

128134
PreTypeMention getABaseTypeMention(Type t) { none() }
129135

130-
Type getATypeParameterConstraint(TypeParameter tp, TypePath path) {
131-
exists(TypeMention tm | result = tm.getTypeAt(path) |
132-
tm = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr() or
133-
tm = tp.(SelfTypeParameter).getTrait() or
134-
tm =
135-
tp.(ImplTraitTypeTypeParameter)
136-
.getImplTraitTypeRepr()
137-
.getTypeBoundList()
138-
.getABound()
139-
.getTypeRepr()
140-
)
136+
PreTypeMention getATypeParameterConstraint(TypeParameter tp) {
137+
result = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr() or
138+
result = tp.(SelfTypeParameter).getTrait() or
139+
result =
140+
tp.(ImplTraitTypeTypeParameter)
141+
.getImplTraitTypeRepr()
142+
.getTypeBoundList()
143+
.getABound()
144+
.getTypeRepr()
141145
}
142146

143147
/**
@@ -1170,7 +1174,7 @@ private module ContextTyping {
11701174
or
11711175
exists(TypeParameter mid |
11721176
assocFunctionMentionsTypeParameterAtNonRetPos(i, f, mid) and
1173-
tp = getATypeParameterConstraint(mid, _)
1177+
tp = getATypeParameterConstraint(mid).getTypeAt(_)
11741178
)
11751179
}
11761180

@@ -2544,8 +2548,7 @@ private module AssocFunctionResolution {
25442548
Location getLocation() { result = afc.getLocation() }
25452549
}
25462550

2547-
private module CallSatisfiesDerefConstraintInput implements
2548-
SatisfiesConstraintInputSig<CallDerefCand>
2551+
private module CallSatisfiesDerefConstraintInput implements SatisfiesTypeInputSig<CallDerefCand>
25492552
{
25502553
pragma[nomagic]
25512554
predicate relevantConstraint(CallDerefCand mc, Type constraint) {
@@ -2555,7 +2558,7 @@ private module AssocFunctionResolution {
25552558
}
25562559

25572560
private module CallSatisfiesDerefConstraint =
2558-
SatisfiesConstraint<CallDerefCand, CallSatisfiesDerefConstraintInput>;
2561+
SatisfiesType<CallDerefCand, CallSatisfiesDerefConstraintInput>;
25592562

25602563
pragma[nomagic]
25612564
private AssociatedTypeTypeParameter getDerefTargetTypeParameter() {
@@ -3586,21 +3589,20 @@ final private class AwaitTarget extends Expr {
35863589
Type getTypeAt(TypePath path) { result = inferType(this, path) }
35873590
}
35883591

3589-
private module AwaitSatisfiesConstraintInput implements SatisfiesConstraintInputSig<AwaitTarget> {
3592+
private module AwaitSatisfiesTypeInput implements SatisfiesTypeInputSig<AwaitTarget> {
35903593
pragma[nomagic]
35913594
predicate relevantConstraint(AwaitTarget term, Type constraint) {
35923595
exists(term) and
35933596
constraint.(TraitType).getTrait() instanceof FutureTrait
35943597
}
35953598
}
35963599

3597-
private module AwaitSatisfiesConstraint =
3598-
SatisfiesConstraint<AwaitTarget, AwaitSatisfiesConstraintInput>;
3600+
private module AwaitSatisfiesType = SatisfiesType<AwaitTarget, AwaitSatisfiesTypeInput>;
35993601

36003602
pragma[nomagic]
36013603
private Type inferAwaitExprType(AstNode n, TypePath path) {
36023604
exists(TypePath exprPath |
3603-
AwaitSatisfiesConstraint::satisfiesConstraintType(n.(AwaitExpr).getExpr(), _, exprPath, result) and
3605+
AwaitSatisfiesType::satisfiesConstraintType(n.(AwaitExpr).getExpr(), _, exprPath, result) and
36043606
exprPath.isCons(getFutureOutputTypeParameter(), path)
36053607
)
36063608
}
@@ -3779,9 +3781,7 @@ final private class ForIterableExpr extends Expr {
37793781
Type getTypeAt(TypePath path) { result = inferType(this, path) }
37803782
}
37813783

3782-
private module ForIterableSatisfiesConstraintInput implements
3783-
SatisfiesConstraintInputSig<ForIterableExpr>
3784-
{
3784+
private module ForIterableSatisfiesTypeInput implements SatisfiesTypeInputSig<ForIterableExpr> {
37853785
predicate relevantConstraint(ForIterableExpr term, Type constraint) {
37863786
exists(term) and
37873787
exists(Trait t | t = constraint.(TraitType).getTrait() |
@@ -3802,15 +3802,15 @@ private AssociatedTypeTypeParameter getIntoIteratorItemTypeParameter() {
38023802
result = getAssociatedTypeTypeParameter(any(IntoIteratorTrait t).getItemType())
38033803
}
38043804

3805-
private module ForIterableSatisfiesConstraint =
3806-
SatisfiesConstraint<ForIterableExpr, ForIterableSatisfiesConstraintInput>;
3805+
private module ForIterableSatisfiesType =
3806+
SatisfiesType<ForIterableExpr, ForIterableSatisfiesTypeInput>;
38073807

38083808
pragma[nomagic]
38093809
private Type inferForLoopExprType(AstNode n, TypePath path) {
38103810
// type of iterable -> type of pattern (loop variable)
38113811
exists(ForExpr fe, TypePath exprPath, AssociatedTypeTypeParameter tp |
38123812
n = fe.getPat() and
3813-
ForIterableSatisfiesConstraint::satisfiesConstraintType(fe.getIterable(), _, exprPath, result) and
3813+
ForIterableSatisfiesType::satisfiesConstraintType(fe.getIterable(), _, exprPath, result) and
38143814
exprPath.isCons(tp, path)
38153815
|
38163816
tp = getIntoIteratorItemTypeParameter()
@@ -3836,21 +3836,20 @@ final private class InvokedClosureExpr extends Expr {
38363836
CallExpr getCall() { result = call }
38373837
}
38383838

3839-
private module InvokedClosureSatisfiesConstraintInput implements
3840-
SatisfiesConstraintInputSig<InvokedClosureExpr>
3839+
private module InvokedClosureSatisfiesTypeInput implements SatisfiesTypeInputSig<InvokedClosureExpr>
38413840
{
38423841
predicate relevantConstraint(InvokedClosureExpr term, Type constraint) {
38433842
exists(term) and
38443843
constraint.(TraitType).getTrait() instanceof FnOnceTrait
38453844
}
38463845
}
38473846

3848-
private module InvokedClosureSatisfiesConstraint =
3849-
SatisfiesConstraint<InvokedClosureExpr, InvokedClosureSatisfiesConstraintInput>;
3847+
private module InvokedClosureSatisfiesType =
3848+
SatisfiesType<InvokedClosureExpr, InvokedClosureSatisfiesTypeInput>;
38503849

38513850
/** Gets the type of `ce` when viewed as an implementation of `FnOnce`. */
38523851
private Type invokedClosureFnTypeAt(InvokedClosureExpr ce, TypePath path) {
3853-
InvokedClosureSatisfiesConstraint::satisfiesConstraintType(ce, _, path, result)
3852+
InvokedClosureSatisfiesType::satisfiesConstraintType(ce, _, path, result)
38543853
}
38553854

38563855
/**

0 commit comments

Comments
 (0)