Skip to content

Commit 58cb78f

Browse files
authored
Merge pull request #1627 from chorman0773/spec-add-identifiers-visibility-and-privacy
Add identifier syntax to visibility-and-privacy.md
2 parents 24fb268 + 67426bc commit 58cb78f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/visibility-and-privacy.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Visibility and Privacy
22

3+
r[vis]
4+
5+
r[vis.syntax]
36
> **<sup>Syntax<sup>**\
47
> _Visibility_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; `pub`\
@@ -8,20 +11,24 @@
811
> &nbsp;&nbsp; | `pub` `(` `super` `)`\
912
> &nbsp;&nbsp; | `pub` `(` `in` [_SimplePath_] `)`
1013
14+
r[vis.intro]
1115
These two terms are often used interchangeably, and what they are attempting to
1216
convey is the answer to the question "Can this item be used at this location?"
1317

18+
r[vis.name-hierarchy]
1419
Rust's name resolution operates on a global hierarchy of namespaces. Each level
1520
in the hierarchy can be thought of as some item. The items are one of those
1621
mentioned above, but also include external crates. Declaring or defining a new
1722
module can be thought of as inserting a new tree into the hierarchy at the
1823
location of the definition.
1924

25+
r[vis.privacy]
2026
To control whether interfaces can be used across modules, Rust checks each use
2127
of an item to see whether it should be allowed or not. This is where privacy
2228
warnings are generated, or otherwise "you used a private item of another module
2329
and weren't allowed to."
2430

31+
r[vis.default]
2532
By default, everything is *private*, with two exceptions: Associated
2633
items in a `pub` Trait are public by default; Enum variants
2734
in a `pub` enum are also public by default. When an item is declared as `pub`,
@@ -44,6 +51,7 @@ pub enum State {
4451
}
4552
```
4653

54+
r[vis.access]
4755
With the notion of an item being either public or private, Rust allows item
4856
accesses in two cases:
4957

@@ -78,8 +86,10 @@ explain, here's a few use cases and what they would entail:
7886

7987
In the second case, it mentions that a private item "can be accessed" by the
8088
current module and its descendants, but the exact meaning of accessing an item
81-
depends on what the item is. Accessing a module, for example, would mean
82-
looking inside of it (to import more items). On the other hand, accessing a
89+
depends on what the item is.
90+
91+
r[vis.usage]
92+
Accessing a module, for example, would mean looking inside of it (to import more items). On the other hand, accessing a
8393
function would mean that it is invoked. Additionally, path expressions and
8494
import statements are considered to access an item in the sense that the
8595
import/expression is only valid if the destination is in the current visibility
@@ -144,18 +154,30 @@ expressions, types, etc.
144154

145155
## `pub(in path)`, `pub(crate)`, `pub(super)`, and `pub(self)`
146156

157+
r[vis.scoped]
158+
159+
r[vis.scoped.intro]
147160
In addition to public and private, Rust allows users to declare an item as
148161
visible only within a given scope. The rules for `pub` restrictions are as
149162
follows:
163+
164+
r[vis.scoped.in]
150165
- `pub(in path)` makes an item visible within the provided `path`.
151166
`path` must be a simple path which resolves to an ancestor module of the item whose visibility is being declared.
152167
Each identifier in `path` must refer directly to a module (not to a name introduced by a `use` statement).
168+
169+
r[vis.scoped.crate]
153170
- `pub(crate)` makes an item visible within the current crate.
171+
172+
r[vis.scoped.super]
154173
- `pub(super)` makes an item visible to the parent module. This is equivalent
155174
to `pub(in super)`.
175+
176+
r[vis.scoped.self]
156177
- `pub(self)` makes an item visible to the current module. This is equivalent
157178
to `pub(in self)` or not using `pub` at all.
158179

180+
r[vis.scoped.edition2018]
159181
> **Edition differences**: Starting with the 2018 edition, paths for
160182
> `pub(in path)` must start with `crate`, `self`, or `super`. The 2015 edition
161183
> may also use paths starting with `::` or modules from the crate root.
@@ -219,6 +241,9 @@ fn main() { bar() }
219241
220242
## Re-exporting and Visibility
221243

244+
r[vis.reexports]
245+
246+
r[vis.reexports.intro]
222247
Rust allows publicly re-exporting items through a `pub use` directive. Because
223248
this is a public directive, this allows the item to be used in the current
224249
module through the rules above. It essentially allows public access into the
@@ -239,6 +264,7 @@ mod implementation {
239264
This means that any external crate referencing `implementation::api::f` would
240265
receive a privacy violation, while the path `api::f` would be allowed.
241266

267+
r[vis.reexports.private-item]
242268
When re-exporting a private item, it can be thought of as allowing the "privacy
243269
chain" being short-circuited through the reexport instead of passing through
244270
the namespace hierarchy as it normally would.

0 commit comments

Comments
 (0)