Skip to content

Commit 2895441

Browse files
committed
Rust: More type inference tests
1 parent e567b60 commit 2895441

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
multipleResolvedTargets
22
| main.rs:2720:13:2720:17 | x.f() |
3+
| overloading.rs:197:22:197:30 | x.g(...) |

rust/ql/test/library-tests/type-inference/overloading.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,89 @@ pub mod impl_overlap {
150150
S5::m(&S5(true)); // $ target=MyTrait1::m
151151
}
152152
}
153+
154+
mod impl_overlap2 {
155+
trait Trait1<T1> {
156+
fn f(self, x: T1) -> T1;
157+
}
158+
159+
impl Trait1<i32> for i32 {
160+
// f1
161+
fn f(self, x: i32) -> i32 {
162+
0
163+
}
164+
}
165+
166+
impl Trait1<i64> for i32 {
167+
// f2
168+
fn f(self, x: i64) -> i64 {
169+
0
170+
}
171+
}
172+
173+
trait Trait2<T1, T2> {
174+
fn g(self, x: T1) -> T2;
175+
}
176+
177+
impl Trait2<i32, i32> for i32 {
178+
// g3
179+
fn g(self, x: i32) -> i32 {
180+
0
181+
}
182+
}
183+
184+
impl Trait2<i32, i64> for i32 {
185+
// g4
186+
fn g(self, x: i32) -> i64 {
187+
0
188+
}
189+
}
190+
191+
fn f() {
192+
let x = 0;
193+
let y = x.f(0i32); // $ target=f1
194+
let z: i32 = x.f(Default::default()); // $ MISSING: target=f1 target=default
195+
let z = x.f(0i64); // $ target=f2
196+
let z: i64 = x.f(Default::default()); // $ MISSING: target=f2 target=default
197+
let z: i64 = x.g(0i32); // $ target=g4 $ SPURIOUS: target=g3
198+
}
199+
}
200+
201+
mod default_type_args {
202+
struct S<T = i64>(T);
203+
204+
trait MyTrait {
205+
type AssocType;
206+
207+
fn g(self) -> Self::AssocType;
208+
}
209+
210+
impl S {
211+
fn f(self) -> i64 {
212+
self.0 // $ fieldof=S
213+
}
214+
215+
fn g(self) -> i64 {
216+
self.0 // $ fieldof=S
217+
}
218+
}
219+
220+
impl S<bool> {
221+
fn g(self) -> bool {
222+
self.0 // $ fieldof=S
223+
}
224+
}
225+
226+
impl<T> MyTrait for S<T> {
227+
type AssocType = S;
228+
229+
fn g(self) -> S {
230+
let x = S::f(S(Default::default())); // $ type=x:i64 $ MISSING: target=f target=default
231+
let x = Self::AssocType::f(S(Default::default())); // $ target=f target=default type=x:i64
232+
let x = S::<bool>::g(S(Default::default())); // $ target=g target=default type=x:bool
233+
let x = S::<i64>::g(S(Default::default())); // $ target=g target=default type=x:i64
234+
let x = Self::AssocType::g(S(Default::default())); // $ target=g target=default type=x:i64
235+
S(0)
236+
}
237+
}
238+
}

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,53 @@ inferCertainType
38453845
| overloading.rs:150:9:150:24 | ...::m(...) | | {EXTERNAL LOCATION} | () |
38463846
| overloading.rs:150:15:150:23 | &... | | {EXTERNAL LOCATION} | & |
38473847
| overloading.rs:150:19:150:22 | true | | {EXTERNAL LOCATION} | bool |
3848+
| overloading.rs:156:14:156:17 | SelfParam | | overloading.rs:155:5:157:5 | Self [trait Trait1] |
3849+
| overloading.rs:156:20:156:20 | x | | overloading.rs:155:18:155:19 | T1 |
3850+
| overloading.rs:161:14:161:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
3851+
| overloading.rs:161:20:161:20 | x | | {EXTERNAL LOCATION} | i32 |
3852+
| overloading.rs:161:35:163:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
3853+
| overloading.rs:168:14:168:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
3854+
| overloading.rs:168:20:168:20 | x | | {EXTERNAL LOCATION} | i64 |
3855+
| overloading.rs:168:35:170:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3856+
| overloading.rs:174:14:174:17 | SelfParam | | overloading.rs:173:5:175:5 | Self [trait Trait2] |
3857+
| overloading.rs:174:20:174:20 | x | | overloading.rs:173:18:173:19 | T1 |
3858+
| overloading.rs:179:14:179:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
3859+
| overloading.rs:179:20:179:20 | x | | {EXTERNAL LOCATION} | i32 |
3860+
| overloading.rs:179:35:181:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
3861+
| overloading.rs:186:14:186:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
3862+
| overloading.rs:186:20:186:20 | x | | {EXTERNAL LOCATION} | i32 |
3863+
| overloading.rs:186:35:188:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3864+
| overloading.rs:191:12:198:5 | { ... } | | {EXTERNAL LOCATION} | () |
3865+
| overloading.rs:193:21:193:24 | 0i32 | | {EXTERNAL LOCATION} | i32 |
3866+
| overloading.rs:194:13:194:13 | z | | {EXTERNAL LOCATION} | i32 |
3867+
| overloading.rs:195:21:195:24 | 0i64 | | {EXTERNAL LOCATION} | i64 |
3868+
| overloading.rs:196:13:196:13 | z | | {EXTERNAL LOCATION} | i64 |
3869+
| overloading.rs:197:13:197:13 | z | | {EXTERNAL LOCATION} | i64 |
3870+
| overloading.rs:197:26:197:29 | 0i32 | | {EXTERNAL LOCATION} | i32 |
3871+
| overloading.rs:207:14:207:17 | SelfParam | | overloading.rs:204:5:208:5 | Self [trait MyTrait] |
3872+
| overloading.rs:211:14:211:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
3873+
| overloading.rs:211:14:211:17 | SelfParam | T | {EXTERNAL LOCATION} | i64 |
3874+
| overloading.rs:211:27:213:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3875+
| overloading.rs:212:13:212:16 | self | | overloading.rs:202:5:202:25 | S |
3876+
| overloading.rs:212:13:212:16 | self | T | {EXTERNAL LOCATION} | i64 |
3877+
| overloading.rs:215:14:215:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
3878+
| overloading.rs:215:14:215:17 | SelfParam | T | {EXTERNAL LOCATION} | i64 |
3879+
| overloading.rs:215:27:217:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
3880+
| overloading.rs:216:13:216:16 | self | | overloading.rs:202:5:202:25 | S |
3881+
| overloading.rs:216:13:216:16 | self | T | {EXTERNAL LOCATION} | i64 |
3882+
| overloading.rs:221:14:221:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
3883+
| overloading.rs:221:14:221:17 | SelfParam | T | {EXTERNAL LOCATION} | bool |
3884+
| overloading.rs:221:28:223:9 | { ... } | | {EXTERNAL LOCATION} | bool |
3885+
| overloading.rs:222:13:222:16 | self | | overloading.rs:202:5:202:25 | S |
3886+
| overloading.rs:222:13:222:16 | self | T | {EXTERNAL LOCATION} | bool |
3887+
| overloading.rs:229:14:229:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
3888+
| overloading.rs:229:14:229:17 | SelfParam | T | overloading.rs:226:10:226:10 | T |
3889+
| overloading.rs:229:25:236:9 | { ... } | | overloading.rs:202:5:202:25 | S |
3890+
| overloading.rs:229:25:236:9 | { ... } | T | {EXTERNAL LOCATION} | i64 |
3891+
| overloading.rs:230:17:230:17 | x | | {EXTERNAL LOCATION} | i64 |
3892+
| overloading.rs:230:21:230:47 | ...::f(...) | | {EXTERNAL LOCATION} | i64 |
3893+
| overloading.rs:231:17:231:17 | x | | {EXTERNAL LOCATION} | i64 |
3894+
| overloading.rs:231:21:231:61 | ...::f(...) | | {EXTERNAL LOCATION} | i64 |
38483895
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
38493896
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
38503897
| pattern_matching.rs:15:5:18:5 | if ... {...} | | {EXTERNAL LOCATION} | () |
@@ -12137,6 +12184,100 @@ inferType
1213712184
| overloading.rs:150:16:150:23 | S5(...) | | overloading.rs:111:5:112:22 | S5 |
1213812185
| overloading.rs:150:16:150:23 | S5(...) | T5 | {EXTERNAL LOCATION} | bool |
1213912186
| overloading.rs:150:19:150:22 | true | | {EXTERNAL LOCATION} | bool |
12187+
| overloading.rs:156:14:156:17 | SelfParam | | overloading.rs:155:5:157:5 | Self [trait Trait1] |
12188+
| overloading.rs:156:20:156:20 | x | | overloading.rs:155:18:155:19 | T1 |
12189+
| overloading.rs:161:14:161:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
12190+
| overloading.rs:161:20:161:20 | x | | {EXTERNAL LOCATION} | i32 |
12191+
| overloading.rs:161:35:163:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12192+
| overloading.rs:162:13:162:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12193+
| overloading.rs:168:14:168:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
12194+
| overloading.rs:168:20:168:20 | x | | {EXTERNAL LOCATION} | i64 |
12195+
| overloading.rs:168:35:170:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12196+
| overloading.rs:169:13:169:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12197+
| overloading.rs:169:13:169:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12198+
| overloading.rs:174:14:174:17 | SelfParam | | overloading.rs:173:5:175:5 | Self [trait Trait2] |
12199+
| overloading.rs:174:20:174:20 | x | | overloading.rs:173:18:173:19 | T1 |
12200+
| overloading.rs:179:14:179:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
12201+
| overloading.rs:179:20:179:20 | x | | {EXTERNAL LOCATION} | i32 |
12202+
| overloading.rs:179:35:181:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12203+
| overloading.rs:180:13:180:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12204+
| overloading.rs:186:14:186:17 | SelfParam | | {EXTERNAL LOCATION} | i32 |
12205+
| overloading.rs:186:20:186:20 | x | | {EXTERNAL LOCATION} | i32 |
12206+
| overloading.rs:186:35:188:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12207+
| overloading.rs:187:13:187:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12208+
| overloading.rs:187:13:187:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12209+
| overloading.rs:191:12:198:5 | { ... } | | {EXTERNAL LOCATION} | () |
12210+
| overloading.rs:192:13:192:13 | x | | {EXTERNAL LOCATION} | i32 |
12211+
| overloading.rs:192:17:192:17 | 0 | | {EXTERNAL LOCATION} | i32 |
12212+
| overloading.rs:193:13:193:13 | y | | {EXTERNAL LOCATION} | i32 |
12213+
| overloading.rs:193:17:193:17 | x | | {EXTERNAL LOCATION} | i32 |
12214+
| overloading.rs:193:17:193:25 | x.f(...) | | {EXTERNAL LOCATION} | i32 |
12215+
| overloading.rs:193:21:193:24 | 0i32 | | {EXTERNAL LOCATION} | i32 |
12216+
| overloading.rs:194:13:194:13 | z | | {EXTERNAL LOCATION} | i32 |
12217+
| overloading.rs:194:22:194:22 | x | | {EXTERNAL LOCATION} | i32 |
12218+
| overloading.rs:194:22:194:44 | x.f(...) | | {EXTERNAL LOCATION} | i32 |
12219+
| overloading.rs:195:13:195:13 | z | | {EXTERNAL LOCATION} | i64 |
12220+
| overloading.rs:195:17:195:17 | x | | {EXTERNAL LOCATION} | i32 |
12221+
| overloading.rs:195:17:195:25 | x.f(...) | | {EXTERNAL LOCATION} | i64 |
12222+
| overloading.rs:195:21:195:24 | 0i64 | | {EXTERNAL LOCATION} | i64 |
12223+
| overloading.rs:196:13:196:13 | z | | {EXTERNAL LOCATION} | i64 |
12224+
| overloading.rs:196:22:196:22 | x | | {EXTERNAL LOCATION} | i32 |
12225+
| overloading.rs:196:22:196:44 | x.f(...) | | {EXTERNAL LOCATION} | i64 |
12226+
| overloading.rs:197:13:197:13 | z | | {EXTERNAL LOCATION} | i64 |
12227+
| overloading.rs:197:22:197:22 | x | | {EXTERNAL LOCATION} | i32 |
12228+
| overloading.rs:197:22:197:30 | x.g(...) | | {EXTERNAL LOCATION} | i32 |
12229+
| overloading.rs:197:22:197:30 | x.g(...) | | {EXTERNAL LOCATION} | i64 |
12230+
| overloading.rs:197:26:197:29 | 0i32 | | {EXTERNAL LOCATION} | i32 |
12231+
| overloading.rs:207:14:207:17 | SelfParam | | overloading.rs:204:5:208:5 | Self [trait MyTrait] |
12232+
| overloading.rs:211:14:211:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
12233+
| overloading.rs:211:14:211:17 | SelfParam | T | {EXTERNAL LOCATION} | i64 |
12234+
| overloading.rs:211:27:213:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12235+
| overloading.rs:212:13:212:16 | self | | overloading.rs:202:5:202:25 | S |
12236+
| overloading.rs:212:13:212:16 | self | T | {EXTERNAL LOCATION} | i64 |
12237+
| overloading.rs:212:13:212:18 | self.0 | | {EXTERNAL LOCATION} | i64 |
12238+
| overloading.rs:215:14:215:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
12239+
| overloading.rs:215:14:215:17 | SelfParam | T | {EXTERNAL LOCATION} | i64 |
12240+
| overloading.rs:215:27:217:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12241+
| overloading.rs:216:13:216:16 | self | | overloading.rs:202:5:202:25 | S |
12242+
| overloading.rs:216:13:216:16 | self | T | {EXTERNAL LOCATION} | i64 |
12243+
| overloading.rs:216:13:216:18 | self.0 | | {EXTERNAL LOCATION} | i64 |
12244+
| overloading.rs:221:14:221:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
12245+
| overloading.rs:221:14:221:17 | SelfParam | T | {EXTERNAL LOCATION} | bool |
12246+
| overloading.rs:221:28:223:9 | { ... } | | {EXTERNAL LOCATION} | bool |
12247+
| overloading.rs:222:13:222:16 | self | | overloading.rs:202:5:202:25 | S |
12248+
| overloading.rs:222:13:222:16 | self | T | {EXTERNAL LOCATION} | bool |
12249+
| overloading.rs:222:13:222:18 | self.0 | | {EXTERNAL LOCATION} | bool |
12250+
| overloading.rs:229:14:229:17 | SelfParam | | overloading.rs:202:5:202:25 | S |
12251+
| overloading.rs:229:14:229:17 | SelfParam | T | overloading.rs:226:10:226:10 | T |
12252+
| overloading.rs:229:25:236:9 | { ... } | | overloading.rs:202:5:202:25 | S |
12253+
| overloading.rs:229:25:236:9 | { ... } | T | {EXTERNAL LOCATION} | i64 |
12254+
| overloading.rs:230:17:230:17 | x | | {EXTERNAL LOCATION} | i64 |
12255+
| overloading.rs:230:21:230:47 | ...::f(...) | | {EXTERNAL LOCATION} | i64 |
12256+
| overloading.rs:230:26:230:46 | S(...) | | overloading.rs:202:5:202:25 | S |
12257+
| overloading.rs:231:17:231:17 | x | | {EXTERNAL LOCATION} | i64 |
12258+
| overloading.rs:231:21:231:61 | ...::f(...) | | {EXTERNAL LOCATION} | i64 |
12259+
| overloading.rs:231:40:231:60 | S(...) | | overloading.rs:202:5:202:25 | S |
12260+
| overloading.rs:231:40:231:60 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12261+
| overloading.rs:231:42:231:59 | ...::default(...) | | {EXTERNAL LOCATION} | i64 |
12262+
| overloading.rs:232:17:232:17 | x | | {EXTERNAL LOCATION} | bool |
12263+
| overloading.rs:232:21:232:55 | ...::g(...) | | {EXTERNAL LOCATION} | bool |
12264+
| overloading.rs:232:34:232:54 | S(...) | | overloading.rs:202:5:202:25 | S |
12265+
| overloading.rs:232:34:232:54 | S(...) | T | {EXTERNAL LOCATION} | bool |
12266+
| overloading.rs:232:36:232:53 | ...::default(...) | | {EXTERNAL LOCATION} | bool |
12267+
| overloading.rs:233:17:233:17 | x | | {EXTERNAL LOCATION} | i64 |
12268+
| overloading.rs:233:21:233:54 | ...::g(...) | | {EXTERNAL LOCATION} | i64 |
12269+
| overloading.rs:233:33:233:53 | S(...) | | overloading.rs:202:5:202:25 | S |
12270+
| overloading.rs:233:33:233:53 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12271+
| overloading.rs:233:35:233:52 | ...::default(...) | | {EXTERNAL LOCATION} | i64 |
12272+
| overloading.rs:234:17:234:17 | x | | {EXTERNAL LOCATION} | i64 |
12273+
| overloading.rs:234:21:234:61 | ...::g(...) | | {EXTERNAL LOCATION} | i64 |
12274+
| overloading.rs:234:40:234:60 | S(...) | | overloading.rs:202:5:202:25 | S |
12275+
| overloading.rs:234:40:234:60 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12276+
| overloading.rs:234:42:234:59 | ...::default(...) | | {EXTERNAL LOCATION} | i64 |
12277+
| overloading.rs:235:13:235:16 | S(...) | | overloading.rs:202:5:202:25 | S |
12278+
| overloading.rs:235:13:235:16 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12279+
| overloading.rs:235:13:235:16 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12280+
| overloading.rs:235:15:235:15 | 0 | | {EXTERNAL LOCATION} | i32 |
1214012281
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
1214112282
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
1214212283
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |

0 commit comments

Comments
 (0)