Skip to content

Commit 5b1d0a4

Browse files
committed
wip
1 parent f721375 commit 5b1d0a4

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,17 @@ private module MkSiblingImpls<resolveTypeMentionAtSig/2 resolveTypeMentionAt> {
9797
// impl instanceof Builtins::BuiltinImpl and
9898
exists(Type t | implSiblings(trait, impl, impl2) |
9999
t = resolveTypeMentionAt(impl.getTraitPath(), path) and
100-
not (t = resolveTypeMentionAt(impl2.getTraitPath(), path) and not t instanceof TypeParameter)
100+
forall(Type t2 | t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) |
101+
t != t2 and
102+
(not t instanceof TypeParameter or not t2 instanceof TypeParameter)
103+
)
101104
or
102105
t = resolveTypeMentionAt(impl2.getTraitPath(), path) and
103-
not (t = resolveTypeMentionAt(impl.getTraitPath(), path) and not t instanceof TypeParameter)
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+
)
104111
// and
105112
// t1 = resolveTypeMentionAt(impl.getTraitPath(), path) and
106113
// t2 = resolveTypeMentionAt(impl2.getTraitPath(), path) and

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11605,6 +11605,7 @@ inferType
1160511605
| main.rs:2223:9:2223:31 | ... .my_add(...) | T | main.rs:2107:5:2107:19 | S |
1160611606
| main.rs:2223:9:2223:31 | ... .my_add(...) | T.T | main.rs:2109:10:2109:17 | T::Output[MyAdd] |
1160711607
| main.rs:2223:9:2223:31 | ... .my_add(...) | T.T | main.rs:2118:10:2118:17 | T::Output[MyAdd] |
11608+
| main.rs:2223:9:2223:31 | ... .my_add(...) | T.T | main.rs:2127:14:2127:14 | T::Output[MyAdd] |
1160811609
| main.rs:2223:11:2223:14 | 1i64 | | {EXTERNAL LOCATION} | i64 |
1160911610
| main.rs:2223:24:2223:30 | S(...) | | main.rs:2107:5:2107:19 | S |
1161011611
| main.rs:2223:24:2223:30 | S(...) | T | {EXTERNAL LOCATION} | i64 |
@@ -11899,8 +11900,10 @@ inferType
1189911900
| main.rs:2311:26:2311:27 | .. | | {EXTERNAL LOCATION} | RangeFull |
1190011901
| main.rs:2312:9:2312:51 | for ... in ... { ... } | | {EXTERNAL LOCATION} | () |
1190111902
| main.rs:2312:18:2312:48 | &... | | {EXTERNAL LOCATION} | & |
11903+
| main.rs:2312:18:2312:48 | &... | TRef | {EXTERNAL LOCATION} | i64 |
1190211904
| main.rs:2312:19:2312:36 | [...] | | {EXTERNAL LOCATION} | [;] |
1190311905
| main.rs:2312:19:2312:36 | [...] | TArray | {EXTERNAL LOCATION} | i64 |
11906+
| main.rs:2312:19:2312:48 | ...[range_full] | | {EXTERNAL LOCATION} | i64 |
1190411907
| main.rs:2312:20:2312:23 | 1i64 | | {EXTERNAL LOCATION} | i64 |
1190511908
| main.rs:2312:26:2312:29 | 2i64 | | {EXTERNAL LOCATION} | i64 |
1190611909
| main.rs:2312:32:2312:35 | 3i64 | | {EXTERNAL LOCATION} | i64 |

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,37 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
626626
)
627627
}
628628

629+
pragma[nomagic]
630+
private predicate hasTypeParameterAt(
631+
App app, TypeAbstraction abs, Constraint constraint, TypePath path, TypeParameter tp
632+
) {
633+
tp = getTypeAt(app, abs, constraint, path) and
634+
tp = abs.getATypeParameter()
635+
}
636+
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+
648+
private Type getNonPseudoTypeAt(App app, TypePath path) {
649+
result = app.getTypeAt(path) and not isPseudoType(result)
650+
}
651+
652+
pragma[nomagic]
653+
private Type getNonPseudoTypeAt2(
654+
App app, TypeAbstraction abs, Constraint constraint, TypeParameter tp, TypePath path
655+
) {
656+
hasTypeParameterAt(app, abs, constraint, path, tp) and
657+
result = getNonPseudoTypeAt(app, path)
658+
}
659+
629660
/**
630661
* Holds if `app` is _not_ a possible instantiation of `constraint`, because `app`
631662
* and `constraint` differ on concrete types at `path`.
@@ -649,22 +680,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
649680
exists(Type t, Type t2 |
650681
t = getTypeAt(app, abs, constraint, path) and
651682
not t = abs.getATypeParameter() and
652-
app.getTypeAt(path) = t2 and
653-
not isPseudoType(t2) and
683+
t2 = getNonPseudoTypeAt(app, path) and
654684
t2 != t
655685
)
656686
or
657687
// satisfiesConcreteTypes(app, abs, constraint) and
658688
exists(TypeParameter tp, TypePath path2, Type t, Type t2 |
659-
tp = getTypeAt(app, abs, constraint, path) and
660-
tp = getTypeAt(app, abs, constraint, path2) and
661-
tp = abs.getATypeParameter() and
662-
path != path2 and
663-
app.getTypeAt(path) = t and
664-
app.getTypeAt(path2) = t2 and
665-
not isPseudoType(t) and
666-
not isPseudoType(t2) and
667-
t != t2
689+
t = getNonPseudoTypeAt2(app, abs, constraint, tp, path) and
690+
t2 = getNonPseudoTypeAt2(app, abs, constraint, tp, path2) and
691+
t != t2 and
692+
path != path2
668693
)
669694
}
670695
}
@@ -1030,10 +1055,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10301055
ambiguous = true
10311056
or
10321057
forall(TypePath prefix, TypeAbstraction other |
1033-
typeAbstractionHasAmbiguousConstraintAt(abs, constraintRoot, other, prefix)
1058+
typeAbstractionHasAmbiguousConstraintAt(abs, constraintRoot, other, prefix) and
1059+
prefix.isPrefixOf(path)
10341060
|
1035-
not prefix.isPrefixOf(path)
1036-
or
10371061
TermIsInstantiationOfCondition::isNotInstantiationOf(term, other, _, _)
10381062
) and
10391063
ambiguous = false

0 commit comments

Comments
 (0)