rust: use the new name Location::file_as_c_str() in Rust >= 1.91.0

As part of the stabilization of Location::file_with_nul(), it was brought
up that the with_nul() suffix usually means something else in Rust APIs,
so the API is being renamed prior to stabilization [1].

Thus, use the new name on new rustc versions.

Link: https://www.github.com/rust-lang/rust/pull/145928 [1]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250827-file_as_c_str-v1-1-d3f5a3916a9c@google.com
[ Kept `cfg` separation. Reworded slightly. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by Alice Ryhl and committed by Miguel Ojeda c09461a0 1b237f19

+13 -5
+3
init/Kconfig
··· 145 config RUSTC_HAS_FILE_WITH_NUL 146 def_bool RUSTC_VERSION >= 108900 147 148 config PAHOLE_VERSION 149 int 150 default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))
··· 145 config RUSTC_HAS_FILE_WITH_NUL 146 def_bool RUSTC_VERSION >= 108900 147 148 + config RUSTC_HAS_FILE_AS_C_STR 149 + def_bool RUSTC_VERSION >= 109100 150 + 151 config PAHOLE_VERSION 152 int 153 default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))
+10 -5
rust/kernel/lib.rs
··· 296 297 /// Gets the C string file name of a [`Location`]. 298 /// 299 - /// If `file_with_nul()` is not available, returns a string that warns about it. 300 /// 301 /// [`Location`]: core::panic::Location 302 /// ··· 310 /// let caller = core::panic::Location::caller(); 311 /// 312 /// // Output: 313 - /// // - A path like "rust/kernel/example.rs" if file_with_nul() is available. 314 - /// // - "<Location::file_with_nul() not supported>" otherwise. 315 /// let caller_file = file_from_location(caller); 316 /// 317 /// // Prints out the message with caller's file name. ··· 326 /// ``` 327 #[inline] 328 pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr { 329 - #[cfg(CONFIG_RUSTC_HAS_FILE_WITH_NUL)] 330 { 331 loc.file_with_nul() 332 } ··· 339 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))] 340 { 341 let _ = loc; 342 - c"<Location::file_with_nul() not supported>" 343 } 344 }
··· 296 297 /// Gets the C string file name of a [`Location`]. 298 /// 299 + /// If `Location::file_as_c_str()` is not available, returns a string that warns about it. 300 /// 301 /// [`Location`]: core::panic::Location 302 /// ··· 310 /// let caller = core::panic::Location::caller(); 311 /// 312 /// // Output: 313 + /// // - A path like "rust/kernel/example.rs" if `file_as_c_str()` is available. 314 + /// // - "<Location::file_as_c_str() not supported>" otherwise. 315 /// let caller_file = file_from_location(caller); 316 /// 317 /// // Prints out the message with caller's file name. ··· 326 /// ``` 327 #[inline] 328 pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr { 329 + #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)] 330 + { 331 + loc.file_as_c_str() 332 + } 333 + 334 + #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))] 335 { 336 loc.file_with_nul() 337 } ··· 334 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))] 335 { 336 let _ = loc; 337 + c"<Location::file_as_c_str() not supported>" 338 } 339 }