-
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
Functional record update: private fields should not throw errors if not explicitly used in literals #70564
Comments
The desugaring change would break existing use cases, yes. |
@petrochenkov Interesting. It's hard to understand from the branch code, but is desugaring in your experiment same as I have shown above? I don't really understand why |
Tried to add |
This discussion also recently came up on IRLO again: https://internals.rust-lang.org/t/pre-rfc-relaxed-non-exhaustive-structs/11977
As the linked comment says: "FRU from borrowed contexts, you can’t move the whole value, but can copy individual Copy fields" |
@RalfJung Ah, I see, haven't realised it's possible. It could be interesting to enable this at least for owned case, but I suppose that's what makes feature request more nuanced (which explains previous forum discussions). |
I'll just add to this that if we could use the alternative desugaring, this would also make functional record update syntax work with |
This is a breaking change. See also rust-lang/rust#70564 (comment) for why this change is a little sad.
This is a breaking change. See also rust-lang/rust#70564 (comment) for why this change is a little sad.
Would it be feasible to make this work without fundamentally changing the desugaring? I.e. somehow "ignore" the non-exhaustiveness of the struct (and privacy of implicitly-copied / -moved fields) when it is used with FRU? |
Also either this or #63538 should probably be closed as a duplicate. |
There are several threads on Rust internals discussing this problem:
There is mention of an RFC to introduce a new syntax, so that existing uses of @starkat99 do you think you could create the RFC you proposed back then? |
Another option suggested by @cchiw is to allow the normal functional record update syntax on non_exhaustive structs and structs with private fields if they implement Two other solutions involving syntax are:
CC @Manishearth |
I like the idea of introducing a minimal version of this feature which only allows the syntax on non-exhaustive structs with private fields if they are all // Present
let mut config = Config::default();
config.a = 123;
use(config);
// Future
use(Config { a: 123, ..Default::default()); The real question is, though, is how intuitive and, conversely, how surprising such semantics would be to an average user. I could imagine such a user running into the |
Could this be a use case for supporting |
I tried this code:
I expected to see this happen: code compiles successfully.
Instead, this happened: rustc throws an error
As a user, I field this confusing, because I'm not trying to access or set the private field explicitly anywhere in the literal.
Meta
Applies to all known rustc versions.
I understand why this happens in principle, if the literal is desugared literally (no pun intended) to:
However, it seems that this code could be equally expanded to:
This way an immutable literal could be used with many more kinds of structures without users manually resorting to the temporarily-mutable variable as in the 2nd example.
Do I miss some other difference between the two that would break some existing usecases if the implementation is changed?
The text was updated successfully, but these errors were encountered: