Skip to content

Commit 383e7b6

Browse files
committed
wip2
1 parent 5b1d0a4 commit 383e7b6

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ private signature Type resolveTypeMentionAtSig(AstNode tm, TypePath path);
2222
* how to resolve type mentions (`PreTypeMention` vs. `TypeMention`).
2323
*/
2424
private module MkSiblingImpls<resolveTypeMentionAtSig/2 resolveTypeMentionAt> {
25-
pragma[nomagic]
26-
private Type resolveNonTypeParameterTypeAt(AstNode tm, TypePath path) {
27-
result = resolveTypeMentionAt(tm, path) and
28-
not result instanceof TypeParameter
29-
}
30-
3125
private class Tm extends AstNode {
3226
Type getTypeAt(TypePath path) { result = resolveTypeMentionAt(this, path) }
3327
}
@@ -95,35 +89,23 @@ private module MkSiblingImpls<resolveTypeMentionAtSig/2 resolveTypeMentionAt> {
9589
ImplItemNode impl, ImplItemNode impl2, Trait trait, TypePath path
9690
) {
9791
// impl instanceof Builtins::BuiltinImpl and
98-
exists(Type t | implSiblings(trait, impl, impl2) |
99-
t = resolveTypeMentionAt(impl.getTraitPath(), path) and
100-
forall(Type t2 | t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) |
101-
t != t2 and
102-
(not t instanceof TypeParameter or not t2 instanceof TypeParameter)
103-
)
104-
or
105-
t = resolveTypeMentionAt(impl2.getTraitPath(), path) and
106-
// not (t = resolveTypeMentionAt(impl.getTraitPath(), path) and not t instanceof TypeParameter)
107-
forall(Type t2 | t2 = resolveTypeMentionAt(impl.getTraitPath(), path) |
108-
t != t2 and
109-
(not t instanceof TypeParameter or not t2 instanceof TypeParameter)
110-
)
111-
// and
112-
// t1 = resolveTypeMentionAt(impl.getTraitPath(), path) and
113-
// t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) and
114-
// t1 != t2
115-
// |
116-
// not t1 instanceof TypeParameter or
117-
// not t2 instanceof TypeParameter
92+
exists(Type t1, Type t2 |
93+
implSiblings(trait, impl, impl2) and
94+
t1 = resolveTypeMentionAt(impl.getTraitPath(), path) and
95+
t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) and
96+
t1 != t2
97+
|
98+
not t1 instanceof TypeParameter or
99+
not t2 instanceof TypeParameter
118100
)
119-
// or
120-
// // todo: handle blanket/non-blanket siblings in `implSiblings`
121-
// trait =
122-
// any(IndexTrait it |
123-
// implSiblingCandidate(impl, it, _, _) and
124-
// impl instanceof Builtins::BuiltinImpl and
125-
// path = TypePath::singleton(TAssociatedTypeTypeParameter(trait, it.getOutputType()))
126-
// )
101+
or
102+
// todo: handle blanket/non-blanket siblings in `implSiblings`
103+
trait =
104+
any(IndexTrait it |
105+
implSiblingCandidate(impl, it, _, _) and
106+
impl instanceof Builtins::BuiltinImpl and
107+
path = TypePath::singleton(TAssociatedTypeTypeParameter(trait, it.getOutputType()))
108+
)
127109
}
128110
}
129111

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11900,10 +11900,8 @@ inferType
1190011900
| main.rs:2311:26:2311:27 | .. | | {EXTERNAL LOCATION} | RangeFull |
1190111901
| main.rs:2312:9:2312:51 | for ... in ... { ... } | | {EXTERNAL LOCATION} | () |
1190211902
| main.rs:2312:18:2312:48 | &... | | {EXTERNAL LOCATION} | & |
11903-
| main.rs:2312:18:2312:48 | &... | TRef | {EXTERNAL LOCATION} | i64 |
1190411903
| main.rs:2312:19:2312:36 | [...] | | {EXTERNAL LOCATION} | [;] |
1190511904
| main.rs:2312:19:2312:36 | [...] | TArray | {EXTERNAL LOCATION} | i64 |
11906-
| main.rs:2312:19:2312:48 | ...[range_full] | | {EXTERNAL LOCATION} | i64 |
1190711905
| main.rs:2312:20:2312:23 | 1i64 | | {EXTERNAL LOCATION} | i64 |
1190811906
| main.rs:2312:26:2312:29 | 2i64 | | {EXTERNAL LOCATION} | i64 |
1190911907
| main.rs:2312:32:2312:35 | 3i64 | | {EXTERNAL LOCATION} | i64 |

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
634634
tp = abs.getATypeParameter()
635635
}
636636

637-
pragma[nomagic]
638-
private predicate hasTypeParameterAt2(
639-
App app, TypeAbstraction abs, Constraint constraint, TypePath path1, TypePath path2
640-
) {
641-
exists(TypeParameter tp |
642-
hasTypeParameterAt(app, abs, constraint, path1, tp) and
643-
hasTypeParameterAt(app, abs, constraint, path2, tp) and
644-
path1 != path2
645-
)
646-
}
647-
648637
private Type getNonPseudoTypeAt(App app, TypePath path) {
649638
result = app.getTypeAt(path) and not isPseudoType(result)
650639
}
@@ -1038,6 +1027,29 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10381027
)
10391028
}
10401029

1030+
private module TermIsInstantiationOfConditionInput2 implements
1031+
IsInstantiationOfInputSig<Term, TypeMention>
1032+
{
1033+
predicate potentialInstantiationOf(Term term, TypeAbstraction abs, TypeMention cond) {
1034+
TermIsInstantiationOfConditionInput::potentialInstantiationOf(term, abs, cond) and
1035+
exists(
1036+
TypeMention sub, Type constraintRoot, TypeMention constraintMention,
1037+
TypeAbstraction abs2
1038+
|
1039+
hasConstraintMention(term, abs2, sub, _, constraintRoot, constraintMention) and
1040+
conditionSatisfiesConstraintTypeAt(abs2, sub, constraintMention, _, _) and
1041+
typeAbstractionHasAmbiguousConstraintAt(abs2, constraintRoot, abs, _)
1042+
)
1043+
}
1044+
1045+
predicate relevantConstraint(TypeMention constraint) {
1046+
TermIsInstantiationOfConditionInput::relevantConstraint(constraint)
1047+
}
1048+
}
1049+
1050+
private module TermIsInstantiationOfCondition2 =
1051+
IsInstantiationOf<Term, TypeMention, TermIsInstantiationOfConditionInput2>;
1052+
10411053
pragma[nomagic]
10421054
private predicate satisfiesConstraintTypeMention0(
10431055
Term term, Constraint constraint, TypeMention constraintMention, TypeAbstraction abs,
@@ -1050,15 +1062,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10501062
exists(TypePath prefix, TypeAbstraction other |
10511063
typeAbstractionHasAmbiguousConstraintAt(abs, constraintRoot, other, prefix) and
10521064
prefix.isPrefixOf(path) and
1053-
hasConstraintMention(term, other, _, _, constraintRoot, _)
1065+
TermIsInstantiationOfCondition2::isInstantiationOf(term, other, _)
1066+
// hasConstraintMention(term, other, _, _, constraintRoot, _)
10541067
) and
10551068
ambiguous = true
10561069
or
10571070
forall(TypePath prefix, TypeAbstraction other |
10581071
typeAbstractionHasAmbiguousConstraintAt(abs, constraintRoot, other, prefix) and
10591072
prefix.isPrefixOf(path)
10601073
|
1061-
TermIsInstantiationOfCondition::isNotInstantiationOf(term, other, _, _)
1074+
TermIsInstantiationOfCondition2::isNotInstantiationOf(term, other, _, _)
10621075
) and
10631076
ambiguous = false
10641077
)

0 commit comments

Comments
 (0)