File: lint-never-type-fallback-flowing-into-unsafe.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (170 lines) | stat: -rw-r--r-- 5,294 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//@ revisions: e2015 e2024
//@[e2015] check-pass
//@[e2024] check-fail
//@[e2024] edition:2024

use std::{marker, mem, ptr};

fn main() {}

fn _zero() {
    if false {
        unsafe { mem::zeroed() }
        //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
        //[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
        //[e2024]~| warning: the type `!` does not permit zero-initialization
    } else {
        return;
    };

    // no ; -> type is inferred without fallback
    if true { unsafe { mem::zeroed() } } else { return }
}

fn _trans() {
    if false {
        unsafe {
            struct Zst;
            core::mem::transmute(Zst)
            //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
            //[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
            //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
        }
    } else {
        return;
    };
}

fn _union() {
    if false {
        union Union<T: Copy> {
            a: (),
            b: T,
        }

        unsafe { Union { a: () }.b }
        //[e2015]~^ warn: never type fallback affects this union access
        //[e2024]~^^ error: never type fallback affects this union access
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
    } else {
        return;
    };
}

fn _deref() {
    if false {
        unsafe { *ptr::from_ref(&()).cast() }
        //[e2015]~^ warn: never type fallback affects this raw pointer dereference
        //[e2024]~^^ error: never type fallback affects this raw pointer dereference
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
    } else {
        return;
    };
}

fn _only_generics() {
    if false {
        unsafe fn internally_create<T>(_: Option<T>) {
            unsafe {
                let _ = mem::zeroed::<T>();
            }
        }

        // We need the option (and unwrap later) to call a function in a way,
        // which makes it affected by the fallback, but without having it return anything
        let x = None;

        unsafe { internally_create(x) }
        //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
        //[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!

        x.unwrap()
    } else {
        return;
    };
}

fn _stored_function() {
    if false {
        let zeroed = mem::zeroed;
        //[e2015]~^ warn: never type fallback affects this `unsafe` function
        //[e2024]~^^ error: never type fallback affects this `unsafe` function
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!

        unsafe { zeroed() }
        //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
        //[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
    } else {
        return;
    };
}

fn _only_generics_stored_function() {
    if false {
        unsafe fn internally_create<T>(_: Option<T>) {
            unsafe {
                let _ = mem::zeroed::<T>();
            }
        }

        let x = None;
        let f = internally_create;
        //[e2015]~^ warn: never type fallback affects this `unsafe` function
        //[e2024]~^^ error: never type fallback affects this `unsafe` function
        //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!

        unsafe { f(x) }

        x.unwrap()
    } else {
        return;
    };
}

fn _method() {
    struct S<T>(marker::PhantomData<T>);

    impl<T> S<T> {
        #[allow(unused)] // FIXME: the unused lint is probably incorrect here
        unsafe fn create_out_of_thin_air(&self) -> T {
            todo!()
        }
    }

    if false {
        unsafe {
            S(marker::PhantomData).create_out_of_thin_air()
            //[e2015]~^ warn: never type fallback affects this call to an `unsafe` method
            //[e2024]~^^ error: never type fallback affects this call to an `unsafe` method
            //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
        }
    } else {
        return;
    };
}

// Minimization of the famous `objc` crate issue
fn _objc() {
    pub unsafe fn send_message<R>() -> Result<R, ()> {
        Ok(unsafe { core::mem::zeroed() })
    }

    macro_rules! msg_send {
        () => {
            match send_message::<_ /* ?0 */>() {
                //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
                //[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
                //~| warn: this changes meaning in Rust 2024 and in a future release in all editions!
                Ok(x) => x,
                Err(_) => loop {},
            }
        };
    }

    unsafe {
        msg_send!();
    }
}