Skip to content

Commit de7cd3e

Browse files
ipv6foreverojeda
authored andcommitted
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 <[email protected]> Closes: #1150 Signed-off-by: Igor Korotin <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Applied `rustfmt`. Reworded slightly. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 977c430 commit de7cd3e

File tree

11 files changed

+56
-49
lines changed

11 files changed

+56
-49
lines changed

rust/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ macro_rules! alias {
1717

1818
// Check size compatibility with `core`.
1919
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>()
2121
);
2222
)*}
2323
}

rust/kernel/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl DeviceContext for Normal {}
240240
macro_rules! dev_printk {
241241
($method:ident, $dev:expr, $($f:tt)*) => {
242242
{
243-
($dev).$method(core::format_args!($($f)*));
243+
($dev).$method(::core::format_args!($($f)*));
244244
}
245245
}
246246
}

rust/kernel/device_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ macro_rules! module_device_table {
159159
"_", line!(),
160160
"_", stringify!($table_name))
161161
]
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()) };
164164
};
165165
}

rust/kernel/kunit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ macro_rules! kunit_assert {
5959
}
6060

6161
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;
6363
static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
6464

6565
// SAFETY: FFI call without safety requirements.
@@ -130,11 +130,11 @@ macro_rules! kunit_assert {
130130
unsafe {
131131
$crate::bindings::__kunit_do_failed_assertion(
132132
kunit_test,
133-
core::ptr::addr_of!(LOCATION.0),
133+
::core::ptr::addr_of!(LOCATION.0),
134134
$crate::bindings::kunit_assert_type_KUNIT_ASSERTION,
135-
core::ptr::addr_of!(ASSERTION.0.assert),
135+
::core::ptr::addr_of!(ASSERTION.0.assert),
136136
Some($crate::bindings::kunit_unary_assert_format),
137-
core::ptr::null(),
137+
::core::ptr::null(),
138138
);
139139
}
140140

rust/kernel/static_assert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
#[macro_export]
3535
macro_rules! static_assert {
3636
($condition:expr $(,$arg:literal)?) => {
37-
const _: () = core::assert!($condition $(,$arg)?);
37+
const _: () = ::core::assert!($condition $(,$arg)?);
3838
};
3939
}

rust/kernel/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ mod tests {
595595

596596
macro_rules! format {
597597
($($f:tt)*) => ({
598-
&*String::from_fmt(kernel::fmt!($($f)*))
598+
&*String::from_fmt(::kernel::fmt!($($f)*))
599599
})
600600
}
601601

@@ -944,5 +944,5 @@ impl fmt::Debug for CString {
944944
/// A convenience alias for [`core::format_args`].
945945
#[macro_export]
946946
macro_rules! fmt {
947-
($($f:tt)*) => ( core::format_args!($($f)*) )
947+
($($f:tt)*) => ( ::core::format_args!($($f)*) )
948948
}

rust/macros/kunit.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,43 +85,43 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
8585
// Looks like:
8686
//
8787
// ```
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(); }
9090
//
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(),
9595
// ];
9696
//
97-
// kernel::kunit_unsafe_test_suite!(kunit_test_suit_name, TEST_CASES);
97+
// ::kernel::kunit_unsafe_test_suite!(kunit_test_suit_name, TEST_CASES);
9898
// ```
9999
let mut kunit_macros = "".to_owned();
100100
let mut test_cases = "".to_owned();
101101
for test in &tests {
102102
let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}");
103103
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}(); }}"
105105
);
106106
writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
107107
writeln!(
108108
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}),"
110110
)
111111
.unwrap();
112112
}
113113

114114
writeln!(kunit_macros).unwrap();
115115
writeln!(
116116
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];",
118118
tests.len() + 1
119119
)
120120
.unwrap();
121121

122122
writeln!(
123123
kunit_macros,
124-
"kernel::kunit_unsafe_test_suite!({attr}, TEST_CASES);"
124+
"::kernel::kunit_unsafe_test_suite!({attr}, TEST_CASES);"
125125
)
126126
.unwrap();
127127

rust/macros/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
283283
/// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14;
284284
/// macro_rules! pub_no_prefix {
285285
/// ($prefix:ident, $($newname:ident),+) => {
286-
/// kernel::macros::paste! {
286+
/// ::kernel::macros::paste! {
287287
/// $(pub(crate) const $newname: u32 = [<$prefix $newname>];)+
288288
/// }
289289
/// };
@@ -340,7 +340,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
340340
/// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14;
341341
/// macro_rules! pub_no_prefix {
342342
/// ($prefix:ident, $($newname:ident),+) => {
343-
/// kernel::macros::paste! {
343+
/// ::kernel::macros::paste! {
344344
/// $(pub(crate) const fn [<$newname:lower:span>]() -> u32 { [<$prefix $newname:span>] })+
345345
/// }
346346
/// };
@@ -375,7 +375,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
375375
/// ```
376376
/// macro_rules! create_numbered_fn {
377377
/// ($name:literal, $val:literal) => {
378-
/// kernel::macros::paste! {
378+
/// ::kernel::macros::paste! {
379379
/// fn [<some_ $name _fn $val>]() -> u32 { $val }
380380
/// }
381381
/// };

rust/macros/module.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,24 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
215215
// SAFETY: `__this_module` is constructed by the kernel at load time and will not be
216216
// freed until the module is unloaded.
217217
#[cfg(MODULE)]
218-
static THIS_MODULE: kernel::ThisModule = unsafe {{
218+
static THIS_MODULE: ::kernel::ThisModule = unsafe {{
219219
extern \"C\" {{
220-
static __this_module: kernel::types::Opaque<kernel::bindings::module>;
220+
static __this_module: ::kernel::types::Opaque<::kernel::bindings::module>;
221221
}}
222222
223-
kernel::ThisModule::from_ptr(__this_module.get())
223+
::kernel::ThisModule::from_ptr(__this_module.get())
224224
}};
225225
#[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())
228228
}};
229229
230230
/// The `LocalModule` type is the type of the module created by `module!`,
231231
/// `module_pci_driver!`, `module_platform_driver!`, etc.
232232
type LocalModule = {type_};
233233
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}\");
236236
}}
237237
238238
// Double nested modules, since then nobody can access the public items inside.
@@ -250,8 +250,8 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
250250
#[used]
251251
static __IS_RUST_MODULE: () = ();
252252
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();
255255
256256
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
257257
/// # Safety
@@ -262,7 +262,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
262262
#[doc(hidden)]
263263
#[no_mangle]
264264
#[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 {{
266266
// SAFETY: This function is inaccessible to the outside due to the double
267267
// module wrapping it. It is called exactly once by the C side via its
268268
// unique name.
@@ -302,11 +302,12 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
302302
#[doc(hidden)]
303303
#[link_section = \"{initcall_section}\"]
304304
#[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;
306307
307308
#[cfg(not(MODULE))]
308309
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
309-
core::arch::global_asm!(
310+
::core::arch::global_asm!(
310311
r#\".section \"{initcall_section}\", \"a\"
311312
__{name}_initcall:
312313
.long __{name}_init - .
@@ -317,7 +318,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
317318
#[cfg(not(MODULE))]
318319
#[doc(hidden)]
319320
#[no_mangle]
320-
pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
321+
pub extern \"C\" fn __{name}_init() -> ::kernel::ffi::c_int {{
321322
// SAFETY: This function is inaccessible to the outside due to the double
322323
// module wrapping it. It is called exactly once by the C side via its
323324
// placement above in the initcall section.
@@ -340,9 +341,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
340341
/// # Safety
341342
///
342343
/// This function must only be called once.
343-
unsafe fn __init() -> kernel::ffi::c_int {{
344+
unsafe fn __init() -> ::kernel::ffi::c_int {{
344345
let initer =
345-
<{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
346+
<{type_} as ::kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
346347
// SAFETY: No data race, since `__MOD` can only be accessed by this module
347348
// and there only `__init` and `__exit` access it. These functions are only
348349
// called once and `__exit` cannot be called before or during `__init`.

scripts/rustdoc_test_builder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
//
2929
// ```
3030
// 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> {
3232
// ```
3333
//
3434
// It should be unlikely that doctest code matches such lines (when code is formatted properly).
@@ -49,8 +49,10 @@ fn main() {
4949

5050
// Qualify `Result` to avoid the collision with our own `Result` coming from the prelude.
5151
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+
),
5456
);
5557

5658
// For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on

scripts/rustdoc_test_gen.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,30 @@ fn main() {
167167
rust_tests,
168168
r#"/// Generated `{name}` KUnit test case from a Rust documentation test.
169169
#[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) {{
171171
/// Overrides the usual [`assert!`] macro with one that calls KUnit instead.
172172
#[allow(unused)]
173173
macro_rules! assert {{
174174
($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+
);
176178
}}}}
177179
}}
178180
179181
/// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead.
180182
#[allow(unused)]
181183
macro_rules! assert_eq {{
182184
($left:expr, $right:expr $(,)?) => {{{{
183-
kernel::kunit_assert_eq!("{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right);
185+
::kernel::kunit_assert_eq!(
186+
"{kunit_name}", "{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right
187+
);
184188
}}}}
185189
}}
186190
187191
// Many tests need the prelude, so provide it by default.
188192
#[allow(unused)]
189-
use kernel::prelude::*;
193+
use ::kernel::prelude::*;
190194
191195
// Unconditionally print the location of the original doctest (i.e. rather than the location in
192196
// the generated file) so that developers can easily map the test back to the source code.
@@ -197,11 +201,11 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut kernel::bindings::kunit) {{
197201
// This follows the syntax for declaring test metadata in the proposed KTAP v2 spec, which may
198202
// be used for the proposed KUnit test attributes API. Thus hopefully this will make migration
199203
// easier later on.
200-
kernel::kunit::info(format_args!(" # {kunit_name}.location: {real_path}:{line}\n"));
204+
::kernel::kunit::info(format_args!(" # {kunit_name}.location: {real_path}:{line}\n"));
201205
202206
/// The anchor where the test code body starts.
203207
#[allow(unused)]
204-
static __DOCTEST_ANCHOR: i32 = core::line!() as i32 + {body_offset} + 1;
208+
static __DOCTEST_ANCHOR: i32 = ::core::line!() as i32 + {body_offset} + 1;
205209
{{
206210
{body}
207211
main();

0 commit comments

Comments
 (0)