|
75 | 75 | This noncompliant example requires Rust Edition 2021 or earlier to compile. |
76 | 76 |
|
77 | 77 | .. rust-example:: |
| 78 | + :compile_fail: |
78 | 79 |
|
79 | 80 | // Undefined behavior by the linker or loader is possible |
80 | 81 | // if another 'something' function is defined. |
|
98 | 99 | This noncompliant example requires Rust Edition 2021 or earlier to compile. |
99 | 100 |
|
100 | 101 | .. rust-example:: |
101 | | -
|
102 | | - use std::ffi; |
103 | | -
|
104 | | - extern "C" { |
105 | | - // If 'malloc' is otherwise defined with a 'usize' argument, the compiler |
106 | | - // may generate code for calls to this function using this incompatible declaration, |
107 | | - // resulting in undefined behavior. |
108 | | - fn malloc(size: f32) -> *mut ffi::c_void; |
109 | | - } |
110 | | -
|
111 | | - fn main() { |
112 | | - // Call the extern "C" function (requires an 'unsafe' block) |
113 | | - unsafe { |
114 | | - let ptr = malloc(1024.0); |
115 | | - if !ptr.is_null() { |
116 | | - // Use the allocated memory then free it with 'libc::free' |
117 | | - println!("malloc returned: {:?}", ptr); |
118 | | - } |
119 | | - } |
120 | | - } |
| 102 | + :compile_fail: |
| 103 | +
|
| 104 | + use std::ffi; |
| 105 | +
|
| 106 | + extern "C" { |
| 107 | + // If 'malloc' is otherwise defined with a 'usize' argument, the compiler |
| 108 | + // may generate code for calls to this function using this incompatible declaration, |
| 109 | + // resulting in undefined behavior. |
| 110 | + fn malloc(size: f32) -> *mut ffi::c_void; |
| 111 | + } |
| 112 | +
|
| 113 | + fn main() { |
| 114 | + // Call the extern "C" function (requires an 'unsafe' block) |
| 115 | + unsafe { |
| 116 | + let ptr = malloc(1024.0); |
| 117 | + if !ptr.is_null() { |
| 118 | + // Use the allocated memory then free it with 'libc::free' |
| 119 | + println!("malloc returned: {:?}", ptr); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
121 | 123 |
|
122 | 124 | .. non_compliant_example:: |
123 | 125 | :id: non_compl_ex_Hk3mNp5qRs7t |
|
0 commit comments