Skip to content

Commit 02674ce

Browse files
authored
Merge pull request #1040 from ehuss/scopes
Add "scopes" chapter.
2 parents 0e07fc5 + 8251778 commit 02674ce

File tree

8 files changed

+376
-14
lines changed

8 files changed

+376
-14
lines changed

src/expressions/block-expr.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
A *block expression*, or *block*, is a control flow expression and anonymous namespace scope for items and variable declarations.
1616
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
1717
As an anonymous namespace scope, item declarations are only in scope inside the block itself and variables declared by `let` statements are in scope from the next statement until the end of the block.
18+
See the [scopes] chapter for more details.
1819

1920
The syntax for a block is `{`, then any [inner attributes], then any number of [statements], then an optional expression, called the final operand, and finally a `}`.
2021

@@ -243,6 +244,7 @@ fn is_unix_platform() -> bool {
243244
[inner attributes]: ../attributes.md
244245
[method]: ../items/associated-items.md#methods
245246
[mutable reference]: ../types/pointer.md#mutables-references-
247+
[scopes]: ../names/scopes.md
246248
[shared references]: ../types/pointer.md#shared-references-
247249
[statement]: ../statements.md
248250
[statements]: ../statements.md

src/items.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ There are several kinds of items:
5353
* [implementations]
5454
* [`extern` blocks]
5555

56-
Some items form an implicit scope for the declaration of sub-items. In other
57-
words, within a function or module, declarations of items can (in many cases)
58-
be mixed with the statements, control blocks, and similar artifacts that
59-
otherwise compose the item body. The meaning of these scoped items is the same
60-
as if the item was declared outside the scope — it is still a static item
61-
— except that the item's *path name* within the module namespace is
62-
qualified by the name of the enclosing item, or is private to the enclosing
63-
item (in the case of functions). The grammar specifies the exact locations in
64-
which sub-item declarations may appear.
56+
Items may be declared in the [root of the crate], a [module][modules], or a [block expression].
57+
A subset of items, called [associated items], may be declared in [traits] and [implementations].
58+
A subset of items, called external items, may be declared in [`extern` blocks].
59+
60+
Items may be defined in any order, with the exception of [`macro_rules`] which has its own scoping behavior.
61+
[Name resolution] of item names allows items to be defined before or after where the item is referred to in the module or block.
62+
63+
See [item scopes] for information on the scoping rules of items.
6564

6665
[_ConstantItem_]: items/constant-items.md
6766
[_Enumeration_]: items/enumerations.md
@@ -82,15 +81,23 @@ which sub-item declarations may appear.
8281
[_Visibility_]: visibility-and-privacy.md
8382
[`extern crate` declarations]: items/extern-crates.md
8483
[`extern` blocks]: items/external-blocks.md
84+
[`macro_rules`]: macros-by-example.md
8585
[`use` declarations]: items/use-declarations.md
86+
[associated items]: items/associated-items.md
87+
[block expression]: expressions/block-expr.md
8688
[constant items]: items/constant-items.md
8789
[enumeration definitions]: items/enumerations.md
8890
[function definitions]: items/functions.md
8991
[implementations]: items/implementations.md
92+
[item scopes]: names/scopes.md#item-scopes
9093
[modules]: items/modules.md
94+
[name resolution]: names/name-resolution.md
9195
[paths]: paths.md
96+
[root of the crate]: crates-and-source-files.md
97+
[statement]: statements.md
9298
[static items]: items/static-items.md
9399
[struct definitions]: items/structs.md
94100
[trait definitions]: items/traits.md
101+
[traits]: items/traits.md
95102
[type definitions]: items/type-aliases.md
96103
[union definitions]: items/unions.md

src/items/generics.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ parameters are listed in angle <span class="parenthetical">brackets (`<...>`)</s
2323
usually immediately after the name of the item and before its definition. For
2424
implementations, which don't have a name, they come directly after `impl`.
2525
The order of generic parameters is restricted to lifetime parameters and then type and const parameters intermixed.
26+
The same parameter name may not be declared more than once in a _GenericParams_ list.
2627

2728
Some examples of items with type, const, and lifetime parameters:
2829

@@ -37,6 +38,7 @@ struct EitherOrderWorks<const N: bool, U>(U);
3738
Generic parameters are in scope within the item definition where they are
3839
declared. They are not in scope for items declared within the body of a
3940
function as described in [item declarations].
41+
See [generic parameter scopes] for more details.
4042

4143
[References], [raw pointers], [arrays], [slices], [tuples], and
4244
[function pointers] have lifetime or type parameters as well, but are not
@@ -286,6 +288,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
286288
[functions]: functions.md
287289
[function pointers]: ../types/function-pointer.md
288290
[generic implementations]: implementations.md#generic-implementations
291+
[generic parameter scopes]: ../names/scopes.md#generic-parameter-scopes
289292
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
290293
[implementations]: implementations.md
291294
[item declarations]: ../statements.md#item-declarations

src/names.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Some entities are [explicitly declared](#explicitly-declared-entities) in the
1313
source code, and some are [implicitly declared](#implicitly-declared-entities)
1414
as part of the language or compiler extensions.
1515

16-
[*Paths*] are used to refer to an entity, possibly in another scope. Lifetimes
16+
[*Paths*] are used to refer to an entity, possibly in another module or type. Lifetimes
1717
and loop labels use a [dedicated syntax][lifetimes-and-loop-labels] using a
1818
leading quote.
1919

0 commit comments

Comments
 (0)