-
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
"impl Copy" can bypass field privacy #128872
Comments
An alternative would be to remove the special rules around implementing This rule would bring it in line with other unsafe marker traits, which can also implemented elsewhere in the same crate. Though it would unnecessarily require |
I think we definitely want to keep the ability to write a safe Being able to write |
Pretty sure we can! We already have checks that all fields are actually copy-able, so also checking that they're visible at the location of the impl doesn't feel too challenging. It also feels very reasonable to do so. I think we should future compat lint this everywhere. Don't have strong opinions whether to increase the severity of the lint in editions @idanarye this is an excellent observation and thanks @RalfJung for moving it into a new issue and pinging t-types/me :3 |
If we had unsafe fields (e.g. because the pointer is unsafe to access/expose) then could the unsafe in |
form an impl perspective, sure |
|
I think making |
What if instead of the rule being " This will mean, of course, that even if a struct only has The advantage of this approach is that it does not rely on the fact that If we go that route, it'd be important to remember (not that it can be forgotten - the standard library will break if this neglected) that the attribute should not prevent implementing owned traits on foreign types from. This does raise a question though - should this attribute restrict that kind of |
|
I'm a bit torn on whether the trait being safe or unsafe to implement should distinguish these cases or not. Even with As another angle, as the concern here is about seeming to violate field privacy, it's not obvious to me that one of the powers of an |
The way I view However, there's probably other ways to view the situation that can lead to different results. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
We don't have negative impls yet, but wouldn't a possible alternative (not necessarily better, but for completeness of design space exploration) to instead require of users to In this particular example, the module defining Again this is for completeness only, because it adds some additional burden on unsafe Rust users, now having to explicitly state that their types are not |
@idanarye makes an excellent point:
I had never realized this, and I think it can be viewed as a violation of our privacy rules: outside modules in the same crate are not able to access the private field, and yet they are able to make the type copyable!
@rust-lang/types Is there any chance we can fix
impl Copy
(over an edition, presumably) so that it is only allowed inside modules where all fields of the type are accessible, i.e., where you could have written the obviousClone
impl that copies all fields?The text was updated successfully, but these errors were encountered: