Skip to content

Commit 1f78215

Browse files
authored
Merge pull request #937 from ehuss/names
Start documenting name resolution.
2 parents fb1ce8a + a989af0 commit 1f78215

15 files changed

+606
-93
lines changed

src/SUMMARY.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
- [Comments](comments.md)
1212
- [Whitespace](whitespace.md)
1313
- [Tokens](tokens.md)
14-
- [Paths](paths.md)
1514

1615
- [Macros](macros.md)
1716
- [Macros By Example](macros-by-example.md)
@@ -37,7 +36,6 @@
3736
- [External blocks](items/external-blocks.md)
3837
- [Generic parameters](items/generics.md)
3938
- [Associated Items](items/associated-items.md)
40-
- [Visibility and Privacy](visibility-and-privacy.md)
4139

4240
- [Attributes](attributes.md)
4341
- [Testing](attributes/testing.md)
@@ -103,6 +101,14 @@
103101

104102
- [Special types and traits](special-types-and-traits.md)
105103

104+
- [Names](names.md)
105+
- [Namespaces](names/namespaces.md)
106+
- [Scopes](names/scopes.md)
107+
- [Preludes](names/preludes.md)
108+
- [Paths](paths.md)
109+
- [Name resolution](names/name-resolution.md)
110+
- [Visibility and privacy](visibility-and-privacy.md)
111+
106112
- [Memory model](memory-model.md)
107113
- [Memory allocation and lifetime](memory-allocation-and-lifetime.md)
108114
- [Memory ownership](memory-ownership.md)

src/attributes.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ active. All other attributes are inert.
150150
## Tool attributes
151151

152152
The compiler may allow attributes for external tools where each tool resides
153-
in its own namespace. The first segment of the attribute path is the name of
154-
the tool, with one or more additional segments whose interpretation is up to
155-
the tool.
153+
in its own namespace in the [tool prelude]. The first segment of the attribute
154+
path is the name of the tool, with one or more additional segments whose
155+
interpretation is up to the tool.
156156

157157
When a tool is not in use, the tool's attributes are accepted without a
158158
warning. When the tool is in use, the tool is responsible for processing and
@@ -279,11 +279,11 @@ The following is an index of all built-in attributes.
279279
[`macro_use`]: macros-by-example.md#the-macro_use-attribute
280280
[`must_use`]: attributes/diagnostics.md#the-must_use-attribute
281281
[`no_builtins`]: attributes/codegen.md#the-no_builtins-attribute
282-
[`no_implicit_prelude`]: items/modules.md#prelude-items
282+
[`no_implicit_prelude`]: names/preludes.md#the-no_implicit_prelude-attribute
283283
[`no_link`]: items/extern-crates.md#the-no_link-attribute
284284
[`no_main`]: crates-and-source-files.md#the-no_main-attribute
285285
[`no_mangle`]: abi.md#the-no_mangle-attribute
286-
[`no_std`]: crates-and-source-files.md#preludes-and-no_std
286+
[`no_std`]: names/preludes.md#the-no_std-attribute
287287
[`non_exhaustive`]: attributes/type_system.md#the-non_exhaustive-attribute
288288
[`panic_handler`]: runtime.md#the-panic_handler-attribute
289289
[`path`]: items/modules.md#the-path-attribute
@@ -315,6 +315,7 @@ The following is an index of all built-in attributes.
315315
[modules]: items/modules.md
316316
[statements]: statements.md
317317
[struct]: items/structs.md
318+
[tool prelude]: names/preludes.md#tool-prelude
318319
[union]: items/unions.md
319320
[closure]: expressions/closure-expr.md
320321
[function pointer]: types/function-pointer.md

src/crates-and-source-files.md

+16-26
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,8 @@ not treated as a shebang, but instead as the start of an attribute.
9595

9696
## Preludes and `no_std`
9797

98-
All crates have a *prelude* that automatically inserts names from a specific
99-
module, the *prelude module*, into scope of each [module] and an [`extern
100-
crate`] into the crate root module. By default, the *standard prelude* is used.
101-
The linked crate is [`std`] and the prelude module is [`std::prelude::v1`].
102-
103-
The prelude can be changed to the *core prelude* by using the `no_std`
104-
[attribute] on the root crate module. The linked crate is [`core`] and the
105-
prelude module is [`core::prelude::v1`]. Using the core prelude over the
106-
standard prelude is useful when either the crate is targeting a platform that
107-
does not support the standard library or is purposefully not using the
108-
capabilities of the standard library. Those capabilities are mainly dynamic
109-
memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g.
110-
`std::fs` and `std::io`).
111-
112-
<div class="warning">
113-
114-
Warning: Using `no_std` does not prevent the standard library from being linked
115-
in. It is still valid to put `extern crate std;` into the crate and dependencies
116-
can also link it in.
117-
118-
</div>
98+
This section has been moved to the [Preludes chapter](names/preludes.md).
99+
<!-- this is to appease the linkchecker, will remove once other books are updated -->
119100

120101
## Main Functions
121102

@@ -168,11 +149,6 @@ or `-` (U+002D) characters.
168149
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
169150
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
170151
[`Termination`]: ../std/process/trait.Termination.html
171-
[`core`]: ../core/index.html
172-
[`core::prelude::v1`]: ../core/prelude/index.html
173-
[`extern crate`]: items/extern-crates.md
174-
[`std`]: ../std/index.html
175-
[`std::prelude::v1`]: ../std/prelude/index.html
176152
[attribute]: attributes.md
177153
[attributes]: attributes.md
178154
[comments]: comments.md
@@ -182,3 +158,17 @@ or `-` (U+002D) characters.
182158
[trait or lifetime bounds]: trait-bounds.md
183159
[where clauses]: items/generics.md#where-clauses
184160
[whitespace]: whitespace.md
161+
162+
<script>
163+
(function() {
164+
var fragments = {
165+
"#preludes-and-no_std": "names/preludes.html",
166+
};
167+
var target = fragments[window.location.hash];
168+
if (target) {
169+
var url = window.location.toString();
170+
var base = url.substring(0, url.lastIndexOf('/'));
171+
window.location.replace(base + "/" + target);
172+
}
173+
})();
174+
</script>

src/glossary.md

+60
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ through a mechanism called ‘trait objects’.
6161

6262
A dynamically sized type (DST) is a type without a statically known size or alignment.
6363

64+
### Entity
65+
66+
An [*entity*] is a language construct that can be referred to in some way
67+
within the source program, usually via a [path][paths]. Entities include
68+
[types], [items], [generic parameters], [variable bindings], [loop labels],
69+
[lifetimes], [fields], [attributes], and [lints].
70+
6471
### Expression
6572

6673
An expression is a combination of values, constants, variables, operators
@@ -123,6 +130,28 @@ This is not affected by applied type arguments. `struct Foo` is considered local
123130
`Vec<Foo>` is not. `LocalType<ForeignType>` is local. Type aliases do not
124131
affect locality.
125132

133+
### Name
134+
135+
A [*name*] is an [identifier] or [lifetime or loop label] that refers to an
136+
[entity](#entity). A *name binding* is when an entity declaration introduces
137+
an identifier or label associated with that entity. [Paths],
138+
identifiers, and labels are used to refer to an entity.
139+
140+
### Name resolution
141+
142+
[*Name resolution*] is the compile-time process of tying [paths],
143+
[identifiers], and [labels] to [entity](#entity) declarations.
144+
145+
### Namespace
146+
147+
A *namespace* is a logical grouping of declared [names](#name) based on the
148+
kind of [entity](#entity) the name refers to. Namespaces allow the occurrence
149+
of a name in one namespace to not conflict with the same name in another
150+
namespace.
151+
152+
Within a namespace, names are organized in a hierarchy, where each level of
153+
the hierarchy has its own collection of named entities.
154+
126155
### Nominal types
127156

128157
Types that can be referred to by a path directly. Specifically [enums],
@@ -133,11 +162,22 @@ Types that can be referred to by a path directly. Specifically [enums],
133162
[Traits] that can be used as [trait objects]. Only traits that follow specific
134163
[rules][object safety] are object safe.
135164

165+
### Path
166+
167+
A [*path*] is a sequence of one or more path segments used to refer to an
168+
[entity](#entity) in the current scope or other levels of a
169+
[namespace](#namespace) hierarchy.
170+
136171
### Prelude
137172

138173
Prelude, or The Rust Prelude, is a small collection of items - mostly traits - that are
139174
imported into every module of every crate. The traits in the prelude are pervasive.
140175

176+
### Scope
177+
178+
A [*scope*] is the region of source text where a named [entity](#entity) may
179+
be referenced with that name.
180+
141181
### Scrutinee
142182

143183
A scrutinee is the expression that is matched on in `match` expressions and
@@ -216,17 +256,37 @@ example of an uninhabited type is the [never type] `!`, or an enum with no varia
216256

217257
[alignment]: type-layout.md#size-and-alignment
218258
[associated item]: #associated-item
259+
[attributes]: attributes.md
260+
[*entity*]: names.md
219261
[enums]: items/enumerations.md
262+
[fields]: expressions/field-expr.md
220263
[free item]: #free-item
264+
[generic parameters]: items/generics.md
265+
[identifier]: identifiers.md
266+
[identifiers]: identifiers.md
221267
[implementation]: items/implementations.md
222268
[implementations]: items/implementations.md
223269
[inherent implementation]: items/implementations.md#inherent-implementations
224270
[item]: items.md
271+
[items]: items.md
272+
[labels]: tokens.md#lifetimes-and-loop-labels
273+
[lifetime or loop label]: tokens.md#lifetimes-and-loop-labels
274+
[lifetimes]: tokens.md#lifetimes-and-loop-labels
275+
[lints]: attributes/diagnostics.md#lint-check-attributes
276+
[loop labels]: tokens.md#lifetimes-and-loop-labels
225277
[method]: items/associated-items.md#methods
278+
[*Name resolution*]: names/name-resolution.md
279+
[*name*]: names.md
280+
[*namespace*]: names/namespaces.md
226281
[never type]: types/never.md
227282
[object safety]: items/traits.md#object-safety
283+
[*path*]: paths.md
284+
[Paths]: paths.md
285+
[*scope*]: names/scopes.md
228286
[structs]: items/structs.md
229287
[trait objects]: types/trait-object.md
230288
[traits]: items/traits.md
289+
[types]: types.md
231290
[undefined-behavior]: behavior-considered-undefined.md
232291
[unions]: items/unions.md
292+
[variable bindings]: patterns.md

src/items/extern-crates.md

+23-41
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
1313
An _`extern crate` declaration_ specifies a dependency on an external crate.
1414
The external crate is then bound into the declaring scope as the [identifier]
15-
provided in the `extern crate` declaration. The `as` clause can be used to
16-
bind the imported crate to a different name.
15+
provided in the `extern crate` declaration. Additionally, if the `extern
16+
crate` appears in the crate root, then the crate name is also added to the
17+
[extern prelude], making it automatically in scope in all modules. The `as`
18+
clause can be used to bind the imported crate to a different name.
1719

1820
The external crate is resolved to a specific `soname` at compile time, and a
1921
runtime linkage requirement to that `soname` is passed to the linker for
@@ -52,39 +54,8 @@ extern crate hello_world; // hyphen replaced with an underscore
5254

5355
## Extern Prelude
5456

55-
External crates imported with `extern crate` in the root module or provided to
56-
the compiler (as with the `--extern` flag with `rustc`) are added to the
57-
"extern prelude". Crates in the extern prelude are in scope in the entire
58-
crate, including inner modules. If imported with `extern crate orig_name as
59-
new_name`, then the symbol `new_name` is instead added to the prelude.
60-
61-
The `core` crate is always added to the extern prelude. The `std` crate
62-
is added as long as the [`no_std`] attribute is not specified in the crate root.
63-
64-
The [`no_implicit_prelude`] attribute can be used on a module to disable
65-
prelude lookups within that module.
66-
67-
> **Edition Differences**: In the 2015 edition, crates in the extern prelude
68-
> cannot be referenced via [use declarations], so it is generally standard
69-
> practice to include `extern crate` declarations to bring them into scope.
70-
>
71-
> Beginning in the 2018 edition, [use declarations] can reference crates in
72-
> the extern prelude, so it is considered unidiomatic to use `extern crate`.
73-
74-
> **Note**: Additional crates that ship with `rustc`, such as [`alloc`], and
75-
> [`test`], are not automatically included with the `--extern` flag when using
76-
> Cargo. They must be brought into scope with an `extern crate` declaration,
77-
> even in the 2018 edition.
78-
>
79-
> ```rust
80-
> extern crate alloc;
81-
> use alloc::rc::Rc;
82-
> ```
83-
84-
<!--
85-
See https://github.com/rust-lang/rust/issues/57288 for more about the
86-
alloc/test limitation.
87-
-->
57+
This section has been moved to [Preludes — Extern Prelude](../names/preludes.md#extern-prelude).
58+
<!-- this is to appease the linkchecker, will remove once other books are updated -->
8859

8960
## Underscore Imports
9061

@@ -94,7 +65,7 @@ useful for crates that only need to be linked, but are never referenced, and
9465
will avoid being reported as unused.
9566

9667
The [`macro_use` attribute] works as usual and import the macro names
97-
into the macro-use prelude.
68+
into the [`macro_use` prelude].
9869

9970
## The `no_link` attribute
10071

@@ -105,8 +76,19 @@ crate to access only its macros.
10576
[IDENTIFIER]: ../identifiers.md
10677
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
10778
[`macro_use` attribute]: ../macros-by-example.md#the-macro_use-attribute
108-
[`alloc`]: https://doc.rust-lang.org/alloc/
109-
[`no_implicit_prelude`]: modules.md#prelude-items
110-
[`no_std`]: ../crates-and-source-files.md#preludes-and-no_std
111-
[`test`]: https://doc.rust-lang.org/test/
112-
[use declarations]: use-declarations.md
79+
[extern prelude]: ../names/preludes.md#extern-prelude
80+
[`macro_use` prelude]: ../names/preludes.md#macro_use-prelude
81+
82+
<script>
83+
(function() {
84+
var fragments = {
85+
"#extern-prelude": "../names/preludes.html#extern-prelude",
86+
};
87+
var target = fragments[window.location.hash];
88+
if (target) {
89+
var url = window.location.toString();
90+
var base = url.substring(0, url.lastIndexOf('/'));
91+
window.location.replace(base + "/" + target);
92+
}
93+
})();
94+
</script>

src/items/modules.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -128,34 +128,39 @@ mod thread {
128128
}
129129
```
130130

131-
## Prelude Items
132-
133-
Modules implicitly have some names in scope. These name are to built-in types,
134-
macros imported with [`#[macro_use]`][macro_use] on an extern crate, and by the crate's
135-
[prelude]. These names are all made of a single identifier. These names are not
136-
part of the module, so for example, any name `name`, `self::name` is not a
137-
valid path. The names added by the [prelude] can be removed by placing the
138-
`no_implicit_prelude` [attribute] onto the module or one of its ancestor modules.
139-
140131
## Attributes on Modules
141132

142133
Modules, like all items, accept outer attributes. They also accept inner
143134
attributes: either after `{` for a module with a body, or at the beginning of the
144135
source file, after the optional BOM and shebang.
145136

146137
The built-in attributes that have meaning on a module are [`cfg`],
147-
[`deprecated`], [`doc`], [the lint check attributes], `path`, and
148-
`no_implicit_prelude`. Modules also accept macro attributes.
138+
[`deprecated`], [`doc`], [the lint check attributes], [`path`], and
139+
[`no_implicit_prelude`]. Modules also accept macro attributes.
149140

150141
[_InnerAttribute_]: ../attributes.md
151142
[_Item_]: ../items.md
152-
[macro_use]: ../macros-by-example.md#the-macro_use-attribute
153143
[`cfg`]: ../conditional-compilation.md
154144
[`deprecated`]: ../attributes/diagnostics.md#the-deprecated-attribute
155145
[`doc`]: ../../rustdoc/the-doc-attribute.html
146+
[`no_implicit_prelude`]: ../names/preludes.md#the-no_implicit_prelude-attribute
147+
[`path`]: #the-path-attribute
156148
[IDENTIFIER]: ../identifiers.md
157149
[attribute]: ../attributes.md
158150
[items]: ../items.md
159151
[module path]: ../paths.md
160-
[prelude]: ../crates-and-source-files.md#preludes-and-no_std
161152
[the lint check attributes]: ../attributes/diagnostics.md#lint-check-attributes
153+
154+
<script>
155+
(function() {
156+
var fragments = {
157+
"#prelude-items": "../names/preludes.html",
158+
};
159+
var target = fragments[window.location.hash];
160+
if (target) {
161+
var url = window.location.toString();
162+
var base = url.substring(0, url.lastIndexOf('/'));
163+
window.location.replace(base + "/" + target);
164+
}
165+
})();
166+
</script>

src/items/use-declarations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,5 @@ m!(use std as _;);
202202
[IDENTIFIER]: ../identifiers.md
203203
[_SimplePath_]: ../paths.md#simple-paths
204204
[`extern crate`]: extern-crates.md
205-
[extern prelude]: extern-crates.md#extern-prelude
205+
[extern prelude]: ../names/preludes.md#extern-prelude
206206
[path qualifiers]: ../paths.md#path-qualifiers

src/keywords.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ is possible to declare a variable or method with the name `union`.
9797

9898
* `union` is used to declare a [union] and is only a keyword when used in a
9999
union declaration.
100-
* `'static` is used for the static lifetime and cannot be used as a generic
101-
lifetime parameter
100+
* `'static` is used for the static lifetime and cannot be used as a [generic
101+
lifetime parameter] or [loop label]
102102

103103
```compile_fail
104104
// error[E0262]: invalid lifetime parameter name: `'static`
@@ -127,3 +127,5 @@ is possible to declare a variable or method with the name `union`.
127127
[union]: items/unions.md
128128
[variants]: items/enumerations.md
129129
[`dyn`]: types/trait-object.md
130+
[loop label]: expressions/loop-expr.md#loop-labels
131+
[generic lifetime parameter]: items/generics.md

0 commit comments

Comments
 (0)