Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rust: use absolute paths in macros referencing core and kernel

Macros and auto-generated code should use absolute paths, `::core::...`
and `::kernel::...`, for core and kernel references.

This prevents issues where user-defined modules named `core` or `kernel`
could be picked up instead of the `core` or `kernel` crates.

Thus clean some references up.

Suggested-by: Benno Lossin <benno.lossin@proton.me>
Closes: https://github.com/Rust-for-Linux/linux/issues/1150
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/r/20250519164615.3310844-1-igor.korotin.linux@gmail.com
[ Applied `rustfmt`. Reworded slightly. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Igor Korotin and committed by
Miguel Ojeda
de7cd3e4 977c4308

+56 -49
+1 -1
rust/ffi.rs
··· 17 17 18 18 // Check size compatibility with `core`. 19 19 const _: () = assert!( 20 - core::mem::size_of::<$name>() == core::mem::size_of::<core::ffi::$name>() 20 + ::core::mem::size_of::<$name>() == ::core::mem::size_of::<::core::ffi::$name>() 21 21 ); 22 22 )*} 23 23 }
+1 -1
rust/kernel/device.rs
··· 240 240 macro_rules! dev_printk { 241 241 ($method:ident, $dev:expr, $($f:tt)*) => { 242 242 { 243 - ($dev).$method(core::format_args!($($f)*)); 243 + ($dev).$method(::core::format_args!($($f)*)); 244 244 } 245 245 } 246 246 }
+2 -2
rust/kernel/device_id.rs
··· 159 159 "_", line!(), 160 160 "_", stringify!($table_name)) 161 161 ] 162 - static $module_table_name: [core::mem::MaybeUninit<u8>; $table_name.raw_ids().size()] = 163 - unsafe { core::mem::transmute_copy($table_name.raw_ids()) }; 162 + static $module_table_name: [::core::mem::MaybeUninit<u8>; $table_name.raw_ids().size()] = 163 + unsafe { ::core::mem::transmute_copy($table_name.raw_ids()) }; 164 164 }; 165 165 }
+4 -4
rust/kernel/kunit.rs
··· 59 59 } 60 60 61 61 static FILE: &'static $crate::str::CStr = $crate::c_str!($file); 62 - static LINE: i32 = core::line!() as i32 - $diff; 62 + static LINE: i32 = ::core::line!() as i32 - $diff; 63 63 static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); 64 64 65 65 // SAFETY: FFI call without safety requirements. ··· 130 130 unsafe { 131 131 $crate::bindings::__kunit_do_failed_assertion( 132 132 kunit_test, 133 - core::ptr::addr_of!(LOCATION.0), 133 + ::core::ptr::addr_of!(LOCATION.0), 134 134 $crate::bindings::kunit_assert_type_KUNIT_ASSERTION, 135 - core::ptr::addr_of!(ASSERTION.0.assert), 135 + ::core::ptr::addr_of!(ASSERTION.0.assert), 136 136 Some($crate::bindings::kunit_unary_assert_format), 137 - core::ptr::null(), 137 + ::core::ptr::null(), 138 138 ); 139 139 } 140 140
+1 -1
rust/kernel/static_assert.rs
··· 34 34 #[macro_export] 35 35 macro_rules! static_assert { 36 36 ($condition:expr $(,$arg:literal)?) => { 37 - const _: () = core::assert!($condition $(,$arg)?); 37 + const _: () = ::core::assert!($condition $(,$arg)?); 38 38 }; 39 39 }
+2 -2
rust/kernel/str.rs
··· 595 595 596 596 macro_rules! format { 597 597 ($($f:tt)*) => ({ 598 - &*String::from_fmt(kernel::fmt!($($f)*)) 598 + &*String::from_fmt(::kernel::fmt!($($f)*)) 599 599 }) 600 600 } 601 601 ··· 944 944 /// A convenience alias for [`core::format_args`]. 945 945 #[macro_export] 946 946 macro_rules! fmt { 947 - ($($f:tt)*) => ( core::format_args!($($f)*) ) 947 + ($($f:tt)*) => ( ::core::format_args!($($f)*) ) 948 948 }
+11 -11
rust/macros/kunit.rs
··· 85 85 // Looks like: 86 86 // 87 87 // ``` 88 - // unsafe extern "C" fn kunit_rust_wrapper_foo(_test: *mut kernel::bindings::kunit) { foo(); } 89 - // unsafe extern "C" fn kunit_rust_wrapper_bar(_test: *mut kernel::bindings::kunit) { bar(); } 88 + // unsafe extern "C" fn kunit_rust_wrapper_foo(_test: *mut ::kernel::bindings::kunit) { foo(); } 89 + // unsafe extern "C" fn kunit_rust_wrapper_bar(_test: *mut ::kernel::bindings::kunit) { bar(); } 90 90 // 91 - // static mut TEST_CASES: [kernel::bindings::kunit_case; 3] = [ 92 - // kernel::kunit::kunit_case(kernel::c_str!("foo"), kunit_rust_wrapper_foo), 93 - // kernel::kunit::kunit_case(kernel::c_str!("bar"), kunit_rust_wrapper_bar), 94 - // kernel::kunit::kunit_case_null(), 91 + // static mut TEST_CASES: [::kernel::bindings::kunit_case; 3] = [ 92 + // ::kernel::kunit::kunit_case(::kernel::c_str!("foo"), kunit_rust_wrapper_foo), 93 + // ::kernel::kunit::kunit_case(::kernel::c_str!("bar"), kunit_rust_wrapper_bar), 94 + // ::kernel::kunit::kunit_case_null(), 95 95 // ]; 96 96 // 97 - // kernel::kunit_unsafe_test_suite!(kunit_test_suit_name, TEST_CASES); 97 + // ::kernel::kunit_unsafe_test_suite!(kunit_test_suit_name, TEST_CASES); 98 98 // ``` 99 99 let mut kunit_macros = "".to_owned(); 100 100 let mut test_cases = "".to_owned(); 101 101 for test in &tests { 102 102 let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}"); 103 103 let kunit_wrapper = format!( 104 - "unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut kernel::bindings::kunit) {{ {test}(); }}" 104 + "unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit) {{ {test}(); }}" 105 105 ); 106 106 writeln!(kunit_macros, "{kunit_wrapper}").unwrap(); 107 107 writeln!( 108 108 test_cases, 109 - " kernel::kunit::kunit_case(kernel::c_str!(\"{test}\"), {kunit_wrapper_fn_name})," 109 + " ::kernel::kunit::kunit_case(::kernel::c_str!(\"{test}\"), {kunit_wrapper_fn_name})," 110 110 ) 111 111 .unwrap(); 112 112 } ··· 114 114 writeln!(kunit_macros).unwrap(); 115 115 writeln!( 116 116 kunit_macros, 117 - "static mut TEST_CASES: [kernel::bindings::kunit_case; {}] = [\n{test_cases} kernel::kunit::kunit_case_null(),\n];", 117 + "static mut TEST_CASES: [::kernel::bindings::kunit_case; {}] = [\n{test_cases} ::kernel::kunit::kunit_case_null(),\n];", 118 118 tests.len() + 1 119 119 ) 120 120 .unwrap(); 121 121 122 122 writeln!( 123 123 kunit_macros, 124 - "kernel::kunit_unsafe_test_suite!({attr}, TEST_CASES);" 124 + "::kernel::kunit_unsafe_test_suite!({attr}, TEST_CASES);" 125 125 ) 126 126 .unwrap(); 127 127
+3 -3
rust/macros/lib.rs
··· 283 283 /// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14; 284 284 /// macro_rules! pub_no_prefix { 285 285 /// ($prefix:ident, $($newname:ident),+) => { 286 - /// kernel::macros::paste! { 286 + /// ::kernel::macros::paste! { 287 287 /// $(pub(crate) const $newname: u32 = [<$prefix $newname>];)+ 288 288 /// } 289 289 /// }; ··· 340 340 /// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14; 341 341 /// macro_rules! pub_no_prefix { 342 342 /// ($prefix:ident, $($newname:ident),+) => { 343 - /// kernel::macros::paste! { 343 + /// ::kernel::macros::paste! { 344 344 /// $(pub(crate) const fn [<$newname:lower:span>]() -> u32 { [<$prefix $newname:span>] })+ 345 345 /// } 346 346 /// }; ··· 375 375 /// ``` 376 376 /// macro_rules! create_numbered_fn { 377 377 /// ($name:literal, $val:literal) => { 378 - /// kernel::macros::paste! { 378 + /// ::kernel::macros::paste! { 379 379 /// fn [<some_ $name _fn $val>]() -> u32 { $val } 380 380 /// } 381 381 /// };
+16 -15
rust/macros/module.rs
··· 215 215 // SAFETY: `__this_module` is constructed by the kernel at load time and will not be 216 216 // freed until the module is unloaded. 217 217 #[cfg(MODULE)] 218 - static THIS_MODULE: kernel::ThisModule = unsafe {{ 218 + static THIS_MODULE: ::kernel::ThisModule = unsafe {{ 219 219 extern \"C\" {{ 220 - static __this_module: kernel::types::Opaque<kernel::bindings::module>; 220 + static __this_module: ::kernel::types::Opaque<::kernel::bindings::module>; 221 221 }} 222 222 223 - kernel::ThisModule::from_ptr(__this_module.get()) 223 + ::kernel::ThisModule::from_ptr(__this_module.get()) 224 224 }}; 225 225 #[cfg(not(MODULE))] 226 - static THIS_MODULE: kernel::ThisModule = unsafe {{ 227 - kernel::ThisModule::from_ptr(core::ptr::null_mut()) 226 + static THIS_MODULE: ::kernel::ThisModule = unsafe {{ 227 + ::kernel::ThisModule::from_ptr(::core::ptr::null_mut()) 228 228 }}; 229 229 230 230 /// The `LocalModule` type is the type of the module created by `module!`, 231 231 /// `module_pci_driver!`, `module_platform_driver!`, etc. 232 232 type LocalModule = {type_}; 233 233 234 - impl kernel::ModuleMetadata for {type_} {{ 235 - const NAME: &'static kernel::str::CStr = kernel::c_str!(\"{name}\"); 234 + impl ::kernel::ModuleMetadata for {type_} {{ 235 + const NAME: &'static ::kernel::str::CStr = ::kernel::c_str!(\"{name}\"); 236 236 }} 237 237 238 238 // Double nested modules, since then nobody can access the public items inside. ··· 250 250 #[used] 251 251 static __IS_RUST_MODULE: () = (); 252 252 253 - static mut __MOD: core::mem::MaybeUninit<{type_}> = 254 - core::mem::MaybeUninit::uninit(); 253 + static mut __MOD: ::core::mem::MaybeUninit<{type_}> = 254 + ::core::mem::MaybeUninit::uninit(); 255 255 256 256 // Loadable modules need to export the `{{init,cleanup}}_module` identifiers. 257 257 /// # Safety ··· 262 262 #[doc(hidden)] 263 263 #[no_mangle] 264 264 #[link_section = \".init.text\"] 265 - pub unsafe extern \"C\" fn init_module() -> kernel::ffi::c_int {{ 265 + pub unsafe extern \"C\" fn init_module() -> ::kernel::ffi::c_int {{ 266 266 // SAFETY: This function is inaccessible to the outside due to the double 267 267 // module wrapping it. It is called exactly once by the C side via its 268 268 // unique name. ··· 302 302 #[doc(hidden)] 303 303 #[link_section = \"{initcall_section}\"] 304 304 #[used] 305 - pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init; 305 + pub static __{name}_initcall: extern \"C\" fn() -> ::kernel::ffi::c_int = 306 + __{name}_init; 306 307 307 308 #[cfg(not(MODULE))] 308 309 #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)] 309 - core::arch::global_asm!( 310 + ::core::arch::global_asm!( 310 311 r#\".section \"{initcall_section}\", \"a\" 311 312 __{name}_initcall: 312 313 .long __{name}_init - . ··· 318 317 #[cfg(not(MODULE))] 319 318 #[doc(hidden)] 320 319 #[no_mangle] 321 - pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{ 320 + pub extern \"C\" fn __{name}_init() -> ::kernel::ffi::c_int {{ 322 321 // SAFETY: This function is inaccessible to the outside due to the double 323 322 // module wrapping it. It is called exactly once by the C side via its 324 323 // placement above in the initcall section. ··· 341 340 /// # Safety 342 341 /// 343 342 /// This function must only be called once. 344 - unsafe fn __init() -> kernel::ffi::c_int {{ 343 + unsafe fn __init() -> ::kernel::ffi::c_int {{ 345 344 let initer = 346 - <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE); 345 + <{type_} as ::kernel::InPlaceModule>::init(&super::super::THIS_MODULE); 347 346 // SAFETY: No data race, since `__MOD` can only be accessed by this module 348 347 // and there only `__init` and `__exit` access it. These functions are only 349 348 // called once and `__exit` cannot be called before or during `__init`.
+5 -3
scripts/rustdoc_test_builder.rs
··· 28 28 // 29 29 // ``` 30 30 // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_28_0() { 31 - // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl core::fmt::Debug> { 31 + // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl ::core::fmt::Debug> { 32 32 // ``` 33 33 // 34 34 // It should be unlikely that doctest code matches such lines (when code is formatted properly). ··· 49 49 50 50 // Qualify `Result` to avoid the collision with our own `Result` coming from the prelude. 51 51 let body = body.replace( 52 - &format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"), 53 - &format!("{rustdoc_function_name}() -> core::result::Result<(), impl core::fmt::Debug> {{"), 52 + &format!("{rustdoc_function_name}() -> Result<(), impl ::core::fmt::Debug> {{"), 53 + &format!( 54 + "{rustdoc_function_name}() -> ::core::result::Result<(), impl ::core::fmt::Debug> {{" 55 + ), 54 56 ); 55 57 56 58 // For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on
+10 -6
scripts/rustdoc_test_gen.rs
··· 167 167 rust_tests, 168 168 r#"/// Generated `{name}` KUnit test case from a Rust documentation test. 169 169 #[no_mangle] 170 - pub extern "C" fn {kunit_name}(__kunit_test: *mut kernel::bindings::kunit) {{ 170 + pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ 171 171 /// Overrides the usual [`assert!`] macro with one that calls KUnit instead. 172 172 #[allow(unused)] 173 173 macro_rules! assert {{ 174 174 ($cond:expr $(,)?) => {{{{ 175 - kernel::kunit_assert!("{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond); 175 + ::kernel::kunit_assert!( 176 + "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $cond 177 + ); 176 178 }}}} 177 179 }} 178 180 ··· 182 180 #[allow(unused)] 183 181 macro_rules! assert_eq {{ 184 182 ($left:expr, $right:expr $(,)?) => {{{{ 185 - kernel::kunit_assert_eq!("{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right); 183 + ::kernel::kunit_assert_eq!( 184 + "{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right 185 + ); 186 186 }}}} 187 187 }} 188 188 189 189 // Many tests need the prelude, so provide it by default. 190 190 #[allow(unused)] 191 - use kernel::prelude::*; 191 + use ::kernel::prelude::*; 192 192 193 193 // Unconditionally print the location of the original doctest (i.e. rather than the location in 194 194 // the generated file) so that developers can easily map the test back to the source code. ··· 201 197 // This follows the syntax for declaring test metadata in the proposed KTAP v2 spec, which may 202 198 // be used for the proposed KUnit test attributes API. Thus hopefully this will make migration 203 199 // easier later on. 204 - kernel::kunit::info(format_args!(" # {kunit_name}.location: {real_path}:{line}\n")); 200 + ::kernel::kunit::info(format_args!(" # {kunit_name}.location: {real_path}:{line}\n")); 205 201 206 202 /// The anchor where the test code body starts. 207 203 #[allow(unused)] 208 - static __DOCTEST_ANCHOR: i32 = core::line!() as i32 + {body_offset} + 1; 204 + static __DOCTEST_ANCHOR: i32 = ::core::line!() as i32 + {body_offset} + 1; 209 205 {{ 210 206 {body} 211 207 main();