File: implicit_autorefs.rs

package info (click to toggle)
rustc 1.88.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 934,128 kB
  • sloc: xml: 158,127; python: 36,062; javascript: 19,855; sh: 19,700; cpp: 18,947; ansic: 12,993; asm: 4,792; makefile: 690; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (99 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//@ check-pass
//@ run-rustfix

#![allow(dead_code)] // For the rustfix-ed code.

use std::mem::ManuallyDrop;
use std::ops::Deref;

unsafe fn test_const(ptr: *const [u8]) {
    let _ = (*ptr)[..16];
    //~^ WARN implicit autoref
}

struct Test {
    field: [u8],
}

unsafe fn test_field(ptr: *const Test) -> *const [u8] {
    let l = (*ptr).field.len();
    //~^ WARN implicit autoref

    &raw const (*ptr).field[..l - 1]
    //~^ WARN implicit autoref
}

unsafe fn test_builtin_index(a: *mut [String]) {
    _ = (*a)[0].len();
    //~^ WARN implicit autoref

    _ = (*a)[..1][0].len();
    //~^ WARN implicit autoref
    //~^^ WARN implicit autoref
}

unsafe fn test_overloaded_deref_const(ptr: *const ManuallyDrop<Test>) {
    let _ = (*ptr).field;
    //~^ WARN implicit autoref
    let _ = &raw const (*ptr).field;
    //~^ WARN implicit autoref
}

unsafe fn test_overloaded_deref_mut(ptr: *mut ManuallyDrop<Test>) {
    let _ = (*ptr).field;
    //~^ WARN implicit autoref
}

unsafe fn test_double_overloaded_deref_const(ptr: *const ManuallyDrop<ManuallyDrop<Test>>) {
    let _ = (*ptr).field;
    //~^ WARN implicit autoref
}

unsafe fn test_manually_overloaded_deref() {
    struct W<T>(T);

    impl<T> Deref for W<T> {
        type Target = T;
        fn deref(&self) -> &T { &self.0 }
    }

    let w: W<i32> = W(5);
    let w = &raw const w;
    let _p: *const i32 = &raw const **w;
    //~^ WARN implicit autoref
}

struct Test2 {
    // Derefs to `[u8]`.
    field: &'static [u8]
}

fn test_more_manual_deref(ptr: *const Test2) -> usize {
    unsafe { (*ptr).field.len() }
    //~^ WARN implicit autoref
}

unsafe fn test_no_attr(ptr: *mut ManuallyDrop<u8>) {
    ptr.write(ManuallyDrop::new(1)); // Should not warn, as `ManuallyDrop::write` is not
                                     // annotated with `#[rustc_no_implicit_auto_ref]`
}

unsafe fn test_vec_get(ptr: *mut Vec<u8>) {
    let _ = (*ptr).get(0);
    //~^ WARN implicit autoref
    let _ = (*ptr).get_unchecked(0);
    //~^ WARN implicit autoref
    let _ = (*ptr).get_mut(0);
    //~^ WARN implicit autoref
    let _ = (*ptr).get_unchecked_mut(0);
    //~^ WARN implicit autoref
}

unsafe fn test_string(ptr: *mut String) {
    let _ = (*ptr).len();
    //~^ WARN implicit autoref
    let _ = (*ptr).is_empty();
    //~^ WARN implicit autoref
}

fn main() {}