Skip to content

Commit 4eea443

Browse files
authored
Merge pull request #20222 from geoffw0/pathbuf
Rust: Add a type inference test case resembling PathBuf.canonicalize.
2 parents 877d397 + 1c186e2 commit 4eea443

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ pub mod pattern_matching_experimental {
25192519
}
25202520

25212521
pub mod exec {
2522-
// a *greatly* simplified model of `MySqlConnection.execute` in SQLX
2522+
// a highly simplified model of `MySqlConnection.execute` in SQLx
25232523

25242524
trait Connection {}
25252525

@@ -2555,6 +2555,54 @@ pub mod exec {
25552555
}
25562556
}
25572557

2558+
pub mod path_buf {
2559+
// a highly simplified model of `PathBuf::canonicalize`
2560+
2561+
pub struct Path {
2562+
}
2563+
2564+
impl Path {
2565+
pub const fn new() -> Path {
2566+
Path { }
2567+
}
2568+
2569+
pub fn canonicalize(&self) -> Result<PathBuf, ()> {
2570+
Ok(PathBuf::new()) // $ target=new
2571+
}
2572+
}
2573+
2574+
pub struct PathBuf {
2575+
}
2576+
2577+
impl PathBuf {
2578+
pub const fn new() -> PathBuf {
2579+
PathBuf { }
2580+
}
2581+
}
2582+
2583+
// `PathBuf` provides `canonicalize` via `Deref`:
2584+
impl std::ops::Deref for PathBuf {
2585+
type Target = Path;
2586+
2587+
#[inline]
2588+
fn deref(&self) -> &Path {
2589+
// (very much not a real implementation)
2590+
static path : Path = Path::new(); // $ target=new
2591+
&path
2592+
}
2593+
}
2594+
2595+
pub fn f() {
2596+
let path1 = Path::new(); // $ target=new type=path1:Path
2597+
let path2 = path1.canonicalize(); // $ target=canonicalize
2598+
let path3 = path2.unwrap(); // $ target=unwrap type=path3:PathBuf
2599+
2600+
let pathbuf1 = PathBuf::new(); // $ target=new type=pathbuf1:PathBuf
2601+
let pathbuf2 = pathbuf1.canonicalize(); // $ MISSING: target=canonicalize
2602+
let pathbuf3 = pathbuf2.unwrap(); // $ MISSING: target=unwrap type=pathbuf3:PathBuf
2603+
}
2604+
}
2605+
25582606
mod closure;
25592607
mod dereference;
25602608
mod dyn_type;
@@ -2587,6 +2635,7 @@ fn main() {
25872635
method_determined_by_argument_type::f(); // $ target=f
25882636
tuples::f(); // $ target=f
25892637
exec::f(); // $ target=f
2638+
path_buf::f(); // $ target=f
25902639
dereference::test(); // $ target=test
25912640
pattern_matching::test_all_patterns(); // $ target=test_all_patterns
25922641
pattern_matching_experimental::box_patterns(); // $ target=box_patterns

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,11 +4950,49 @@ inferType
49504950
| main.rs:2554:44:2554:44 | c | | main.rs:2541:5:2541:29 | MySqlConnection |
49514951
| main.rs:2554:47:2554:67 | "SELECT * FROM users" | | file://:0:0:0:0 | & |
49524952
| main.rs:2554:47:2554:67 | "SELECT * FROM users" | &T | {EXTERNAL LOCATION} | str |
4953-
| main.rs:2564:5:2564:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
4954-
| main.rs:2565:5:2565:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
4955-
| main.rs:2565:20:2565:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4956-
| main.rs:2565:41:2565:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4957-
| main.rs:2581:5:2581:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
4953+
| main.rs:2565:36:2567:9 | { ... } | | main.rs:2561:5:2562:5 | Path |
4954+
| main.rs:2566:13:2566:20 | Path {...} | | main.rs:2561:5:2562:5 | Path |
4955+
| main.rs:2569:29:2569:33 | SelfParam | | file://:0:0:0:0 | & |
4956+
| main.rs:2569:29:2569:33 | SelfParam | &T | main.rs:2561:5:2562:5 | Path |
4957+
| main.rs:2569:59:2571:9 | { ... } | | {EXTERNAL LOCATION} | Result |
4958+
| main.rs:2569:59:2571:9 | { ... } | E | file://:0:0:0:0 | () |
4959+
| main.rs:2569:59:2571:9 | { ... } | T | main.rs:2574:5:2575:5 | PathBuf |
4960+
| main.rs:2570:13:2570:30 | Ok(...) | | {EXTERNAL LOCATION} | Result |
4961+
| main.rs:2570:13:2570:30 | Ok(...) | E | file://:0:0:0:0 | () |
4962+
| main.rs:2570:13:2570:30 | Ok(...) | T | main.rs:2574:5:2575:5 | PathBuf |
4963+
| main.rs:2570:16:2570:29 | ...::new(...) | | main.rs:2574:5:2575:5 | PathBuf |
4964+
| main.rs:2578:39:2580:9 | { ... } | | main.rs:2574:5:2575:5 | PathBuf |
4965+
| main.rs:2579:13:2579:23 | PathBuf {...} | | main.rs:2574:5:2575:5 | PathBuf |
4966+
| main.rs:2588:18:2588:22 | SelfParam | | file://:0:0:0:0 | & |
4967+
| main.rs:2588:18:2588:22 | SelfParam | &T | main.rs:2574:5:2575:5 | PathBuf |
4968+
| main.rs:2588:34:2592:9 | { ... } | | file://:0:0:0:0 | & |
4969+
| main.rs:2588:34:2592:9 | { ... } | &T | main.rs:2561:5:2562:5 | Path |
4970+
| main.rs:2590:34:2590:44 | ...::new(...) | | main.rs:2561:5:2562:5 | Path |
4971+
| main.rs:2591:13:2591:17 | &path | | file://:0:0:0:0 | & |
4972+
| main.rs:2591:13:2591:17 | &path | &T | main.rs:2561:5:2562:5 | Path |
4973+
| main.rs:2591:14:2591:17 | path | | main.rs:2561:5:2562:5 | Path |
4974+
| main.rs:2596:13:2596:17 | path1 | | main.rs:2561:5:2562:5 | Path |
4975+
| main.rs:2596:21:2596:31 | ...::new(...) | | main.rs:2561:5:2562:5 | Path |
4976+
| main.rs:2597:13:2597:17 | path2 | | {EXTERNAL LOCATION} | Result |
4977+
| main.rs:2597:13:2597:17 | path2 | E | file://:0:0:0:0 | () |
4978+
| main.rs:2597:13:2597:17 | path2 | T | main.rs:2574:5:2575:5 | PathBuf |
4979+
| main.rs:2597:21:2597:25 | path1 | | main.rs:2561:5:2562:5 | Path |
4980+
| main.rs:2597:21:2597:40 | path1.canonicalize() | | {EXTERNAL LOCATION} | Result |
4981+
| main.rs:2597:21:2597:40 | path1.canonicalize() | E | file://:0:0:0:0 | () |
4982+
| main.rs:2597:21:2597:40 | path1.canonicalize() | T | main.rs:2574:5:2575:5 | PathBuf |
4983+
| main.rs:2598:13:2598:17 | path3 | | main.rs:2574:5:2575:5 | PathBuf |
4984+
| main.rs:2598:21:2598:25 | path2 | | {EXTERNAL LOCATION} | Result |
4985+
| main.rs:2598:21:2598:25 | path2 | E | file://:0:0:0:0 | () |
4986+
| main.rs:2598:21:2598:25 | path2 | T | main.rs:2574:5:2575:5 | PathBuf |
4987+
| main.rs:2598:21:2598:34 | path2.unwrap() | | main.rs:2574:5:2575:5 | PathBuf |
4988+
| main.rs:2600:13:2600:20 | pathbuf1 | | main.rs:2574:5:2575:5 | PathBuf |
4989+
| main.rs:2600:24:2600:37 | ...::new(...) | | main.rs:2574:5:2575:5 | PathBuf |
4990+
| main.rs:2601:24:2601:31 | pathbuf1 | | main.rs:2574:5:2575:5 | PathBuf |
4991+
| main.rs:2612:5:2612:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
4992+
| main.rs:2613:5:2613:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
4993+
| main.rs:2613:20:2613:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4994+
| main.rs:2613:41:2613:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4995+
| main.rs:2629:5:2629:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
49584996
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
49594997
| pattern_matching.rs:13:26:133:1 | { ... } | T | file://:0:0:0:0 | () |
49604998
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |

0 commit comments

Comments
 (0)