Skip to content

Commit 6a2502c

Browse files
committed
Rust: Add type inference tests for raw pointers
1 parent 848677e commit 6a2502c

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::ptr::null_mut;
2+
3+
fn raw_pointer_const_deref(x: *const i32) -> i32 {
4+
let _y = unsafe { *x }; // $ MISSING: type=_y:i32
5+
0
6+
}
7+
8+
fn raw_pointer_mut_deref(x: *mut bool) -> i32 {
9+
let _y = unsafe { *x }; // $ MISSING: type=_y:bool
10+
0
11+
}
12+
13+
fn raw_const_borrow() {
14+
let a: i64 = 10;
15+
let x = &raw const a; // $ MISSING: type=x:TPtrConst.i64
16+
unsafe {
17+
let _y = *x; // $ type=_y:i64 SPURIOUS: target=deref
18+
}
19+
}
20+
21+
fn raw_mut_borrow() {
22+
let mut a = 10i32;
23+
let x = &raw mut a; // $ MISSING: type=x:TPtrMut.i32
24+
unsafe {
25+
let _y = *x; // $ type=_y:i32 SPURIOUS: target=deref
26+
}
27+
}
28+
29+
fn raw_mut_write(cond: bool) {
30+
let a = 10i32;
31+
// The type of `x` must be inferred from the write below.
32+
let ptr_written = null_mut(); // $ target=null_mut MISSING: type=ptr_written:TPtrMut.i32
33+
if cond {
34+
unsafe {
35+
// NOTE: This write is undefined behavior because `x` is a null pointer.
36+
*ptr_written = a;
37+
let _y = *ptr_written; // $ MISSING: type=_y:i32
38+
}
39+
}
40+
}
41+
42+
fn raw_type_from_deref(cond: bool) {
43+
// The type of `x` must be inferred from the read below.
44+
let ptr_read = null_mut(); // $ target=null_mut MISSING: type=ptr_read:TPtrMut.i64
45+
if cond {
46+
unsafe {
47+
// NOTE: This read is undefined behavior because `x` is a null pointer.
48+
let _y: i64 = *ptr_read;
49+
}
50+
}
51+
}
52+
53+
pub fn test() {
54+
raw_pointer_const_deref(&10); // $ target=raw_pointer_const_deref
55+
raw_pointer_mut_deref(&mut true); // $ target=raw_pointer_mut_deref
56+
raw_const_borrow(); // $ target=raw_const_borrow
57+
raw_mut_borrow(); // $ target=raw_mut_borrow
58+
raw_mut_write(false); // $ target=raw_mut_write
59+
raw_type_from_deref(false); // $ target=raw_type_from_deref
60+
}

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8333,4 +8333,84 @@ inferType
83338333
| pattern_matching.rs:843:5:843:32 | patterns_in_let_statements(...) | | {EXTERNAL LOCATION} | () |
83348334
| pattern_matching.rs:844:5:844:37 | patterns_in_function_parameters(...) | | {EXTERNAL LOCATION} | () |
83358335
| pattern_matching.rs:845:5:845:30 | patterns_in_control_flow(...) | | {EXTERNAL LOCATION} | () |
8336+
| raw_pointer.rs:3:28:3:28 | x | | file://:0:0:0:0 | * |
8337+
| raw_pointer.rs:3:28:3:28 | x | TPtr | {EXTERNAL LOCATION} | i32 |
8338+
| raw_pointer.rs:3:50:6:1 | { ... } | | {EXTERNAL LOCATION} | i32 |
8339+
| raw_pointer.rs:4:24:4:24 | x | | file://:0:0:0:0 | * |
8340+
| raw_pointer.rs:4:24:4:24 | x | TPtr | {EXTERNAL LOCATION} | i32 |
8341+
| raw_pointer.rs:5:5:5:5 | 0 | | {EXTERNAL LOCATION} | i32 |
8342+
| raw_pointer.rs:8:26:8:26 | x | | file://:0:0:0:0 | * |
8343+
| raw_pointer.rs:8:26:8:26 | x | TPtr | {EXTERNAL LOCATION} | bool |
8344+
| raw_pointer.rs:8:47:11:1 | { ... } | | {EXTERNAL LOCATION} | i32 |
8345+
| raw_pointer.rs:9:24:9:24 | x | | file://:0:0:0:0 | * |
8346+
| raw_pointer.rs:9:24:9:24 | x | TPtr | {EXTERNAL LOCATION} | bool |
8347+
| raw_pointer.rs:10:5:10:5 | 0 | | {EXTERNAL LOCATION} | i32 |
8348+
| raw_pointer.rs:13:23:19:1 | { ... } | | {EXTERNAL LOCATION} | () |
8349+
| raw_pointer.rs:14:9:14:9 | a | | {EXTERNAL LOCATION} | i64 |
8350+
| raw_pointer.rs:14:18:14:19 | 10 | | {EXTERNAL LOCATION} | i32 |
8351+
| raw_pointer.rs:14:18:14:19 | 10 | | {EXTERNAL LOCATION} | i64 |
8352+
| raw_pointer.rs:15:9:15:9 | x | | {EXTERNAL LOCATION} | & |
8353+
| raw_pointer.rs:15:9:15:9 | x | TRef | {EXTERNAL LOCATION} | i64 |
8354+
| raw_pointer.rs:15:13:15:24 | &raw const a | | {EXTERNAL LOCATION} | & |
8355+
| raw_pointer.rs:15:13:15:24 | &raw const a | TRef | {EXTERNAL LOCATION} | i64 |
8356+
| raw_pointer.rs:15:24:15:24 | a | | {EXTERNAL LOCATION} | i64 |
8357+
| raw_pointer.rs:16:5:18:5 | { ... } | | {EXTERNAL LOCATION} | () |
8358+
| raw_pointer.rs:17:13:17:14 | _y | | {EXTERNAL LOCATION} | i64 |
8359+
| raw_pointer.rs:17:18:17:19 | * ... | | {EXTERNAL LOCATION} | i64 |
8360+
| raw_pointer.rs:17:19:17:19 | x | | {EXTERNAL LOCATION} | & |
8361+
| raw_pointer.rs:17:19:17:19 | x | TRef | {EXTERNAL LOCATION} | i64 |
8362+
| raw_pointer.rs:21:21:27:1 | { ... } | | {EXTERNAL LOCATION} | () |
8363+
| raw_pointer.rs:22:13:22:13 | a | | {EXTERNAL LOCATION} | i32 |
8364+
| raw_pointer.rs:22:17:22:21 | 10i32 | | {EXTERNAL LOCATION} | i32 |
8365+
| raw_pointer.rs:23:9:23:9 | x | | {EXTERNAL LOCATION} | & |
8366+
| raw_pointer.rs:23:9:23:9 | x | TRef | {EXTERNAL LOCATION} | i32 |
8367+
| raw_pointer.rs:23:13:23:22 | &raw mut a | | {EXTERNAL LOCATION} | & |
8368+
| raw_pointer.rs:23:13:23:22 | &raw mut a | TRef | {EXTERNAL LOCATION} | i32 |
8369+
| raw_pointer.rs:23:22:23:22 | a | | {EXTERNAL LOCATION} | i32 |
8370+
| raw_pointer.rs:24:5:26:5 | { ... } | | {EXTERNAL LOCATION} | () |
8371+
| raw_pointer.rs:25:13:25:14 | _y | | {EXTERNAL LOCATION} | i32 |
8372+
| raw_pointer.rs:25:18:25:19 | * ... | | {EXTERNAL LOCATION} | i32 |
8373+
| raw_pointer.rs:25:19:25:19 | x | | {EXTERNAL LOCATION} | & |
8374+
| raw_pointer.rs:25:19:25:19 | x | TRef | {EXTERNAL LOCATION} | i32 |
8375+
| raw_pointer.rs:29:18:29:21 | cond | | {EXTERNAL LOCATION} | bool |
8376+
| raw_pointer.rs:29:30:40:1 | { ... } | | {EXTERNAL LOCATION} | () |
8377+
| raw_pointer.rs:30:9:30:9 | a | | {EXTERNAL LOCATION} | i32 |
8378+
| raw_pointer.rs:30:13:30:17 | 10i32 | | {EXTERNAL LOCATION} | i32 |
8379+
| raw_pointer.rs:32:9:32:19 | ptr_written | | file://:0:0:0:0 | * |
8380+
| raw_pointer.rs:32:23:32:32 | null_mut(...) | | file://:0:0:0:0 | * |
8381+
| raw_pointer.rs:33:5:39:5 | if cond {...} | | {EXTERNAL LOCATION} | () |
8382+
| raw_pointer.rs:33:8:33:11 | cond | | {EXTERNAL LOCATION} | bool |
8383+
| raw_pointer.rs:33:13:39:5 | { ... } | | {EXTERNAL LOCATION} | () |
8384+
| raw_pointer.rs:34:9:38:9 | { ... } | | {EXTERNAL LOCATION} | () |
8385+
| raw_pointer.rs:36:13:36:24 | * ... | | {EXTERNAL LOCATION} | i32 |
8386+
| raw_pointer.rs:36:13:36:28 | ... = ... | | {EXTERNAL LOCATION} | () |
8387+
| raw_pointer.rs:36:14:36:24 | ptr_written | | file://:0:0:0:0 | * |
8388+
| raw_pointer.rs:36:28:36:28 | a | | {EXTERNAL LOCATION} | i32 |
8389+
| raw_pointer.rs:37:23:37:33 | ptr_written | | file://:0:0:0:0 | * |
8390+
| raw_pointer.rs:42:24:42:27 | cond | | {EXTERNAL LOCATION} | bool |
8391+
| raw_pointer.rs:42:36:51:1 | { ... } | | {EXTERNAL LOCATION} | () |
8392+
| raw_pointer.rs:44:9:44:16 | ptr_read | | file://:0:0:0:0 | * |
8393+
| raw_pointer.rs:44:20:44:29 | null_mut(...) | | file://:0:0:0:0 | * |
8394+
| raw_pointer.rs:45:5:50:5 | if cond {...} | | {EXTERNAL LOCATION} | () |
8395+
| raw_pointer.rs:45:8:45:11 | cond | | {EXTERNAL LOCATION} | bool |
8396+
| raw_pointer.rs:45:13:50:5 | { ... } | | {EXTERNAL LOCATION} | () |
8397+
| raw_pointer.rs:46:9:49:9 | { ... } | | {EXTERNAL LOCATION} | () |
8398+
| raw_pointer.rs:48:17:48:18 | _y | | {EXTERNAL LOCATION} | i64 |
8399+
| raw_pointer.rs:48:27:48:35 | * ... | | {EXTERNAL LOCATION} | i64 |
8400+
| raw_pointer.rs:48:28:48:35 | ptr_read | | file://:0:0:0:0 | * |
8401+
| raw_pointer.rs:53:15:60:1 | { ... } | | {EXTERNAL LOCATION} | () |
8402+
| raw_pointer.rs:54:5:54:32 | raw_pointer_const_deref(...) | | {EXTERNAL LOCATION} | i32 |
8403+
| raw_pointer.rs:54:29:54:31 | &10 | | {EXTERNAL LOCATION} | & |
8404+
| raw_pointer.rs:54:29:54:31 | &10 | TRef | {EXTERNAL LOCATION} | i32 |
8405+
| raw_pointer.rs:54:30:54:31 | 10 | | {EXTERNAL LOCATION} | i32 |
8406+
| raw_pointer.rs:55:5:55:36 | raw_pointer_mut_deref(...) | | {EXTERNAL LOCATION} | i32 |
8407+
| raw_pointer.rs:55:27:55:35 | &mut true | | {EXTERNAL LOCATION} | & |
8408+
| raw_pointer.rs:55:27:55:35 | &mut true | TRef | {EXTERNAL LOCATION} | bool |
8409+
| raw_pointer.rs:55:32:55:35 | true | | {EXTERNAL LOCATION} | bool |
8410+
| raw_pointer.rs:56:5:56:22 | raw_const_borrow(...) | | {EXTERNAL LOCATION} | () |
8411+
| raw_pointer.rs:57:5:57:20 | raw_mut_borrow(...) | | {EXTERNAL LOCATION} | () |
8412+
| raw_pointer.rs:58:5:58:24 | raw_mut_write(...) | | {EXTERNAL LOCATION} | () |
8413+
| raw_pointer.rs:58:19:58:23 | false | | {EXTERNAL LOCATION} | bool |
8414+
| raw_pointer.rs:59:5:59:30 | raw_type_from_deref(...) | | {EXTERNAL LOCATION} | () |
8415+
| raw_pointer.rs:59:25:59:29 | false | | {EXTERNAL LOCATION} | bool |
83368416
testFailures

0 commit comments

Comments
 (0)