-
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 #[repr(C)]
to Path
#49456
Add #[repr(C)]
to Path
#49456
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
cc @eddyb |
It would be at least dubious in user code, but libstd is tied to the compiler version anyway so it's allowed to make additional assumptions. I am also uncertain what |
I just wondered how I would handle DSTs myself, that's where I looked first. I figured it would be good to have correct code in the standard library source, otherwise users will copy it and it breaks later. |
I understand, but I am not sure whether we're actually guaranteeing that repr(C) does the right thing in this case. |
Is there any correct way to do this then? |
As I said, I'm really not sure. |
Shouldn't the correct way be |
I don't think anyone has ever thought about what that means for DSTs. FWIW, it's pretty much like repr(C) wrt memory layout (the difference only matters for calling conventions). But besides the layout there's also the question of the metadata (in this case, the length). Or more generally the fact that DST layout is weird and different from non-DST layout. |
Considering how this is part of the standard library, if the compiler were changed so that this didn't work, we'd know about it before that change were even accepted. Which, mind you, would probably break a lot of crates anyway... |
As other people have people out, Since these are DSTs and never passed by value, there is no "call ABI" concern AFAIK, and, in memory, both Rust and C have the same newtype layout (that of the inner type), but it's not clear that we guarantee it on the Rust side, so you can only assume it in an unstable compiler-specific way. But that's still fine, IMO. I don't think we need to do anything specific unless there's any problem. |
Ping from triage, @alexcrichton |
Ah thanks for the ping! It looks like the discussion here is concluding that it's unclear whether this attribute is necessary at this time, right? In light of that I'm personally tempted to close this as "working as intended" and we can revisit it later if it becomes a problem. @torkleyy how does that sound to you? |
Yeah I realized that this PR doesn't make the code any more or less correct than it was. For now I'm just going to use this pattern for my code and make sure I update my code should there ever be a better way to create DSTs. |
Path::new
casts a*const OsStr
to a*const Path
. I don't think this is strictly allowed when using Rust's struct layout.