Skip to content

Rust: Also apply adjustedAccessType in RelevantAccess #19729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,17 +985,18 @@ module Make1<LocationSig Location, InputSig1<Location> 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, _)
}

/**
Expand All @@ -1004,19 +1005,20 @@ module Make1<LocationSig Location, InputSig1<Location> 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() {
Expand Down Expand Up @@ -1076,7 +1078,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
TypeAbstraction abs, TypeMention sub, TypePath path, Type t
) {
exists(TypeMention constraintMention |
at = MkRelevantAccess(a, apos, prefix) and
at = MkRelevantAccess(a, _, apos, prefix) and
Copy link
Preview

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using an underscore for the target parameter in MkRelevantAccess can obscure which argument is being ignored; consider naming this dummy parameter explicitly (e.g., dummyTarget) to improve readability.

Suggested change
at = MkRelevantAccess(a, _, apos, prefix) and
at = MkRelevantAccess(a, dummyTarget, apos, prefix) and

Copilot uses AI. Check for mistakes.

hasConstraintMention(at, abs, sub, constraint, constraintMention) and
conditionSatisfiesConstraintTypeAt(abs, sub, constraintMention, path, t)
)
Expand Down
Loading