Skip to content

Rust: Add a type inference test case resembling PathBuf.canonicalize. #20222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ pub mod pattern_matching_experimental {
}

pub mod exec {
// a *greatly* simplified model of `MySqlConnection.execute` in SQLX
// a highly simplified model of `MySqlConnection.execute` in SQLx

trait Connection {}

Expand Down Expand Up @@ -2555,6 +2555,54 @@ pub mod exec {
}
}

pub mod path_buf {
// a highly simplified model of `PathBuf::canonicalize`

pub struct Path {
}

impl Path {
pub const fn new() -> Path {
Path { }
}

pub fn canonicalize(&self) -> Result<PathBuf, ()> {
Ok(PathBuf::new()) // $ target=new
}
}

pub struct PathBuf {
}

impl PathBuf {
pub const fn new() -> PathBuf {
PathBuf { }
}
}

// `PathBuf` provides `canonicalize` via `Deref`:
impl std::ops::Deref for PathBuf {
type Target = Path;

#[inline]
fn deref(&self) -> &Path {
// (very much not a real implementation)
static path : Path = Path::new(); // $ target=new
&path
}
}

pub fn f() {
let path1 = Path::new(); // $ target=new type=path1:Path
let path2 = path1.canonicalize(); // $ target=canonicalize
let path3 = path2.unwrap(); // $ target=unwrap type=path3:PathBuf

let pathbuf1 = PathBuf::new(); // $ target=new type=pathbuf1:PathBuf
let pathbuf2 = pathbuf1.canonicalize(); // $ MISSING: target=canonicalize
let pathbuf3 = pathbuf2.unwrap(); // $ MISSING: target=unwrap type=pathbuf3:PathBuf
}
}

mod closure;
mod dereference;
mod dyn_type;
Expand Down Expand Up @@ -2587,6 +2635,7 @@ fn main() {
method_determined_by_argument_type::f(); // $ target=f
tuples::f(); // $ target=f
exec::f(); // $ target=f
path_buf::f(); // $ target=f
dereference::test(); // $ target=test
pattern_matching::test_all_patterns(); // $ target=test_all_patterns
pattern_matching_experimental::box_patterns(); // $ target=box_patterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4950,11 +4950,49 @@ inferType
| main.rs:2554:44:2554:44 | c | | main.rs:2541:5:2541:29 | MySqlConnection |
| main.rs:2554:47:2554:67 | "SELECT * FROM users" | | file://:0:0:0:0 | & |
| main.rs:2554:47:2554:67 | "SELECT * FROM users" | &T | {EXTERNAL LOCATION} | str |
| main.rs:2564:5:2564:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
| main.rs:2565:5:2565:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
| main.rs:2565:20:2565:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
| main.rs:2565:41:2565:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
| main.rs:2581:5:2581:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
| main.rs:2565:36:2567:9 | { ... } | | main.rs:2561:5:2562:5 | Path |
| main.rs:2566:13:2566:20 | Path {...} | | main.rs:2561:5:2562:5 | Path |
| main.rs:2569:29:2569:33 | SelfParam | | file://:0:0:0:0 | & |
| main.rs:2569:29:2569:33 | SelfParam | &T | main.rs:2561:5:2562:5 | Path |
| main.rs:2569:59:2571:9 | { ... } | | {EXTERNAL LOCATION} | Result |
| main.rs:2569:59:2571:9 | { ... } | E | file://:0:0:0:0 | () |
| main.rs:2569:59:2571:9 | { ... } | T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2570:13:2570:30 | Ok(...) | | {EXTERNAL LOCATION} | Result |
| main.rs:2570:13:2570:30 | Ok(...) | E | file://:0:0:0:0 | () |
| main.rs:2570:13:2570:30 | Ok(...) | T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2570:16:2570:29 | ...::new(...) | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2578:39:2580:9 | { ... } | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2579:13:2579:23 | PathBuf {...} | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2588:18:2588:22 | SelfParam | | file://:0:0:0:0 | & |
| main.rs:2588:18:2588:22 | SelfParam | &T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2588:34:2592:9 | { ... } | | file://:0:0:0:0 | & |
| main.rs:2588:34:2592:9 | { ... } | &T | main.rs:2561:5:2562:5 | Path |
| main.rs:2590:34:2590:44 | ...::new(...) | | main.rs:2561:5:2562:5 | Path |
| main.rs:2591:13:2591:17 | &path | | file://:0:0:0:0 | & |
| main.rs:2591:13:2591:17 | &path | &T | main.rs:2561:5:2562:5 | Path |
| main.rs:2591:14:2591:17 | path | | main.rs:2561:5:2562:5 | Path |
| main.rs:2596:13:2596:17 | path1 | | main.rs:2561:5:2562:5 | Path |
| main.rs:2596:21:2596:31 | ...::new(...) | | main.rs:2561:5:2562:5 | Path |
| main.rs:2597:13:2597:17 | path2 | | {EXTERNAL LOCATION} | Result |
| main.rs:2597:13:2597:17 | path2 | E | file://:0:0:0:0 | () |
| main.rs:2597:13:2597:17 | path2 | T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2597:21:2597:25 | path1 | | main.rs:2561:5:2562:5 | Path |
| main.rs:2597:21:2597:40 | path1.canonicalize() | | {EXTERNAL LOCATION} | Result |
| main.rs:2597:21:2597:40 | path1.canonicalize() | E | file://:0:0:0:0 | () |
| main.rs:2597:21:2597:40 | path1.canonicalize() | T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2598:13:2598:17 | path3 | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2598:21:2598:25 | path2 | | {EXTERNAL LOCATION} | Result |
| main.rs:2598:21:2598:25 | path2 | E | file://:0:0:0:0 | () |
| main.rs:2598:21:2598:25 | path2 | T | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2598:21:2598:34 | path2.unwrap() | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2600:13:2600:20 | pathbuf1 | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2600:24:2600:37 | ...::new(...) | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2601:24:2601:31 | pathbuf1 | | main.rs:2574:5:2575:5 | PathBuf |
| main.rs:2612:5:2612:20 | ...::f(...) | | main.rs:72:5:72:21 | Foo |
| main.rs:2613:5:2613:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
| main.rs:2613:20:2613:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
| main.rs:2613:41:2613:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
| main.rs:2629:5:2629:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
| pattern_matching.rs:13:26:133:1 | { ... } | T | file://:0:0:0:0 | () |
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |
Expand Down