Skip to content

Commit 93fd6ec

Browse files
authored
Merge pull request #19729 from hvitved/rust/type-inference-adjust-type-relevant-access
Rust: Also apply `adjustedAccessType` in `RelevantAccess`
2 parents 9e6ce98 + 18392a0 commit 93fd6ec

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ mod trait_associated_type {
697697
println!("{:?}", x3.put(1).unwrap()); // $ method=S::put method=unwrap
698698

699699
// Call to default implementation in `trait` block
700-
println!("{:?}", x3.putTwo(2, 3).unwrap()); // $ method=putTwo MISSING: method=unwrap
700+
println!("{:?}", x3.putTwo(2, 3).unwrap()); // $ method=putTwo method=unwrap
701701

702702
let x4 = g(S); // $ MISSING: type=x4:AT
703703
println!("{:?}", x4);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ inferType
778778
| main.rs:697:33:697:33 | 1 | | {EXTERNAL LOCATION} | i32 |
779779
| main.rs:700:18:700:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
780780
| main.rs:700:26:700:27 | x3 | | main.rs:619:5:620:13 | S |
781+
| main.rs:700:26:700:40 | x3.putTwo(...) | | main.rs:568:5:571:5 | Wrapper |
782+
| main.rs:700:26:700:40 | x3.putTwo(...) | A | main.rs:639:36:639:50 | AssociatedParam |
783+
| main.rs:700:26:700:49 | ... .unwrap() | | main.rs:639:36:639:50 | AssociatedParam |
781784
| main.rs:700:36:700:36 | 2 | | {EXTERNAL LOCATION} | i32 |
782785
| main.rs:700:39:700:39 | 3 | | {EXTERNAL LOCATION} | i32 |
783786
| main.rs:702:20:702:20 | S | | main.rs:619:5:620:13 | S |

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -985,17 +985,18 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
985985

986986
private module AccessConstraint {
987987
predicate relevantAccessConstraint(
988-
Access a, AccessPosition apos, TypePath path, Type constraint
988+
Access a, Declaration target, AccessPosition apos, TypePath path, Type constraint
989989
) {
990990
exists(DeclarationPosition dpos |
991991
accessDeclarationPositionMatch(apos, dpos) and
992-
typeParameterConstraintHasTypeParameter(a.getTarget(), dpos, path, _, constraint, _, _)
992+
target = a.getTarget() and
993+
typeParameterConstraintHasTypeParameter(target, dpos, path, _, constraint, _, _)
993994
)
994995
}
995996

996997
private newtype TRelevantAccess =
997-
MkRelevantAccess(Access a, AccessPosition apos, TypePath path) {
998-
relevantAccessConstraint(a, apos, path, _)
998+
MkRelevantAccess(Access a, Declaration target, AccessPosition apos, TypePath path) {
999+
relevantAccessConstraint(a, target, apos, path, _)
9991000
}
10001001

10011002
/**
@@ -1004,19 +1005,20 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10041005
*/
10051006
private class RelevantAccess extends MkRelevantAccess {
10061007
Access a;
1008+
Declaration target;
10071009
AccessPosition apos;
10081010
TypePath path;
10091011

1010-
RelevantAccess() { this = MkRelevantAccess(a, apos, path) }
1012+
RelevantAccess() { this = MkRelevantAccess(a, target, apos, path) }
10111013

10121014
Type getTypeAt(TypePath suffix) {
1013-
a.getInferredType(apos, path.appendInverse(suffix)) = result
1015+
adjustedAccessType(a, apos, target, path.appendInverse(suffix), result)
10141016
}
10151017

10161018
/** Holds if this relevant access has the type `type` and should satisfy `constraint`. */
10171019
predicate hasTypeConstraint(Type type, Type constraint) {
1018-
type = a.getInferredType(apos, path) and
1019-
relevantAccessConstraint(a, apos, path, constraint)
1020+
adjustedAccessType(a, apos, target, path, type) and
1021+
relevantAccessConstraint(a, target, apos, path, constraint)
10201022
}
10211023

10221024
string toString() {
@@ -1076,7 +1078,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10761078
TypeAbstraction abs, TypeMention sub, TypePath path, Type t
10771079
) {
10781080
exists(TypeMention constraintMention |
1079-
at = MkRelevantAccess(a, apos, prefix) and
1081+
at = MkRelevantAccess(a, _, apos, prefix) and
10801082
hasConstraintMention(at, abs, sub, constraint, constraintMention) and
10811083
conditionSatisfiesConstraintTypeAt(abs, sub, constraintMention, path, t)
10821084
)

0 commit comments

Comments
 (0)