Skip to content

Commit eabdf09

Browse files
committed
Start documenting name resolution.
1 parent fb1ce8a commit eabdf09

15 files changed

+564
-92
lines changed

src/SUMMARY.md

Lines changed: 8 additions & 2 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 16 additions & 26 deletions
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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ 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 [type], [item], [generic parameter], [variable binding],
67+
[loop label], [lifetime], [field], or [attribute].
68+
6469
### Expression
6570

6671
An expression is a combination of values, constants, variables, operators
@@ -123,6 +128,24 @@ This is not affected by applied type arguments. `struct Foo` is considered local
123128
`Vec<Foo>` is not. `LocalType<ForeignType>` is local. Type aliases do not
124129
affect locality.
125130

131+
### Name
132+
133+
A [*name*] is an [identifier] or [lifetime or loop label] that refers to an
134+
[entity](#entity). A *name binding* is when an entity declaration introduces
135+
an identifier or label associated with that entity. [Paths],
136+
identifiers, and labels are used to refer to an entity.
137+
138+
### Name resolution
139+
140+
[*Name resolution*] is the compile-time process of tying [paths],
141+
[identifiers], and [labels] to [entity](#entity) declarations.
142+
143+
### Namespace
144+
145+
A [*namespace*] is a collection of uniquely named [entities](#entity).
146+
Namespaces can be organized in a hierarchy, where each level of the hierarchy
147+
has its own collection of named entities.
148+
126149
### Nominal types
127150

128151
Types that can be referred to by a path directly. Specifically [enums],
@@ -133,11 +156,22 @@ Types that can be referred to by a path directly. Specifically [enums],
133156
[Traits] that can be used as [trait objects]. Only traits that follow specific
134157
[rules][object safety] are object safe.
135158

159+
### Path
160+
161+
A [*path*] is a sequence of one or more path segments used to refer to an
162+
[entity](#entity) in the current scope or other levels of a
163+
[namespace](#namespace) hierarchy.
164+
136165
### Prelude
137166

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

170+
### Scope
171+
172+
A [*scope*] is the region of source text where a named [entity](#entity) may
173+
be referenced with that name.
174+
141175
### Scrutinee
142176

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

217251
[alignment]: type-layout.md#size-and-alignment
218252
[associated item]: #associated-item
253+
[attribute]: attributes.md
254+
[*entity*]: names.md
219255
[enums]: items/enumerations.md
256+
[field]: expressions/field-expr.md
220257
[free item]: #free-item
258+
[generic parameter]: items/generics.md
259+
[identifier]: identifiers.md
260+
[identifiers]: identifiers.md
221261
[implementation]: items/implementations.md
222262
[implementations]: items/implementations.md
223263
[inherent implementation]: items/implementations.md#inherent-implementations
224264
[item]: items.md
265+
[item]: items.md
266+
[labels]: tokens.md#lifetimes-and-loop-labels
267+
[lifetime or loop label]: tokens.md#lifetimes-and-loop-labels
268+
[lifetime]: tokens.md#lifetimes-and-loop-labels
269+
[loop label]: tokens.md#lifetimes-and-loop-labels
225270
[method]: items/associated-items.md#methods
271+
[*Name resolution*]: names/name-resolution.md
272+
[*name*]: names.md
273+
[*namespace*]: names/namespaces.md
226274
[never type]: types/never.md
227275
[object safety]: items/traits.md#object-safety
276+
[*path*]: paths.md
277+
[Paths]: paths.md
278+
[*scope*]: names/scopes.md
228279
[structs]: items/structs.md
229280
[trait objects]: types/trait-object.md
230281
[traits]: items/traits.md
282+
[type]: types.md
231283
[undefined-behavior]: behavior-considered-undefined.md
232284
[unions]: items/unions.md
285+
[variable binding]: patterns.md

src/items/extern-crates.md

Lines changed: 21 additions & 40 deletions
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

@@ -105,8 +76,18 @@ 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+
81+
<script>
82+
(function() {
83+
var fragments = {
84+
"#extern-prelude": "../names/preludes.html#extern-prelude",
85+
};
86+
var target = fragments[window.location.hash];
87+
if (target) {
88+
var url = window.location.toString();
89+
var base = url.substring(0, url.lastIndexOf('/'));
90+
window.location.replace(base + "/" + target);
91+
}
92+
})();
93+
</script>

src/items/modules.md

Lines changed: 18 additions & 13 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

src/macros-by-example.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ m!();
301301

302302
Second, it can be used to import macros from another crate, by attaching it to
303303
an `extern crate` declaration appearing in the crate's root module. Macros
304-
imported this way are imported into the prelude of the crate, not textually,
304+
imported this way are imported into the [`macro_use` prelude], not textually,
305305
which means that they can be shadowed by any other name. While macros imported
306306
by `#[macro_use]` can be used before the import statement, in case of a
307307
conflict, the last macro imported wins. Optionally, a list of macros to import
@@ -497,3 +497,4 @@ For more detail, see the [formal specification].
497497
[_Visibility_]: visibility-and-privacy.md
498498
[formal specification]: macro-ambiguity.md
499499
[token]: tokens.md
500+
[`macro_use` prelude]: names/preludes.md#macro_use-prelude

0 commit comments

Comments
 (0)