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

Private struct fields by default #13184

Merged
merged 24 commits into from
Apr 1, 2014
Merged

Conversation

alexcrichton
Copy link
Member

This is an implementation of a portion of RFC #4. This PR makes named struct fields private by default (as opposed to inherited by default).

The only real meaty change is the first commit to rustc, all other commits are just fallout of that change.

Summary of changes made:

  • Named fields are private by default everywhere
  • The priv keyword is now default-deny on named fields (done in a "lint" pass in privacy)

Changes yet to be done (before the RFC is closed)

  • Change tuple structs to have private fields by default
  • Remove priv enum variants
  • Make priv a reserved keyword

@lilyball
Copy link
Contributor

Regarding your "Changes yet to be done", the RFC as written does not make any changes to enums. Removing priv enum variants seems out of scope for this and should be considered in a separate RFC.

@alexcrichton
Copy link
Member Author

Ah, sorry about that, I meant to send a separate RFC for that, and I forgot I didn't!

This commit switches privacy's checking of fields to have *all* fields be
private by default. This does not yet change tuple structs, this only affects
structs with named fields. The fallout of this change will follow shortly.

RFC: 0004-private-fields

cc rust-lang#8122
Closes rust-lang#11809
bors added a commit that referenced this pull request Mar 31, 2014
This is an implementation of a portion of [RFC #4](https://github.com/rust-lang/rfcs/blob/master/active/0004-private-fields.md). This PR makes named struct fields private by default (as opposed to inherited by default).

The only real meaty change is the first commit to `rustc`, all other commits are just fallout of that change.

Summary of changes made:

* Named fields are private by default *everywhere*
* The `priv` keyword is now default-deny on named fields (done in a "lint" pass in privacy)

Changes yet to be done (before the RFC is closed)

* Change tuple structs to have private fields by default
* Remove `priv` enum variants
* Make `priv` a reserved keyword
@bors bors closed this Apr 1, 2014
@bors bors merged commit 37a3131 into rust-lang:master Apr 1, 2014
@alexcrichton alexcrichton deleted the priv-fields branch April 1, 2014 00:07
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Apr 1, 2014
This is a continuation of the work done in rust-lang#13184 to make struct fields private
by default. This commit finishes RFC 4 by making all tuple structs have private
fields by default. Note that enum variants are not affected.

A tuple struct having a private field means that it cannot be matched on in a
pattern match (both refutable and irrefutable), and it also cannot have a value
specified to be constructed. Similarly to private fields, switching the type of
a private field in a tuple struct should be able to be done in a backwards
compatible way.

The one snag that I ran into which wasn't mentioned in the RFC is that this
commit also forbids taking the value of a tuple struct constructor. For example,
this code now fails to compile:

    mod a {
        pub struct A(int);
    }

    let a: fn(int) -> a::A = a::A; //~ ERROR: first field is private

Although no fields are bound in this example, it exposes implementation details
through the type itself. For this reason, taking the value of a struct
constructor with private fields is forbidden (outside the containing module).

RFC: 0004-private-fields
@mvdnes
Copy link
Contributor

mvdnes commented Apr 1, 2014

If I am not mistaken, you missed the time module. Currently I cannot get the sec field from a Timespec struct.

@alexcrichton
Copy link
Member Author

Sorry about that! @sfackler beat me to it, and we just need #13240 to merge now.

@mvdnes
Copy link
Contributor

mvdnes commented Apr 1, 2014

Ah great, thanks!

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Apr 4, 2014
The calculation for whether a field is public or private was tweaked in rust-lang#13184,
but I forgot to update rustdoc.

Closes rust-lang#13310
bors added a commit that referenced this pull request Apr 6, 2014
The calculation for whether a field is public or private was tweaked in #13184,
but I forgot to update rustdoc.

Closes #13310
notriddle pushed a commit to notriddle/rust that referenced this pull request Sep 20, 2022
Filter imports on find-all-references

Attempt to rust-lang#13184
Jarcho pushed a commit to Jarcho/rust that referenced this pull request Aug 24, 2024
fix false positive in explicit_iter_loop with msrv

close rust-lang#13184

This PR fix false positive in explicit_iter_loop when msrv < 1.80

changelog: fix false positive in explicit_iter_loop when msrv < 1.80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants