From 18392a07ceb8492f81c421d7806e32529537d7da Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Wed, 11 Jun 2025 14:13:27 +0200 Subject: [PATCH] Rust: Also apply `adjustedAccessType` in `RelevantAccess` --- .../test/library-tests/type-inference/main.rs | 2 +- .../type-inference/type-inference.expected | 3 +++ .../typeinference/internal/TypeInference.qll | 20 ++++++++++--------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/rust/ql/test/library-tests/type-inference/main.rs b/rust/ql/test/library-tests/type-inference/main.rs index 497bf331e511..929a32026da0 100644 --- a/rust/ql/test/library-tests/type-inference/main.rs +++ b/rust/ql/test/library-tests/type-inference/main.rs @@ -697,7 +697,7 @@ mod trait_associated_type { println!("{:?}", x3.put(1).unwrap()); // $ method=S::put method=unwrap // Call to default implementation in `trait` block - println!("{:?}", x3.putTwo(2, 3).unwrap()); // $ method=putTwo MISSING: method=unwrap + println!("{:?}", x3.putTwo(2, 3).unwrap()); // $ method=putTwo method=unwrap let x4 = g(S); // $ MISSING: type=x4:AT println!("{:?}", x4); diff --git a/rust/ql/test/library-tests/type-inference/type-inference.expected b/rust/ql/test/library-tests/type-inference/type-inference.expected index 30672ca89e52..f48207dfa827 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -778,6 +778,9 @@ inferType | main.rs:697:33:697:33 | 1 | | {EXTERNAL LOCATION} | i32 | | main.rs:700:18:700:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str | | main.rs:700:26:700:27 | x3 | | main.rs:619:5:620:13 | S | +| main.rs:700:26:700:40 | x3.putTwo(...) | | main.rs:568:5:571:5 | Wrapper | +| main.rs:700:26:700:40 | x3.putTwo(...) | A | main.rs:639:36:639:50 | AssociatedParam | +| main.rs:700:26:700:49 | ... .unwrap() | | main.rs:639:36:639:50 | AssociatedParam | | main.rs:700:36:700:36 | 2 | | {EXTERNAL LOCATION} | i32 | | main.rs:700:39:700:39 | 3 | | {EXTERNAL LOCATION} | i32 | | main.rs:702:20:702:20 | S | | main.rs:619:5:620:13 | S | diff --git a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll index 823459e83bb4..b9948ffbd296 100644 --- a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll +++ b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll @@ -985,17 +985,18 @@ module Make1 Input1> { private module AccessConstraint { predicate relevantAccessConstraint( - Access a, AccessPosition apos, TypePath path, Type constraint + Access a, Declaration target, AccessPosition apos, TypePath path, Type constraint ) { exists(DeclarationPosition dpos | accessDeclarationPositionMatch(apos, dpos) and - typeParameterConstraintHasTypeParameter(a.getTarget(), dpos, path, _, constraint, _, _) + target = a.getTarget() and + typeParameterConstraintHasTypeParameter(target, dpos, path, _, constraint, _, _) ) } private newtype TRelevantAccess = - MkRelevantAccess(Access a, AccessPosition apos, TypePath path) { - relevantAccessConstraint(a, apos, path, _) + MkRelevantAccess(Access a, Declaration target, AccessPosition apos, TypePath path) { + relevantAccessConstraint(a, target, apos, path, _) } /** @@ -1004,19 +1005,20 @@ module Make1 Input1> { */ private class RelevantAccess extends MkRelevantAccess { Access a; + Declaration target; AccessPosition apos; TypePath path; - RelevantAccess() { this = MkRelevantAccess(a, apos, path) } + RelevantAccess() { this = MkRelevantAccess(a, target, apos, path) } Type getTypeAt(TypePath suffix) { - a.getInferredType(apos, path.appendInverse(suffix)) = result + adjustedAccessType(a, apos, target, path.appendInverse(suffix), result) } /** Holds if this relevant access has the type `type` and should satisfy `constraint`. */ predicate hasTypeConstraint(Type type, Type constraint) { - type = a.getInferredType(apos, path) and - relevantAccessConstraint(a, apos, path, constraint) + adjustedAccessType(a, apos, target, path, type) and + relevantAccessConstraint(a, target, apos, path, constraint) } string toString() { @@ -1076,7 +1078,7 @@ module Make1 Input1> { TypeAbstraction abs, TypeMention sub, TypePath path, Type t ) { exists(TypeMention constraintMention | - at = MkRelevantAccess(a, apos, prefix) and + at = MkRelevantAccess(a, _, apos, prefix) and hasConstraintMention(at, abs, sub, constraint, constraintMention) and conditionSatisfiesConstraintTypeAt(abs, sub, constraintMention, path, t) )