-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nul-terminated filename for #[track_caller] #131828
Open
Darksonn
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
Darksonn:location-file-nul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#[cfg(not(bootstrap))] | ||
use crate::ffi::CStr; | ||
use crate::fmt; | ||
|
||
/// A struct containing information about the location of a panic. | ||
|
@@ -32,7 +34,11 @@ use crate::fmt; | |
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
#[stable(feature = "panic_hooks", since = "1.10.0")] | ||
pub struct Location<'a> { | ||
file: &'a str, | ||
// When not bootstrapping the compiler, it is an invariant that the last byte of this string | ||
// slice is a nul-byte. | ||
// | ||
// When bootstrapping the compiler, this string may be missing the nul-terminator. | ||
file_with_nul: &'a str, | ||
line: u32, | ||
col: u32, | ||
} | ||
|
@@ -127,7 +133,30 @@ impl<'a> Location<'a> { | |
#[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")] | ||
#[inline] | ||
pub const fn file(&self) -> &str { | ||
self.file | ||
// String slicing in const is very hard, see: | ||
// <https://users.rust-lang.org/t/slicing-strings-in-const/119836> | ||
|
||
let s = self.file_with_nul; | ||
|
||
#[cfg(bootstrap)] | ||
if !matches!(s.as_bytes().last(), Some(0)) { | ||
return s; | ||
} | ||
|
||
#[cfg(debug_assertions)] | ||
if !matches!(s.as_bytes().last(), Some(0)) { | ||
panic!("filename is not nul-terminated"); | ||
} | ||
|
||
// SAFETY: The string contains a nul-byte, so the length is at least one. | ||
let len = unsafe { s.len().unchecked_sub(1) }; | ||
|
||
// SAFETY: `s.as_ptr()` is valid for `len+1` bytes, so it is valid for `len` bytes. | ||
let file = unsafe { core::slice::from_raw_parts(s.as_ptr(), len) }; | ||
|
||
// SAFETY: This is valid utf-8 because the original string is valid utf-8 and the last | ||
// character was a nul-byte, so removing it does not cut a codepoint in half. | ||
unsafe { core::str::from_utf8_unchecked(file) } | ||
} | ||
|
||
/// Returns the line number from which the panic originated. | ||
|
@@ -179,24 +208,29 @@ impl<'a> Location<'a> { | |
pub const fn column(&self) -> u32 { | ||
self.col | ||
} | ||
} | ||
|
||
#[unstable( | ||
feature = "panic_internals", | ||
reason = "internal details of the implementation of the `panic!` and related macros", | ||
issue = "none" | ||
)] | ||
impl<'a> Location<'a> { | ||
#[doc(hidden)] | ||
pub const fn internal_constructor(file: &'a str, line: u32, col: u32) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this internal constructor does not seem to be used anymore. |
||
Location { file, line, col } | ||
/// Returns the name of the source file from which the panic originated as a nul-terminated | ||
/// string. | ||
/// | ||
/// This function is like [`Location::file`], except that it returns a nul-terminated string. It | ||
/// is mainly useful for passing the filename into C or C++ code. | ||
#[must_use] | ||
#[inline] | ||
#[unstable(feature = "panic_file_with_nul", issue = "none")] | ||
#[cfg(not(bootstrap))] | ||
pub fn file_with_nul(&self) -> &CStr { | ||
let file_with_nul = self.file_with_nul.as_bytes(); | ||
|
||
// SAFETY: This struct is only ever constructed by the compiler, which always inserts a | ||
// nul-terminator in this string. | ||
unsafe { CStr::from_bytes_with_nul_unchecked(file_with_nul) } | ||
} | ||
} | ||
|
||
#[stable(feature = "panic_hook_display", since = "1.26.0")] | ||
impl fmt::Display for Location<'_> { | ||
#[inline] | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(formatter, "{}:{}:{}", self.file, self.line, self.col) | ||
write!(formatter, "{}:{}:{}", self.file(), self.line, self.col) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to mark the string as cstring in the compiled object files to allow the linker to deduplicate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that done by using a different
MemoryKind
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we currently have a way to do this, but it would make sense to me to do it by adding a new
MemoryKind
and then special casing this in the codegen backends.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to complicate this PR too much. That sounds like it could happen as a follow-up.