Skip to content

Commit 8ce2618

Browse files
authored
Merge pull request #21151 from hvitved/rust/disable-universal-conds-by-default
Type inference: Disable universal conditions by default
2 parents 73eb3e2 + 65ca884 commit 8ce2618

File tree

5 files changed

+20
-40
lines changed

5 files changed

+20
-40
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ module SatisfiesBlanketConstraint<
117117
predicate relevantConstraint(ArgumentTypeAndBlanketOffset ato, Type constraint) {
118118
relevantConstraint(ato, _, constraint.(TraitType).getTrait())
119119
}
120-
121-
predicate useUniversalConditions() { none() }
122120
}
123121

124122
private module SatisfiesBlanketConstraint =

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,8 +2200,6 @@ private module MethodResolution {
22002200
exists(mc) and
22012201
constraint.(TraitType).getTrait() instanceof DerefTrait
22022202
}
2203-
2204-
predicate useUniversalConditions() { none() }
22052203
}
22062204

22072205
private module MethodCallSatisfiesDerefConstraint =
@@ -3613,8 +3611,6 @@ private module AwaitSatisfiesConstraintInput implements SatisfiesConstraintInput
36133611
exists(term) and
36143612
constraint.(TraitType).getTrait() instanceof FutureTrait
36153613
}
3616-
3617-
predicate useUniversalConditions() { none() }
36183614
}
36193615

36203616
pragma[nomagic]
@@ -3811,8 +3807,6 @@ private module ForIterableSatisfiesConstraintInput implements
38113807
t instanceof IntoIteratorTrait
38123808
)
38133809
}
3814-
3815-
predicate useUniversalConditions() { none() }
38163810
}
38173811

38183812
pragma[nomagic]
@@ -3864,8 +3858,6 @@ private module InvokedClosureSatisfiesConstraintInput implements
38643858
exists(term) and
38653859
constraint.(TraitType).getTrait() instanceof FnOnceTrait
38663860
}
3867-
3868-
predicate useUniversalConditions() { none() }
38693861
}
38703862

38713863
/** Gets the type of `ce` when viewed as an implementation of `FnOnce`. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ mod method_non_parametric_trait_impl {
438438

439439
let thing = MyThing { a: S1 };
440440
let i = thing.convert_to(); // $ type=i:S1 target=T::convert_to
441-
let j = convert_to(thing); // $ type=j:S1 target=convert_to
441+
let j = convert_to(thing); // $ target=convert_to $ MISSING: type=j:S1 -- the blanket implementation `impl<T: MyTrait<S1>> ConvertTo<S1> for T` is currently not included in the constraint analysis
442442
}
443443
}
444444

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7151,8 +7151,6 @@ inferType
71517151
| main.rs:440:17:440:21 | thing | | main.rs:224:5:227:5 | MyThing |
71527152
| main.rs:440:17:440:21 | thing | A | main.rs:235:5:236:14 | S1 |
71537153
| main.rs:440:17:440:34 | thing.convert_to() | | main.rs:235:5:236:14 | S1 |
7154-
| main.rs:441:13:441:13 | j | | main.rs:235:5:236:14 | S1 |
7155-
| main.rs:441:17:441:33 | convert_to(...) | | main.rs:235:5:236:14 | S1 |
71567154
| main.rs:441:28:441:32 | thing | | main.rs:224:5:227:5 | MyThing |
71577155
| main.rs:441:28:441:32 | thing | A | main.rs:235:5:236:14 | S1 |
71587156
| main.rs:450:26:450:29 | SelfParam | | main.rs:449:5:453:5 | Self [trait OverlappingTrait] |

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

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -841,15 +841,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
841841
signature module SatisfiesConstraintInputSig<HasTypeTreeSig HasTypeTree> {
842842
/** Holds if it is relevant to know if `term` satisfies `constraint`. */
843843
predicate relevantConstraint(HasTypeTree term, Type constraint);
844-
845-
/**
846-
* Holds if constraints that are satisfied through conditions that are
847-
* universally quantified type parameters should be used. Such type
848-
* parameters might have type parameter constraints, and these are _not_
849-
* checked. Hence using these represent a trade-off between too many
850-
* constraints and too few constraints being satisfied.
851-
*/
852-
default predicate useUniversalConditions() { any() }
853844
}
854845

855846
module SatisfiesConstraint<
@@ -901,12 +892,14 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
901892
TypeMention constraintMention
902893
) {
903894
exists(Type type | hasTypeConstraint(tt, type, constraint) |
904-
useUniversalConditions() and // todo: remove, and instead check constraints
905-
not exists(countConstraintImplementations(type, constraint)) and
906-
conditionSatisfiesConstraintTypeAt(abs, condition, constraintMention, _, _) and
907-
resolveTypeMentionRoot(condition) = abs.getATypeParameter() and
908-
constraint = resolveTypeMentionRoot(constraintMention)
909-
or
895+
// TODO: Handle universal conditions properly, which means checking type parameter constraints
896+
// Also remember to update logic in `hasNotConstraintMention`
897+
//
898+
// not exists(countConstraintImplementations(type, constraint)) and
899+
// conditionSatisfiesConstraintTypeAt(abs, condition, constraintMention, _, _) and
900+
// resolveTypeMentionRoot(condition) = abs.getATypeParameter() and
901+
// constraint = resolveTypeMentionRoot(constraintMention)
902+
// or
910903
countConstraintImplementations(type, constraint) > 0 and
911904
rootTypesSatisfaction(type, constraint, abs, condition, constraintMention) and
912905
// When there are multiple ways the type could implement the
@@ -936,18 +929,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
936929
pragma[nomagic]
937930
private predicate hasNotConstraintMention(HasTypeTree tt, Type constraint) {
938931
exists(Type type | hasTypeConstraint(tt, type, constraint) |
939-
(
940-
not useUniversalConditions()
941-
or
942-
exists(countConstraintImplementations(type, constraint))
943-
or
944-
forall(TypeAbstraction abs, TypeMention condition, TypeMention constraintMention |
945-
conditionSatisfiesConstraintTypeAt(abs, condition, constraintMention, _, _) and
946-
resolveTypeMentionRoot(condition) = abs.getATypeParameter()
947-
|
948-
not constraint = resolveTypeMentionRoot(constraintMention)
949-
)
950-
) and
932+
// TODO: Handle universal conditions properly, which means taking type parameter constraints into account
933+
// (
934+
// exists(countConstraintImplementations(type, constraint))
935+
// or
936+
// forall(TypeAbstraction abs, TypeMention condition, TypeMention constraintMention |
937+
// conditionSatisfiesConstraintTypeAt(abs, condition, constraintMention, _, _) and
938+
// resolveTypeMentionRoot(condition) = abs.getATypeParameter()
939+
// |
940+
// not constraint = resolveTypeMentionRoot(constraintMention)
941+
// )
942+
// ) and
951943
(
952944
countConstraintImplementations(type, constraint) = 0
953945
or

0 commit comments

Comments
 (0)