Skip to content

Commit f55585d

Browse files
authored
Merge pull request #22759 from hvitved/rust/type-inference-more-bound-resolution
Type inference: Improve constraint matching
2 parents 3aebc64 + 96d56d9 commit f55585d

3 files changed

Lines changed: 191 additions & 63 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,31 @@ fn empty_array() {
28102810
pin_array(arr3, 1); // $ target=pin_array
28112811
}
28122812

2813+
mod contextual_bound {
2814+
#[derive(Default)]
2815+
struct S<T>(T);
2816+
struct MyThingA<A>(A);
2817+
struct MyThingB<B>(B);
2818+
2819+
trait MyTrait<T> {}
2820+
2821+
impl<T> MyTrait<T> for MyThingA<T> {}
2822+
2823+
impl<T> MyTrait<[T; 1]> for MyThingB<T> {}
2824+
2825+
fn bar<T1, T2: MyTrait<T1>>(x: T1, y: Option<T2>) {}
2826+
2827+
fn test_bar() {
2828+
let x = S(0);
2829+
let y = MyThingA(Default::default()); // $ type=y@MyThingA<A>.S<T>:i32 target=default
2830+
bar(x, Some(y)); // $ target=bar
2831+
2832+
let x = S(0);
2833+
let z = MyThingB(Default::default()); // $ type=z@MyThingB<B>.S<T>:i32 target=default
2834+
bar([x], Some(z)); // $ target=bar
2835+
}
2836+
}
2837+
28132838
fn main() {
28142839
field_access::f(); // $ target=f
28152840
method_impl::f(); // $ target=f

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

Lines changed: 108 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,10 +3763,16 @@ inferCertainType
37633763
| main.rs:2809:34:2809:34 | x | | main.rs:2809:18:2809:18 | T |
37643764
| main.rs:2809:40:2809:41 | { ... } | | {EXTERNAL LOCATION} | () |
37653765
| main.rs:2810:15:2810:18 | arr3 | | {EXTERNAL LOCATION} | [;] |
3766-
| main.rs:2813:11:2849:1 | { ... } | | {EXTERNAL LOCATION} | () |
3767-
| main.rs:2816:20:2816:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3768-
| main.rs:2816:41:2816:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3769-
| main.rs:2846:16:2846:19 | true | | {EXTERNAL LOCATION} | bool |
3766+
| main.rs:2825:33:2825:33 | x | | main.rs:2825:12:2825:13 | T1 |
3767+
| main.rs:2825:40:2825:40 | y | | {EXTERNAL LOCATION} | Option |
3768+
| main.rs:2825:40:2825:40 | y | T | main.rs:2825:16:2825:30 | T2 |
3769+
| main.rs:2825:55:2825:56 | { ... } | | {EXTERNAL LOCATION} | () |
3770+
| main.rs:2827:19:2835:5 | { ... } | | {EXTERNAL LOCATION} | () |
3771+
| main.rs:2834:13:2834:15 | [...] | | {EXTERNAL LOCATION} | [;] |
3772+
| main.rs:2838:11:2874:1 | { ... } | | {EXTERNAL LOCATION} | () |
3773+
| main.rs:2841:20:2841:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3774+
| main.rs:2841:41:2841:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
3775+
| main.rs:2871:16:2871:19 | true | | {EXTERNAL LOCATION} | bool |
37703776
| overloading.rs:4:19:4:23 | SelfParam | | {EXTERNAL LOCATION} | & |
37713777
| overloading.rs:4:19:4:23 | SelfParam | TRef | overloading.rs:2:5:11:5 | Self [trait FirstTrait] |
37723778
| overloading.rs:4:34:6:9 | { ... } | | {EXTERNAL LOCATION} | bool |
@@ -12773,49 +12779,104 @@ inferType
1277312779
| main.rs:2810:15:2810:18 | arr3 | | {EXTERNAL LOCATION} | [;] |
1277412780
| main.rs:2810:15:2810:18 | arr3 | TArray | {EXTERNAL LOCATION} | i32 |
1277512781
| main.rs:2810:21:2810:21 | 1 | | {EXTERNAL LOCATION} | i32 |
12776-
| main.rs:2813:11:2849:1 | { ... } | | {EXTERNAL LOCATION} | () |
12777-
| main.rs:2814:5:2814:21 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12778-
| main.rs:2815:5:2815:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
12779-
| main.rs:2816:5:2816:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
12780-
| main.rs:2816:20:2816:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
12781-
| main.rs:2816:41:2816:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
12782-
| main.rs:2817:5:2817:35 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12783-
| main.rs:2818:5:2818:41 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12784-
| main.rs:2819:5:2819:45 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12785-
| main.rs:2820:5:2820:30 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12786-
| main.rs:2821:5:2821:21 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12787-
| main.rs:2822:5:2822:27 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12788-
| main.rs:2823:5:2823:32 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12789-
| main.rs:2824:5:2824:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12790-
| main.rs:2825:5:2825:36 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12791-
| main.rs:2826:5:2826:35 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12792-
| main.rs:2827:5:2827:29 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12793-
| main.rs:2828:5:2828:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12794-
| main.rs:2829:5:2829:24 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12795-
| main.rs:2830:5:2830:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12796-
| main.rs:2831:5:2831:18 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12797-
| main.rs:2832:5:2832:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future |
12798-
| main.rs:2832:5:2832:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () |
12799-
| main.rs:2833:5:2833:19 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12800-
| main.rs:2834:5:2834:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12801-
| main.rs:2835:5:2835:14 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12802-
| main.rs:2836:5:2836:27 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12803-
| main.rs:2837:5:2837:15 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12804-
| main.rs:2838:5:2838:43 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12805-
| main.rs:2839:5:2839:15 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12806-
| main.rs:2840:5:2840:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12807-
| main.rs:2841:5:2841:28 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12808-
| main.rs:2842:5:2842:23 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12809-
| main.rs:2843:5:2843:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () |
12810-
| main.rs:2844:5:2844:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () |
12811-
| main.rs:2845:5:2845:20 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12812-
| main.rs:2846:5:2846:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box |
12813-
| main.rs:2846:5:2846:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global |
12814-
| main.rs:2846:5:2846:20 | ...::f(...) | T | main.rs:2564:5:2566:5 | dyn MyTrait |
12815-
| main.rs:2846:5:2846:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 |
12816-
| main.rs:2846:16:2846:19 | true | | {EXTERNAL LOCATION} | bool |
12817-
| main.rs:2847:5:2847:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12818-
| main.rs:2848:5:2848:17 | empty_array(...) | | {EXTERNAL LOCATION} | () |
12782+
| main.rs:2823:25:2823:25 | 1 | | {EXTERNAL LOCATION} | i32 |
12783+
| main.rs:2825:33:2825:33 | x | | main.rs:2825:12:2825:13 | T1 |
12784+
| main.rs:2825:40:2825:40 | y | | {EXTERNAL LOCATION} | Option |
12785+
| main.rs:2825:40:2825:40 | y | T | main.rs:2825:16:2825:30 | T2 |
12786+
| main.rs:2825:55:2825:56 | { ... } | | {EXTERNAL LOCATION} | () |
12787+
| main.rs:2827:19:2835:5 | { ... } | | {EXTERNAL LOCATION} | () |
12788+
| main.rs:2828:13:2828:13 | x | | main.rs:2814:5:2815:19 | S |
12789+
| main.rs:2828:13:2828:13 | x | T | {EXTERNAL LOCATION} | i32 |
12790+
| main.rs:2828:17:2828:20 | S(...) | | main.rs:2814:5:2815:19 | S |
12791+
| main.rs:2828:17:2828:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12792+
| main.rs:2828:19:2828:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12793+
| main.rs:2829:13:2829:13 | y | | main.rs:2816:5:2816:26 | MyThingA |
12794+
| main.rs:2829:13:2829:13 | y | A | main.rs:2814:5:2815:19 | S |
12795+
| main.rs:2829:13:2829:13 | y | A.T | {EXTERNAL LOCATION} | i32 |
12796+
| main.rs:2829:17:2829:44 | MyThingA(...) | | main.rs:2816:5:2816:26 | MyThingA |
12797+
| main.rs:2829:17:2829:44 | MyThingA(...) | A | main.rs:2814:5:2815:19 | S |
12798+
| main.rs:2829:17:2829:44 | MyThingA(...) | A.T | {EXTERNAL LOCATION} | i32 |
12799+
| main.rs:2829:26:2829:43 | ...::default(...) | | main.rs:2814:5:2815:19 | S |
12800+
| main.rs:2829:26:2829:43 | ...::default(...) | T | {EXTERNAL LOCATION} | i32 |
12801+
| main.rs:2830:9:2830:23 | bar(...) | | {EXTERNAL LOCATION} | () |
12802+
| main.rs:2830:13:2830:13 | x | | main.rs:2814:5:2815:19 | S |
12803+
| main.rs:2830:13:2830:13 | x | T | {EXTERNAL LOCATION} | i32 |
12804+
| main.rs:2830:16:2830:22 | Some(...) | | {EXTERNAL LOCATION} | Option |
12805+
| main.rs:2830:16:2830:22 | Some(...) | T | main.rs:2816:5:2816:26 | MyThingA |
12806+
| main.rs:2830:16:2830:22 | Some(...) | T.A | main.rs:2814:5:2815:19 | S |
12807+
| main.rs:2830:16:2830:22 | Some(...) | T.A.T | {EXTERNAL LOCATION} | i32 |
12808+
| main.rs:2830:21:2830:21 | y | | main.rs:2816:5:2816:26 | MyThingA |
12809+
| main.rs:2830:21:2830:21 | y | A | main.rs:2814:5:2815:19 | S |
12810+
| main.rs:2830:21:2830:21 | y | A.T | {EXTERNAL LOCATION} | i32 |
12811+
| main.rs:2832:13:2832:13 | x | | main.rs:2814:5:2815:19 | S |
12812+
| main.rs:2832:13:2832:13 | x | T | {EXTERNAL LOCATION} | i32 |
12813+
| main.rs:2832:17:2832:20 | S(...) | | main.rs:2814:5:2815:19 | S |
12814+
| main.rs:2832:17:2832:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12815+
| main.rs:2832:19:2832:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12816+
| main.rs:2833:13:2833:13 | z | | main.rs:2817:5:2817:26 | MyThingB |
12817+
| main.rs:2833:13:2833:13 | z | B | main.rs:2814:5:2815:19 | S |
12818+
| main.rs:2833:13:2833:13 | z | B.T | {EXTERNAL LOCATION} | i32 |
12819+
| main.rs:2833:17:2833:44 | MyThingB(...) | | main.rs:2817:5:2817:26 | MyThingB |
12820+
| main.rs:2833:17:2833:44 | MyThingB(...) | B | main.rs:2814:5:2815:19 | S |
12821+
| main.rs:2833:17:2833:44 | MyThingB(...) | B.T | {EXTERNAL LOCATION} | i32 |
12822+
| main.rs:2833:26:2833:43 | ...::default(...) | | main.rs:2814:5:2815:19 | S |
12823+
| main.rs:2833:26:2833:43 | ...::default(...) | T | {EXTERNAL LOCATION} | i32 |
12824+
| main.rs:2834:9:2834:25 | bar(...) | | {EXTERNAL LOCATION} | () |
12825+
| main.rs:2834:13:2834:15 | [...] | | {EXTERNAL LOCATION} | [;] |
12826+
| main.rs:2834:13:2834:15 | [...] | TArray | main.rs:2814:5:2815:19 | S |
12827+
| main.rs:2834:13:2834:15 | [...] | TArray.T | {EXTERNAL LOCATION} | i32 |
12828+
| main.rs:2834:14:2834:14 | x | | main.rs:2814:5:2815:19 | S |
12829+
| main.rs:2834:14:2834:14 | x | T | {EXTERNAL LOCATION} | i32 |
12830+
| main.rs:2834:18:2834:24 | Some(...) | | {EXTERNAL LOCATION} | Option |
12831+
| main.rs:2834:18:2834:24 | Some(...) | T | main.rs:2817:5:2817:26 | MyThingB |
12832+
| main.rs:2834:18:2834:24 | Some(...) | T.B | main.rs:2814:5:2815:19 | S |
12833+
| main.rs:2834:18:2834:24 | Some(...) | T.B.T | {EXTERNAL LOCATION} | i32 |
12834+
| main.rs:2834:23:2834:23 | z | | main.rs:2817:5:2817:26 | MyThingB |
12835+
| main.rs:2834:23:2834:23 | z | B | main.rs:2814:5:2815:19 | S |
12836+
| main.rs:2834:23:2834:23 | z | B.T | {EXTERNAL LOCATION} | i32 |
12837+
| main.rs:2838:11:2874:1 | { ... } | | {EXTERNAL LOCATION} | () |
12838+
| main.rs:2839:5:2839:21 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12839+
| main.rs:2840:5:2840:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
12840+
| main.rs:2841:5:2841:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
12841+
| main.rs:2841:20:2841:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
12842+
| main.rs:2841:41:2841:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
12843+
| main.rs:2842:5:2842:35 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12844+
| main.rs:2843:5:2843:41 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12845+
| main.rs:2844:5:2844:45 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12846+
| main.rs:2845:5:2845:30 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12847+
| main.rs:2846:5:2846:21 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12848+
| main.rs:2847:5:2847:27 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12849+
| main.rs:2848:5:2848:32 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12850+
| main.rs:2849:5:2849:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12851+
| main.rs:2850:5:2850:36 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12852+
| main.rs:2851:5:2851:35 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12853+
| main.rs:2852:5:2852:29 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12854+
| main.rs:2853:5:2853:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12855+
| main.rs:2854:5:2854:24 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12856+
| main.rs:2855:5:2855:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12857+
| main.rs:2856:5:2856:18 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12858+
| main.rs:2857:5:2857:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future |
12859+
| main.rs:2857:5:2857:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () |
12860+
| main.rs:2858:5:2858:19 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12861+
| main.rs:2859:5:2859:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12862+
| main.rs:2860:5:2860:14 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12863+
| main.rs:2861:5:2861:27 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12864+
| main.rs:2862:5:2862:15 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12865+
| main.rs:2863:5:2863:43 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12866+
| main.rs:2864:5:2864:15 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12867+
| main.rs:2865:5:2865:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12868+
| main.rs:2866:5:2866:28 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12869+
| main.rs:2867:5:2867:23 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12870+
| main.rs:2868:5:2868:41 | ...::test_all_patterns(...) | | {EXTERNAL LOCATION} | () |
12871+
| main.rs:2869:5:2869:49 | ...::box_patterns(...) | | {EXTERNAL LOCATION} | () |
12872+
| main.rs:2870:5:2870:20 | ...::test(...) | | {EXTERNAL LOCATION} | () |
12873+
| main.rs:2871:5:2871:20 | ...::f(...) | | {EXTERNAL LOCATION} | Box |
12874+
| main.rs:2871:5:2871:20 | ...::f(...) | A | {EXTERNAL LOCATION} | Global |
12875+
| main.rs:2871:5:2871:20 | ...::f(...) | T | main.rs:2564:5:2566:5 | dyn MyTrait |
12876+
| main.rs:2871:5:2871:20 | ...::f(...) | T.dyn(T) | {EXTERNAL LOCATION} | i32 |
12877+
| main.rs:2871:16:2871:19 | true | | {EXTERNAL LOCATION} | bool |
12878+
| main.rs:2872:5:2872:23 | ...::f(...) | | {EXTERNAL LOCATION} | () |
12879+
| main.rs:2873:5:2873:17 | empty_array(...) | | {EXTERNAL LOCATION} | () |
1281912880
| overloading.rs:4:19:4:23 | SelfParam | | {EXTERNAL LOCATION} | & |
1282012881
| overloading.rs:4:19:4:23 | SelfParam | TRef | overloading.rs:2:5:11:5 | Self [trait FirstTrait] |
1282112882
| overloading.rs:4:34:6:9 | { ... } | | {EXTERNAL LOCATION} | bool |

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,44 +1785,86 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
17851785
)
17861786
or
17871787
exists(TypeParameter tp, TypePath suffix, TypePath mid, TypePath pathToTp |
1788+
exists(TypeParameter constrainedTp, DeclarationPosition dpos |
1789+
typeParameterConstraintHasTypeParameter(target, constrainedTp, constraint, pathToTp,
1790+
tp) and
1791+
accessDeclarationPositionMatch(apos, dpos) and
1792+
constrainedTp = target.getDeclaredType(dpos, _)
1793+
)
1794+
|
17881795
/*
17891796
* Example:
17901797
*
17911798
* ```rust
1799+
* struct S<T> { ... }
1800+
*
17921801
* struct MyThing<A> { ... }
17931802
*
17941803
* trait MyTrait<B> { ... }
17951804
*
17961805
* impl<T> MyTrait<T> for MyThing<T> { ... }
17971806
*
1798-
* fn bar<T1, T2: MyTrait<T1>>(x: T1, y: T2) {}
1807+
* fn bar<T1, T2: MyTrait<T1>>(x: T1, y: Option<T2>) {}
17991808
*
1800-
* let x: i32 = ...;
1809+
* let x: S<i32> = ...;
18011810
* let y = MyThing(Default::default());
1802-
* bar(x, y);
1811+
* bar(x, Some(y));
18031812
* ```
18041813
*
1805-
* At `term` = `bar(x, y)`, we have
1814+
* At `term` = `bar(x, Some(y))`, we have
18061815
* - `constraint = MyTrait<T1>`,
1807-
* - `pathToTypeParamInConstraint` = `"B"`,
1808-
* - `pathToTypeParamInSub` = `"A"`,
1809-
* - `prefix` = `suffix` = `mid` = `""`,
1816+
* - `pathToTypeParamInConstraint` = `"MyTrait<B>"`,
1817+
* - `pathToTypeParamInSub` = `"MyThing<A>"`,
1818+
* - `prefix` = `"Option<T>"`
1819+
* - `mid` = `""`,
18101820
* - `tp = T1`,
1811-
* - `pathToTp` = `"B"`, and
1812-
* - `result` = `i32`.
1821+
* - `pathToTp` = `"MyTrait<B>"`, and
1822+
* - `suffix` = `""` and `result` = `S`, or
1823+
* - `suffix` = `"S<T>"` and `result` = `i32`.
18131824
*
1814-
* That is, it allows us to infer that the type of `y` is `MyThing<i32>`.
1825+
* That is, it allows us to infer that the type of `y` is `MyThing<S<i32>>`.
18151826
*/
18161827

18171828
typeMatch(a, e, target, suffix, result, tp) and
1818-
exists(TypeParameter constrainedTp, DeclarationPosition dpos |
1819-
typeParameterConstraintHasTypeParameter(target, constrainedTp, constraint, pathToTp,
1820-
tp) and
1821-
accessDeclarationPositionMatch(apos, dpos) and
1822-
constrainedTp = target.getDeclaredType(dpos, _)
1823-
) and
18241829
pathToTp = pathToTypeParamInConstraint.appendInverse(mid) and
18251830
path = prefix.append(pathToTypeParamInSub.append(mid).append(suffix))
1831+
or
1832+
/*
1833+
* Example:
1834+
*
1835+
* ```rust
1836+
* struct S<T> { ... }
1837+
*
1838+
* struct MyThing<A> { ... }
1839+
*
1840+
* trait MyTrait<B> { ... }
1841+
*
1842+
* impl<T> MyTrait<[T;1]> for MyThing<T> { ... }
1843+
*
1844+
* fn bar<T1, T2: MyTrait<T1>>(x: T1, y: Option<T2>) {}
1845+
*
1846+
* let x: S<i32> = ...;
1847+
* let y = MyThing(Default::default());
1848+
* bar([x], Some(y));
1849+
* ```
1850+
*
1851+
* At `term` = `bar([x], Some(y))`, we have
1852+
* - `constraint = MyTrait<T1>`,
1853+
* - `pathToTypeParamInConstraint` = `"MyTrait<B>.[;]<TArray>"`,
1854+
* - `pathToTypeParamInSub` = `"MyThing<A>"`,
1855+
* - `prefix` = `"Option<T>"`
1856+
* - `mid` = `"[;]<TArray>"`,
1857+
* - `tp = T1`,
1858+
* - `pathToTp` = `"MyTrait<B>"`, and
1859+
* - `suffix` = `""` and `result` = `S`, or
1860+
* - `suffix` = `"S<T>"` and `result` = `i32`.
1861+
*
1862+
* That is, it allows us to infer that the type of `y` is `MyThing<S<i32>>`.
1863+
*/
1864+
1865+
pathToTypeParamInConstraint = pathToTp.appendInverse(mid) and
1866+
typeMatch(a, e, target, mid.appendInverse(suffix), result, tp) and
1867+
path = prefix.append(pathToTypeParamInSub.append(suffix))
18261868
)
18271869
)
18281870
}

0 commit comments

Comments
 (0)