From f9869daa91d56aef23a2b78f518e797206ecd5af Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Tue, 24 Feb 2026 12:03:15 +0100 Subject: [PATCH] Address review comments --- .../internal/typeinference/TypeInference.qll | 19 +++++++++---------- .../test/library-tests/type-inference/main.rs | 2 +- .../type-inference/type-inference.expected | 3 +++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll index 08ef69ff596..50431c6775b 100644 --- a/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll @@ -1033,7 +1033,7 @@ pragma[nomagic] private Type inferStructExprType0(AstNode n, FunctionPosition pos, TypePath path) { exists(StructExprMatchingInput::Access a, StructExprMatchingInput::AccessPosition apos | n = a.getNodeAt(apos) and - if apos.isStructPos() then pos.isReturn() else pos.asPosition() = 0 // the acutal position doesn't matter, as long as it is positional + if apos.isStructPos() then pos.isReturn() else pos.asPosition() = 0 // the actual position doesn't matter, as long as it is positional | result = StructExprMatching::inferAccessType(a, apos, path) or @@ -1229,12 +1229,9 @@ private module ContextTyping { result = inferCallNonReturnType(n, pos, prefix, path) and hasUnknownTypeAt(n, prefix) | - pos.isPosition() - or // Never propagate type information directly into the receiver, since its type // must already have been known in order to resolve the call - pos.isSelf() and - not prefix.isEmpty() + if pos.isSelf() then not prefix.isEmpty() else any() ) } } @@ -2648,12 +2645,12 @@ private Type inferMethodCallTypeNonSelf(AstNode n, FunctionPosition pos, TypePat * empty, at which point the inferred type can be applied back to `n`. */ pragma[nomagic] -private Type inferMethodCallTypeSelf(AstNode n, DerefChain derefChain, TypePath path) { +private Type inferMethodCallTypeSelf(MethodCall mc, AstNode n, DerefChain derefChain, TypePath path) { exists( MethodCallMatchingInput::AccessPosition apos, string derefChainBorrow, BorrowKind borrow, TypePath path0 | - result = inferMethodCallType0(_, apos, n, derefChainBorrow, path0) and + result = inferMethodCallType0(mc, apos, n, derefChainBorrow, path0) and apos.isSelf() and MethodCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow) | @@ -2672,7 +2669,7 @@ private Type inferMethodCallTypeSelf(AstNode n, DerefChain derefChain, TypePath DerefChain derefChain0, Type t0, TypePath path0, DerefImplItemNode impl, Type selfParamType, TypePath selfPath | - t0 = inferMethodCallTypeSelf(n, derefChain0, path0) and + t0 = inferMethodCallTypeSelf(mc, n, derefChain0, path0) and derefChain0.isCons(impl, derefChain) and selfParamType = impl.resolveSelfTypeAt(selfPath) | @@ -2692,8 +2689,10 @@ private Type inferMethodCallTypeSelf(AstNode n, DerefChain derefChain, TypePath private Type inferMethodCallTypePreCheck(AstNode n, FunctionPosition pos, TypePath path) { result = inferMethodCallTypeNonSelf(n, pos, path) or - result = inferMethodCallTypeSelf(n, DerefChain::nil(), path) and - pos.isSelf() + exists(MethodCall mc | + result = inferMethodCallTypeSelf(mc, n, DerefChain::nil(), path) and + if mc instanceof CallExpr then pos.asPosition() = 0 else pos.isSelf() + ) } /** diff --git a/rust/ql/test/library-tests/type-inference/main.rs b/rust/ql/test/library-tests/type-inference/main.rs index dedd45b40d1..f342d3897f4 100644 --- a/rust/ql/test/library-tests/type-inference/main.rs +++ b/rust/ql/test/library-tests/type-inference/main.rs @@ -2691,7 +2691,7 @@ mod context_typed { let y = Default::default(); // $ type=y:i32 target=default x.push(y); // $ target=push - let s = Default::default(); // $ MISSING: target=default type=s:S + let s = Default::default(); // $ target=default type=s:S S::f(s); // $ target=f } } 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 57e4131a32e..fa0f9eaa0c2 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -12100,7 +12100,10 @@ inferType | main.rs:2692:9:2692:9 | x | T | {EXTERNAL LOCATION} | i32 | | main.rs:2692:9:2692:17 | x.push(...) | | {EXTERNAL LOCATION} | () | | main.rs:2692:16:2692:16 | y | | {EXTERNAL LOCATION} | i32 | +| main.rs:2694:13:2694:13 | s | | main.rs:2639:5:2640:13 | S | +| main.rs:2694:17:2694:34 | ...::default(...) | | main.rs:2639:5:2640:13 | S | | main.rs:2695:9:2695:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:2695:14:2695:14 | s | | main.rs:2639:5:2640:13 | S | | main.rs:2702:14:2702:17 | SelfParam | | main.rs:2700:5:2708:5 | Self [trait MyTrait] | | main.rs:2705:14:2705:18 | SelfParam | | {EXTERNAL LOCATION} | & | | main.rs:2705:14:2705:18 | SelfParam | TRef | main.rs:2700:5:2708:5 | Self [trait MyTrait] |