From 6a2502c97a994cbb5255406310c6f599c8f3a657 Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Tue, 2 Dec 2025 13:00:00 +0100 Subject: [PATCH] Rust: Add type inference tests for raw pointers --- .../type-inference/raw_pointer.rs | 60 ++++++++++++++ .../type-inference/type-inference.expected | 80 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 rust/ql/test/library-tests/type-inference/raw_pointer.rs diff --git a/rust/ql/test/library-tests/type-inference/raw_pointer.rs b/rust/ql/test/library-tests/type-inference/raw_pointer.rs new file mode 100644 index 00000000000..4ae863632d9 --- /dev/null +++ b/rust/ql/test/library-tests/type-inference/raw_pointer.rs @@ -0,0 +1,60 @@ +use std::ptr::null_mut; + +fn raw_pointer_const_deref(x: *const i32) -> i32 { + let _y = unsafe { *x }; // $ MISSING: type=_y:i32 + 0 +} + +fn raw_pointer_mut_deref(x: *mut bool) -> i32 { + let _y = unsafe { *x }; // $ MISSING: type=_y:bool + 0 +} + +fn raw_const_borrow() { + let a: i64 = 10; + let x = &raw const a; // $ MISSING: type=x:TPtrConst.i64 + unsafe { + let _y = *x; // $ type=_y:i64 SPURIOUS: target=deref + } +} + +fn raw_mut_borrow() { + let mut a = 10i32; + let x = &raw mut a; // $ MISSING: type=x:TPtrMut.i32 + unsafe { + let _y = *x; // $ type=_y:i32 SPURIOUS: target=deref + } +} + +fn raw_mut_write(cond: bool) { + let a = 10i32; + // The type of `x` must be inferred from the write below. + let ptr_written = null_mut(); // $ target=null_mut MISSING: type=ptr_written:TPtrMut.i32 + if cond { + unsafe { + // NOTE: This write is undefined behavior because `x` is a null pointer. + *ptr_written = a; + let _y = *ptr_written; // $ MISSING: type=_y:i32 + } + } +} + +fn raw_type_from_deref(cond: bool) { + // The type of `x` must be inferred from the read below. + let ptr_read = null_mut(); // $ target=null_mut MISSING: type=ptr_read:TPtrMut.i64 + if cond { + unsafe { + // NOTE: This read is undefined behavior because `x` is a null pointer. + let _y: i64 = *ptr_read; + } + } +} + +pub fn test() { + raw_pointer_const_deref(&10); // $ target=raw_pointer_const_deref + raw_pointer_mut_deref(&mut true); // $ target=raw_pointer_mut_deref + raw_const_borrow(); // $ target=raw_const_borrow + raw_mut_borrow(); // $ target=raw_mut_borrow + raw_mut_write(false); // $ target=raw_mut_write + raw_type_from_deref(false); // $ target=raw_type_from_deref +} 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 e6845968a7b..e056fdd373b 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -8333,4 +8333,84 @@ inferType | pattern_matching.rs:843:5:843:32 | patterns_in_let_statements(...) | | {EXTERNAL LOCATION} | () | | pattern_matching.rs:844:5:844:37 | patterns_in_function_parameters(...) | | {EXTERNAL LOCATION} | () | | pattern_matching.rs:845:5:845:30 | patterns_in_control_flow(...) | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:3:28:3:28 | x | | file://:0:0:0:0 | * | +| raw_pointer.rs:3:28:3:28 | x | TPtr | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:3:50:6:1 | { ... } | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:4:24:4:24 | x | | file://:0:0:0:0 | * | +| raw_pointer.rs:4:24:4:24 | x | TPtr | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:5:5:5:5 | 0 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:8:26:8:26 | x | | file://:0:0:0:0 | * | +| raw_pointer.rs:8:26:8:26 | x | TPtr | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:8:47:11:1 | { ... } | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:9:24:9:24 | x | | file://:0:0:0:0 | * | +| raw_pointer.rs:9:24:9:24 | x | TPtr | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:10:5:10:5 | 0 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:13:23:19:1 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:14:9:14:9 | a | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:14:18:14:19 | 10 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:14:18:14:19 | 10 | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:15:9:15:9 | x | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:15:9:15:9 | x | TRef | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:15:13:15:24 | &raw const a | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:15:13:15:24 | &raw const a | TRef | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:15:24:15:24 | a | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:16:5:18:5 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:17:13:17:14 | _y | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:17:18:17:19 | * ... | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:17:19:17:19 | x | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:17:19:17:19 | x | TRef | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:21:21:27:1 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:22:13:22:13 | a | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:22:17:22:21 | 10i32 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:23:9:23:9 | x | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:23:9:23:9 | x | TRef | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:23:13:23:22 | &raw mut a | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:23:13:23:22 | &raw mut a | TRef | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:23:22:23:22 | a | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:24:5:26:5 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:25:13:25:14 | _y | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:25:18:25:19 | * ... | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:25:19:25:19 | x | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:25:19:25:19 | x | TRef | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:29:18:29:21 | cond | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:29:30:40:1 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:30:9:30:9 | a | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:30:13:30:17 | 10i32 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:32:9:32:19 | ptr_written | | file://:0:0:0:0 | * | +| raw_pointer.rs:32:23:32:32 | null_mut(...) | | file://:0:0:0:0 | * | +| raw_pointer.rs:33:5:39:5 | if cond {...} | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:33:8:33:11 | cond | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:33:13:39:5 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:34:9:38:9 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:36:13:36:24 | * ... | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:36:13:36:28 | ... = ... | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:36:14:36:24 | ptr_written | | file://:0:0:0:0 | * | +| raw_pointer.rs:36:28:36:28 | a | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:37:23:37:33 | ptr_written | | file://:0:0:0:0 | * | +| raw_pointer.rs:42:24:42:27 | cond | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:42:36:51:1 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:44:9:44:16 | ptr_read | | file://:0:0:0:0 | * | +| raw_pointer.rs:44:20:44:29 | null_mut(...) | | file://:0:0:0:0 | * | +| raw_pointer.rs:45:5:50:5 | if cond {...} | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:45:8:45:11 | cond | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:45:13:50:5 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:46:9:49:9 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:48:17:48:18 | _y | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:48:27:48:35 | * ... | | {EXTERNAL LOCATION} | i64 | +| raw_pointer.rs:48:28:48:35 | ptr_read | | file://:0:0:0:0 | * | +| raw_pointer.rs:53:15:60:1 | { ... } | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:54:5:54:32 | raw_pointer_const_deref(...) | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:54:29:54:31 | &10 | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:54:29:54:31 | &10 | TRef | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:54:30:54:31 | 10 | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:55:5:55:36 | raw_pointer_mut_deref(...) | | {EXTERNAL LOCATION} | i32 | +| raw_pointer.rs:55:27:55:35 | &mut true | | {EXTERNAL LOCATION} | & | +| raw_pointer.rs:55:27:55:35 | &mut true | TRef | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:55:32:55:35 | true | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:56:5:56:22 | raw_const_borrow(...) | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:57:5:57:20 | raw_mut_borrow(...) | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:58:5:58:24 | raw_mut_write(...) | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:58:19:58:23 | false | | {EXTERNAL LOCATION} | bool | +| raw_pointer.rs:59:5:59:30 | raw_type_from_deref(...) | | {EXTERNAL LOCATION} | () | +| raw_pointer.rs:59:25:59:29 | false | | {EXTERNAL LOCATION} | bool | testFailures