diff --git a/rust/ql/lib/codeql/rust/internal/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/TypeInference.qll index ed87b0c71043..b281889ef1d7 100644 --- a/rust/ql/lib/codeql/rust/internal/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/TypeInference.qll @@ -1430,11 +1430,19 @@ private module MethodResolution { * Holds if the method inside `i` with matching name and arity can be ruled * out as a target of this call, because the candidate receiver type represented * by `derefChain` and `borrow` is incompatible with the `self` parameter type. + * + * The types are incompatible because they disagree on a concrete type somewhere + * inside `root`. */ pragma[nomagic] - private predicate hasIncompatibleTarget(ImplOrTraitItemNode i, string derefChain, boolean borrow) { - ReceiverIsInstantiationOfSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this, derefChain, - borrow), i, _) + private predicate hasIncompatibleTarget( + ImplOrTraitItemNode i, string derefChain, boolean borrow, Type root + ) { + exists(TypePath path | + ReceiverIsInstantiationOfSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this, + derefChain, borrow), i, _, path) and + path.isCons(root.getATypeParameter(), _) + ) } /** @@ -1448,7 +1456,7 @@ private module MethodResolution { ImplItemNode impl, string derefChain, boolean borrow ) { ReceiverIsNotInstantiationOfBlanketLikeSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this, - derefChain, borrow), impl, _) + derefChain, borrow), impl, _, _) or ReceiverSatisfiesBlanketLikeConstraint::dissatisfiesBlanketConstraint(MkMethodCallCand(this, derefChain, borrow), impl) @@ -1479,7 +1487,7 @@ private module MethodResolution { forall(ImplOrTraitItemNode i | methodCallNonBlanketCandidate(this, _, i, _, strippedTypePath, strippedType) | - this.hasIncompatibleTarget(i, derefChain, borrow) + this.hasIncompatibleTarget(i, derefChain, borrow, strippedType) ) } @@ -1818,7 +1826,7 @@ private module MethodResolution { */ pragma[nomagic] private predicate hasIncompatibleInherentTarget(Impl impl) { - ReceiverIsNotInstantiationOfInherentSelfParam::argIsNotInstantiationOf(this, impl, _) + ReceiverIsNotInstantiationOfInherentSelfParam::argIsNotInstantiationOf(this, impl, _, _) } /** diff --git a/rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll b/rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll index c219ef0eacca..a74ff762c583 100644 --- a/rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll +++ b/rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll @@ -256,8 +256,10 @@ module ArgIsInstantiationOf< ArgSubstIsInstantiationOf::isInstantiationOf(arg, i, constraint) } - predicate argIsNotInstantiationOf(Arg arg, ImplOrTraitItemNode i, AssocFunctionType constraint) { - ArgSubstIsInstantiationOf::isNotInstantiationOf(arg, i, constraint) + predicate argIsNotInstantiationOf( + Arg arg, ImplOrTraitItemNode i, AssocFunctionType constraint, TypePath path + ) { + ArgSubstIsInstantiationOf::isNotInstantiationOf(arg, i, constraint, path) } } diff --git a/rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected b/rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected index 38a68818c75b..d2807602e2f9 100644 --- a/rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected +++ b/rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected @@ -30,5 +30,6 @@ multipleResolvedTargets | main.rs:2642:13:2642:31 | ...::from(...) | | main.rs:2643:13:2643:31 | ...::from(...) | | main.rs:2644:13:2644:31 | ...::from(...) | +| main.rs:3067:13:3067:17 | x.f() | | pattern_matching.rs:273:13:273:27 | * ... | | pattern_matching.rs:273:14:273:27 | * ... | diff --git a/rust/ql/test/library-tests/type-inference/main.rs b/rust/ql/test/library-tests/type-inference/main.rs index f78202969b8a..466733cf47c7 100644 --- a/rust/ql/test/library-tests/type-inference/main.rs +++ b/rust/ql/test/library-tests/type-inference/main.rs @@ -3036,6 +3036,39 @@ mod context_typed { } } +mod literal_overlap { + trait MyTrait { + fn f(self) -> Self; + } + + impl MyTrait for i32 { + // i32f + fn f(self) -> Self { + self + } + } + + impl MyTrait for usize { + // usizef + fn f(self) -> Self { + self + } + } + + impl MyTrait for &T { + // Reff + fn f(self) -> Self { + self + } + } + + pub fn f() -> usize { + let mut x = 0; + x = x.f(); // $ target=usizef $ SPURIOUS: target=i32f + x + } +} + mod blanket_impl; mod closure; mod dereference; 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 ff3545f0c0fc..14cc45775397 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -3529,48 +3529,62 @@ inferCertainType | main.rs:3032:9:3032:9 | x | A | {EXTERNAL LOCATION} | Global | | main.rs:3035:9:3035:9 | x | | {EXTERNAL LOCATION} | Vec | | main.rs:3035:9:3035:9 | x | A | {EXTERNAL LOCATION} | Global | -| main.rs:3044:11:3079:1 | { ... } | | {EXTERNAL LOCATION} | () | -| main.rs:3045:5:3045:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3046:5:3046:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:5:3047:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:20:3047:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:41:3047:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | -| main.rs:3048:5:3048:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3049:5:3049:41 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3050:5:3050:45 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3051:5:3051:30 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3052:5:3052:33 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3053:5:3053:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3054:5:3054:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3055:5:3055:32 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3056:5:3056:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3057:5:3057:36 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3058:5:3058:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3059:5:3059:29 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3060:5:3060:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3061:5:3061:24 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3062:5:3062:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3063:5:3063:18 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3064:5:3064:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future | -| main.rs:3064:5:3064:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () | -| main.rs:3065:5:3065:19 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3066:5:3066:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3067:5:3067:14 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3068:5:3068:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3069:5:3069:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3070:5:3070:43 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3071:5:3071:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3072:5:3072:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3073:5:3073:23 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3074:5:3074:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3075:5:3075:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3076:5:3076:20 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3077:5:3077:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box | -| main.rs:3077:5:3077:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global | -| main.rs:3077:5:3077:20 | ...::f(...) | T | main.rs:2897:5:2899:5 | dyn MyTrait | -| main.rs:3077:5:3077:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 | -| main.rs:3077:16:3077:19 | true | | {EXTERNAL LOCATION} | bool | -| main.rs:3078:5:3078:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3041:14:3041:17 | SelfParam | | main.rs:3040:5:3042:5 | Self [trait MyTrait] | +| main.rs:3046:14:3046:17 | SelfParam | | {EXTERNAL LOCATION} | i32 | +| main.rs:3046:28:3048:9 | { ... } | | {EXTERNAL LOCATION} | i32 | +| main.rs:3047:13:3047:16 | self | | {EXTERNAL LOCATION} | i32 | +| main.rs:3053:14:3053:17 | SelfParam | | {EXTERNAL LOCATION} | usize | +| main.rs:3053:28:3055:9 | { ... } | | {EXTERNAL LOCATION} | usize | +| main.rs:3054:13:3054:16 | self | | {EXTERNAL LOCATION} | usize | +| main.rs:3060:14:3060:17 | SelfParam | | {EXTERNAL LOCATION} | & | +| main.rs:3060:14:3060:17 | SelfParam | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3060:28:3062:9 | { ... } | | {EXTERNAL LOCATION} | & | +| main.rs:3060:28:3062:9 | { ... } | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3061:13:3061:16 | self | | {EXTERNAL LOCATION} | & | +| main.rs:3061:13:3061:16 | self | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3065:25:3069:5 | { ... } | | {EXTERNAL LOCATION} | usize | +| main.rs:3077:11:3112:1 | { ... } | | {EXTERNAL LOCATION} | () | +| main.rs:3078:5:3078:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3079:5:3079:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:5:3080:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:20:3080:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:41:3080:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | +| main.rs:3081:5:3081:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3082:5:3082:41 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3083:5:3083:45 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3084:5:3084:30 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3085:5:3085:33 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3086:5:3086:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3087:5:3087:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3088:5:3088:32 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3089:5:3089:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3090:5:3090:36 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3091:5:3091:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3092:5:3092:29 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3093:5:3093:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3094:5:3094:24 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3095:5:3095:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3096:5:3096:18 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3097:5:3097:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future | +| main.rs:3097:5:3097:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () | +| main.rs:3098:5:3098:19 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3099:5:3099:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3100:5:3100:14 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3101:5:3101:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3102:5:3102:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3103:5:3103:43 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3104:5:3104:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3105:5:3105:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3106:5:3106:23 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3107:5:3107:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3108:5:3108:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3109:5:3109:20 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3110:5:3110:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box | +| main.rs:3110:5:3110:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global | +| main.rs:3110:5:3110:20 | ...::f(...) | T | main.rs:2897:5:2899:5 | dyn MyTrait | +| main.rs:3110:5:3110:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 | +| main.rs:3110:16:3110:19 | true | | {EXTERNAL LOCATION} | bool | +| main.rs:3111:5:3111:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | | pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option | | pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () | | pattern_matching.rs:15:5:18:5 | if ... {...} | | {EXTERNAL LOCATION} | () | @@ -10983,48 +10997,75 @@ inferType | main.rs:3035:9:3035:9 | x | T | {EXTERNAL LOCATION} | i32 | | main.rs:3035:9:3035:17 | x.push(...) | | {EXTERNAL LOCATION} | () | | main.rs:3035:16:3035:16 | y | | {EXTERNAL LOCATION} | i32 | -| main.rs:3044:11:3079:1 | { ... } | | {EXTERNAL LOCATION} | () | -| main.rs:3045:5:3045:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3046:5:3046:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:5:3047:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:20:3047:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | -| main.rs:3047:41:3047:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | -| main.rs:3048:5:3048:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3049:5:3049:41 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3050:5:3050:45 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3051:5:3051:30 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3052:5:3052:33 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3053:5:3053:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3054:5:3054:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3055:5:3055:32 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3056:5:3056:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3057:5:3057:36 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3058:5:3058:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3059:5:3059:29 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3060:5:3060:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3061:5:3061:24 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3062:5:3062:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3063:5:3063:18 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3064:5:3064:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future | -| main.rs:3064:5:3064:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () | -| main.rs:3065:5:3065:19 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3066:5:3066:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3067:5:3067:14 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3068:5:3068:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3069:5:3069:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3070:5:3070:43 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3071:5:3071:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3072:5:3072:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3073:5:3073:23 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3074:5:3074:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3075:5:3075:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3076:5:3076:20 | ...::test(...) | | {EXTERNAL LOCATION} | () | -| main.rs:3077:5:3077:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box | -| main.rs:3077:5:3077:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global | -| main.rs:3077:5:3077:20 | ...::f(...) | T | main.rs:2897:5:2899:5 | dyn MyTrait | -| main.rs:3077:5:3077:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 | -| main.rs:3077:16:3077:19 | true | | {EXTERNAL LOCATION} | bool | -| main.rs:3078:5:3078:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3041:14:3041:17 | SelfParam | | main.rs:3040:5:3042:5 | Self [trait MyTrait] | +| main.rs:3046:14:3046:17 | SelfParam | | {EXTERNAL LOCATION} | i32 | +| main.rs:3046:28:3048:9 | { ... } | | {EXTERNAL LOCATION} | i32 | +| main.rs:3047:13:3047:16 | self | | {EXTERNAL LOCATION} | i32 | +| main.rs:3053:14:3053:17 | SelfParam | | {EXTERNAL LOCATION} | usize | +| main.rs:3053:28:3055:9 | { ... } | | {EXTERNAL LOCATION} | usize | +| main.rs:3054:13:3054:16 | self | | {EXTERNAL LOCATION} | usize | +| main.rs:3060:14:3060:17 | SelfParam | | {EXTERNAL LOCATION} | & | +| main.rs:3060:14:3060:17 | SelfParam | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3060:28:3062:9 | { ... } | | {EXTERNAL LOCATION} | & | +| main.rs:3060:28:3062:9 | { ... } | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3061:13:3061:16 | self | | {EXTERNAL LOCATION} | & | +| main.rs:3061:13:3061:16 | self | TRef | main.rs:3058:10:3058:10 | T | +| main.rs:3065:25:3069:5 | { ... } | | {EXTERNAL LOCATION} | usize | +| main.rs:3066:17:3066:17 | x | | {EXTERNAL LOCATION} | i32 | +| main.rs:3066:17:3066:17 | x | | {EXTERNAL LOCATION} | usize | +| main.rs:3066:21:3066:21 | 0 | | {EXTERNAL LOCATION} | i32 | +| main.rs:3066:21:3066:21 | 0 | | {EXTERNAL LOCATION} | usize | +| main.rs:3067:9:3067:9 | x | | {EXTERNAL LOCATION} | i32 | +| main.rs:3067:9:3067:9 | x | | {EXTERNAL LOCATION} | usize | +| main.rs:3067:9:3067:17 | ... = ... | | {EXTERNAL LOCATION} | () | +| main.rs:3067:13:3067:13 | x | | {EXTERNAL LOCATION} | i32 | +| main.rs:3067:13:3067:13 | x | | {EXTERNAL LOCATION} | usize | +| main.rs:3067:13:3067:17 | x.f() | | {EXTERNAL LOCATION} | i32 | +| main.rs:3067:13:3067:17 | x.f() | | {EXTERNAL LOCATION} | usize | +| main.rs:3068:9:3068:9 | x | | {EXTERNAL LOCATION} | i32 | +| main.rs:3068:9:3068:9 | x | | {EXTERNAL LOCATION} | usize | +| main.rs:3077:11:3112:1 | { ... } | | {EXTERNAL LOCATION} | () | +| main.rs:3078:5:3078:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3079:5:3079:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:5:3080:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:20:3080:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | +| main.rs:3080:41:3080:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo | +| main.rs:3081:5:3081:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3082:5:3082:41 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3083:5:3083:45 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3084:5:3084:30 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3085:5:3085:33 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3086:5:3086:21 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3087:5:3087:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3088:5:3088:32 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3089:5:3089:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3090:5:3090:36 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3091:5:3091:35 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3092:5:3092:29 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3093:5:3093:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3094:5:3094:24 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3095:5:3095:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3096:5:3096:18 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3097:5:3097:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future | +| main.rs:3097:5:3097:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () | +| main.rs:3098:5:3098:19 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3099:5:3099:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3100:5:3100:14 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3101:5:3101:27 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3102:5:3102:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3103:5:3103:43 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3104:5:3104:15 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3105:5:3105:17 | ...::f(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3106:5:3106:23 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3107:5:3107:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3108:5:3108:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3109:5:3109:20 | ...::test(...) | | {EXTERNAL LOCATION} | () | +| main.rs:3110:5:3110:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box | +| main.rs:3110:5:3110:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global | +| main.rs:3110:5:3110:20 | ...::f(...) | T | main.rs:2897:5:2899:5 | dyn MyTrait | +| main.rs:3110:5:3110:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 | +| main.rs:3110:16:3110:19 | true | | {EXTERNAL LOCATION} | bool | +| main.rs:3111:5:3111:23 | ...::f(...) | | {EXTERNAL LOCATION} | () | | pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option | | pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () | | pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option | diff --git a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll index bf107c7d3f55..4bb348979c1c 100644 --- a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll +++ b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll @@ -705,7 +705,8 @@ module Make1 Input1> { } /** - * Holds if `app` is _not_ a possible instantiation of `constraint`. + * Holds if `app` is _not_ a possible instantiation of `constraint`, because `app` + * and `constraint` differ on concrete types at `path`. * * This is an approximation of `not isInstantiationOf(app, abs, constraint)`, but * defined without a negative occurrence of `isInstantiationOf`. @@ -719,9 +720,11 @@ module Make1 Input1> { * `isInstantiationOf` nor `isNotInstantiationOf` will hold. */ pragma[nomagic] - predicate isNotInstantiationOf(App app, TypeAbstraction abs, Constraint constraint) { + predicate isNotInstantiationOf( + App app, TypeAbstraction abs, Constraint constraint, TypePath path + ) { // `app` and `constraint` differ on a concrete type - exists(Type t, Type t2, TypePath path | + exists(Type t, Type t2 | t = resolveTypeAt(app, abs, constraint, path) and not t = abs.getATypeParameter() and app.getTypeAt(path) = t2 and @@ -983,6 +986,9 @@ module Make1 Input1> { } } + private module SatisfiesConstraintIsInstantiationOf = + IsInstantiationOf; + /** * Holds if `tt` satisfies `constraint` through `abs`, `sub`, and `constraintMention`. */ @@ -1004,13 +1010,21 @@ module Make1 Input1> { // constraint we need to find the right implementation, which is the // one where the type instantiates the precondition. if multipleConstraintImplementations(type, constraint) - then - IsInstantiationOf::isInstantiationOf(tt, - abs, condition) + then SatisfiesConstraintIsInstantiationOf::isInstantiationOf(tt, abs, condition) else any() ) } + pragma[nomagic] + private predicate isNotInstantiationOf( + HasTypeTree tt, TypeAbstraction abs, TypeMention condition, Type root + ) { + exists(TypePath path | + SatisfiesConstraintIsInstantiationOf::isNotInstantiationOf(tt, abs, condition, path) and + path.isCons(root.getATypeParameter(), _) + ) + } + /** * Holds if `tt` does not satisfy `constraint`. * @@ -1040,8 +1054,7 @@ module Make1 Input1> { forex(TypeAbstraction abs, TypeMention condition | rootTypesSatisfaction(type, constraint, abs, condition, _) | - IsInstantiationOf::isNotInstantiationOf(tt, - abs, condition) + isNotInstantiationOf(tt, abs, condition, type) ) ) )