Skip to content

Commit b625747

Browse files
author
Igor Korotin
committed
rust: macros: Fix macro referencing core and kernel crates
To prevent issues where user-defined modules named `core` or `kernel` could be picked up instead of the standard ones, update macros to use absolute paths, `::core::...` and `::kernel::...`, for core and kernel references. Suggested-by: y86-dev <[email protected]> Closes: Rust-for-Linux#1150 Signed-off-by: Igor Korotin <[email protected]>
1 parent 28bb48c commit b625747

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
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
@@ -214,7 +214,7 @@ unsafe impl Sync for Device {}
214214
macro_rules! dev_printk {
215215
($method:ident, $dev:expr, $($f:tt)*) => {
216216
{
217-
($dev).$method(core::format_args!($($f)*));
217+
($dev).$method(::core::format_args!($($f)*));
218218
}
219219
}
220220
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
///
1717
/// ```
1818
/// static_assert!(42 > 24);
19-
/// static_assert!(core::mem::size_of::<u8>() == 1);
19+
/// static_assert!(::core::mem::size_of::<u8>() == 1);
2020
///
2121
/// const X: &[u8] = b"bar";
2222
/// static_assert!(X[1] == b'a');
@@ -29,6 +29,6 @@
2929
#[macro_export]
3030
macro_rules! static_assert {
3131
($condition:expr) => {
32-
const _: () = core::assert!($condition);
32+
const _: () = ::core::assert!($condition);
3333
};
3434
}

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/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
/// };

scripts/rustdoc_test_gen.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,19 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut kernel::bindings::kunit) {{
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

0 commit comments

Comments
 (0)