-
Notifications
You must be signed in to change notification settings - Fork 13k
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 Location::file_with_nul #135054
base: master
Are you sure you want to change the base?
Add Location::file_with_nul #135054
Conversation
Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred to the CTFE machinery cc @rust-lang/wg-const-eval |
library/core/src/panic/location.rs
Outdated
/// Although `file` returns an `&str`, the characters of `file` are guaranteed to be followed | ||
/// by a nul-terminator. This allows for greater interoperabilty with C and C++ code using | ||
/// `__FILE__` or `std::source_location::file_name`, both of which return nul-terminated | ||
/// `const char*`. |
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.
Note that under Stacked Borrows, it is Undefined Behavior to access the trailing nul byte since it is outside the range of this reference. So it seems ill-advised to write documentation that encourages people to perform such accesses.
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.
Removed. At one point I actually had a test for this and removed it when I realized this issue, but I forgot to remove the docs. Thanks for catching!
Renamed to |
This is useful for C/C++ APIs which expect the const char* returned from __FILE__ or std::source_location::file_name.
How does this compare with #131828? |
@bjorn3 This is essentially the same feature, although this implementation doesn't require an increase to the size of the |
The other PR does not change the size of the |
let str_len = self.file_bytes_with_nul.len() - 1; | ||
// SAFETY: `file_bytes_with_nul` without the trailing nul byte is guaranteed to be | ||
// valid UTF8. | ||
unsafe { crate::str::from_raw_parts(self.file_bytes_with_nul.as_ptr(), str_len) } |
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.
str
is allowed to have null bytes within. So you could keep the type at &'a str
and use &self.file[..self.file.len() -1]
here without unsafe code
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 could! I think that just moves the unsafety into a call to from_bytes_with_nul_unchecked
for the CStr, though, so I think it's roughly the same either way.
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.
That unsafety exists either way unless you just directly encode it as CStr, tho we're still holding out for that becoming a thin pointer, at which point it would be expensive to get the str out of 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.
Maybe it's simpler to just store a raw pointer and length without the nul-terminator included. That way, Location
hold the integer we want most of the time without triggering provenance concerns.
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.
That way,
Location
hold the integer we want most of the time without triggering provenance concerns.
What integer do you mean?
I wasn't aware of any provenance concerns, was that discussed in #131828?
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.
The provenance discussion is in a hidden subthread above: #135054 (comment)
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 mean that storing a raw pointer / length instead of a slice would let us store the length without the nul-terminator included, which makes more sense to me.
Thanks for the correction! I must've misread it when I saw it. In either case, I don't have a strong preference and am happy to remove this one if it makes life easier. I didn't know about the first PR when I wrote this one. |
I closed my original PR in favor of this PR, and opened a new PR #135240 that shows how the alternative approach of adding a compiler flag would look. |
Let's do a perf run to get a concrete binary size overhead (which will of course be so tiny that it's negligible, but having proof of that seems to be necessary in this discussion!). |
This comment has been minimized.
This comment has been minimized.
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.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (35c365c): comparison URL. Overall result: ❌ regressions - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary 9.1%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary -1.7%, secondary -2.6%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResults (primary 0.0%, secondary 0.0%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 763.722s -> 763.688s (-0.00%) |
This is useful for C/C++ APIs which expect the const char* returned from FILE or std::source_location::file_name.