Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

cramertj
Copy link
Member

@cramertj cramertj commented Jan 3, 2025

This is useful for C/C++ APIs which expect the const char* returned from FILE or std::source_location::file_name.

@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2025

r? @cuviper

rustbot has assigned @cuviper.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2025

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

@Noratrieb
Copy link
Member

see rust-lang/libs-team#466

compiler/rustc_const_eval/src/interpret/place.rs Outdated Show resolved Hide resolved
/// 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*`.
Copy link
Member

@RalfJung RalfJung Jan 3, 2025

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.

Copy link
Member Author

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!

@cramertj
Copy link
Member Author

cramertj commented Jan 3, 2025

see rust-lang/libs-team#466

Renamed to file_with_nul as per that proposal.

@cramertj cramertj changed the title Add Location::file_cstr Add Location::file_with_nul Jan 3, 2025
This is useful for C/C++ APIs which expect the const char* returned
from __FILE__ or std::source_location::file_name.
@bjorn3
Copy link
Member

bjorn3 commented Jan 5, 2025

How does this compare with #131828?

@cramertj
Copy link
Member Author

cramertj commented Jan 6, 2025

@bjorn3 This is essentially the same feature, although this implementation doesn't require an increase to the size of the Location struct itself (and could actually be used to decrease it by dropping the size of the slice if we cared to do so).

@Darksonn
Copy link
Contributor

Darksonn commented Jan 7, 2025

The other PR does not change the size of the Location struct either.

Comment on lines +147 to +150
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) }
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Member

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)

Copy link
Contributor

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.

@cramertj
Copy link
Member Author

cramertj commented Jan 7, 2025

The other PR does not change the size of the Location struct either.

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.

@Darksonn
Copy link
Contributor

Darksonn commented Jan 8, 2025

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.

@Noratrieb
Copy link
Member

Noratrieb commented Jan 8, 2025

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!).
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 8, 2025
@bors
Copy link
Contributor

bors commented Jan 8, 2025

⌛ Trying commit 73810aa with merge 35c365c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 8, 2025
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.
@bors
Copy link
Contributor

bors commented Jan 8, 2025

☀️ Try build successful - checks-actions
Build commit: 35c365c (35c365c6fce43dba6f1e28794102c6f406ba02d9)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (35c365c): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking 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
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.2%, 0.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

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.

mean range count
Regressions ❌
(primary)
9.1% [9.1%, 9.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 9.1% [9.1%, 9.1%] 1

Cycles

Results (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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.7% [-3.3%, -0.8%] 14
Improvements ✅
(secondary)
-2.6% [-3.8%, -1.3%] 7
All ❌✅ (primary) -1.7% [-3.3%, -0.8%] 14

Binary size

Results (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.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 22
Regressions ❌
(secondary)
0.0% [0.0%, 0.1%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.0%] 22

Bootstrap: 763.722s -> 763.688s (-0.00%)
Artifact size: 325.68 MiB -> 325.70 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants