diff options
| author | Taylor Cramer <cramertj@google.com> | 2025-01-02 17:47:10 -0800 |
|---|---|---|
| committer | Taylor Cramer <cramertj@google.com> | 2025-06-04 09:23:05 -0700 |
| commit | b541f93372c24bcbf036efc2b454cff6f6b8f8c5 (patch) | |
| tree | ec2b844a115219cb2d6efe28a4007aefa08c45e8 /tests | |
| parent | 61413aea937d9663d01b62902535f8d4ec85cc95 (diff) | |
| download | rust-b541f93372c24bcbf036efc2b454cff6f6b8f8c5.tar.gz rust-b541f93372c24bcbf036efc2b454cff6f6b8f8c5.zip | |
Add Location::file_with_nul
This is useful for C/C++ APIs which expect the const char* returned from __FILE__ or std::source_location::file_name.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/rfcs/rfc-2091-track-caller/file-is-nul-terminated.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/ui/rfcs/rfc-2091-track-caller/file-is-nul-terminated.rs b/tests/ui/rfcs/rfc-2091-track-caller/file-is-nul-terminated.rs new file mode 100644 index 00000000000..65e61a21f1a --- /dev/null +++ b/tests/ui/rfcs/rfc-2091-track-caller/file-is-nul-terminated.rs @@ -0,0 +1,25 @@ +//@ run-pass +#![feature(file_with_nul)] + +#[track_caller] +const fn assert_file_has_trailing_zero() { + let caller = core::panic::Location::caller(); + let file_str = caller.file(); + let file_with_nul = caller.file_with_nul(); + if file_str.len() != file_with_nul.count_bytes() { + panic!("mismatched lengths"); + } + let trailing_byte: core::ffi::c_char = unsafe { + *file_with_nul.as_ptr().offset(file_with_nul.count_bytes() as _) + }; + if trailing_byte != 0 { + panic!("trailing byte was nonzero") + } +} + +#[allow(dead_code)] +const _: () = assert_file_has_trailing_zero(); + +fn main() { + assert_file_has_trailing_zero(); +} |
