summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorGary Guo <gary@garyguo.net>2026-09-02 23:00:00 +0100
committerMiguel Ojeda <ojeda@kernel.org>2026-10-03 14:04:56 +0200
commite9a35442ef91f7f599a95e04928e80748fba2998 (patch)
tree289ebab0252f0220416a9cc186ec966e3d52e98b /scripts
parent3504d1bce9920b23499ab1af682cdf1eceba7c75 (diff)
rust: kunit: use `line!()` inside `kunit_assert!`
The `diff` parameter is needed currently because doctests want to override the line number. Simplify it by changing it to use `line!()`. Have doctests override `line!()` macro to achieve the current behavior. An existing doctest requires `line!()` to expand to a literal; so update it to use `::core::line!()` instead. Reviewed-by: David Gow <david@davidgow.net> Signed-off-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260902-kunit-v2-2-eda3ed6c00c5@garyguo.net [ Removed spurious `///`-to-`//` change and adjusted `line!` to match. Added a comment to match `file!`'s. Made `NOTE:` more precise. Reworded to say it is only one doctest being updated. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/rustdoc_test_gen.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
index bb57745ec339..7159cd6e726d 100644
--- a/scripts/rustdoc_test_gen.rs
+++ b/scripts/rustdoc_test_gen.rs
@@ -182,12 +182,20 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
() => {{ "{real_path}" }}
}}
+ /// Overrides the usual [`line!`] macro with one that expands to the real line number.
+ #[allow(unused)]
+ macro_rules! line {{
+ // NOTE: This does not expand to a literal, but a constant expression.
+ // Therefore code that depends on `line!()` expanding to a literal needs special adjustment.
+ () => {{ const {{ ::core::line!() - __DOCTEST_ANCHOR + {line} }} }}
+ }}
+
/// Overrides the usual [`assert!`] macro with one that calls KUnit instead.
#[allow(unused)]
macro_rules! assert {{
($cond:expr $(,)?) => {{{{
::kernel::kunit_assert!(
- "{kunit_name}", __DOCTEST_ANCHOR - {line}, $cond
+ "{kunit_name}", $cond
);
}}}}
}}
@@ -197,7 +205,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
macro_rules! assert_eq {{
($left:expr, $right:expr $(,)?) => {{{{
::kernel::kunit_assert_eq!(
- "{kunit_name}", __DOCTEST_ANCHOR - {line}, $left, $right
+ "{kunit_name}", $left, $right
);
}}}}
}}
@@ -219,7 +227,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{
/// The anchor where the test code body starts.
#[allow(unused)]
- static __DOCTEST_ANCHOR: i32 = ::core::line!() as i32 + {body_offset} + 2;
+ static __DOCTEST_ANCHOR: u32 = ::core::line!() + {body_offset} + 2;
{{
#![allow(unreachable_pub, clippy::disallowed_names)]
{body}