Skip to content

Commit 524b9b1

Browse files
committed
Updates from review.
1 parent 57d286f commit 524b9b1

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

Diff for: src/items.md

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

56-
Items may be declared in the [root of the crate], a [module][modules], or a [statement].
57-
Additionally, a subset of items, called [associated items], may be declared in [traits] and [implementations].
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].
5859

5960
Items may be defined in any order, with the exception of [`macro_rules`] which has its own scoping behavior.
6061
[Name resolution] of item names allows items to be defined before or after where the item is referred to in the module or block.
@@ -83,6 +84,7 @@ See [item scopes] for information on the scoping rules of items.
8384
[`macro_rules`]: macros-by-example.md
8485
[`use` declarations]: items/use-declarations.md
8586
[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

Diff for: src/names/scopes.md

+39-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
A *scope* is the region of source text where a named [entity] may be referenced with that name.
44
The following sections provide details on the scoping rules and behavior, which depend on the kind of entity and where it is declared.
55
The process of how names are resolved to entities is described in the [name resolution] chapter.
6-
More information on "drop scopes" used for the purpose of running destructors maybe be found in the [destructors] chapter.
6+
More information on "drop scopes" used for the purpose of running destructors may be found in the [destructors] chapter.
77

88
## Item scopes
99

@@ -20,10 +20,10 @@ A [path] may be used to refer to an item in another module.
2020

2121
### Associated item scopes
2222

23-
[Associated items] are not scoped and can only be referred to by using a [path] leading from the type they are associated with.
23+
[Associated items] are not scoped and can only be referred to by using a [path] leading from the type or trait they are associated with.
2424
[Methods] can also be referred to via [call expressions].
2525

26-
Similar to items within a module or block, it is an error to introduce an item within a trait or implementation that is a duplicate of another item in the trait or impl in the same namespace.
26+
Similar to items within a module or block, it is an error to introduce an item within a trait or implementation that is a duplicate of another item in the trait or impl in the same namespace.
2727

2828
## Pattern binding scopes
2929

@@ -90,24 +90,33 @@ fn where_scope<'a, T, U>()
9090
{}
9191
```
9292

93-
Generic scopes do not extend into [items] declared inside a function.
93+
It is an error for [items] declared inside a function to refer to a generic parameter from their outer scope.
94+
95+
```rust,compile_fail
96+
fn example<T>() {
97+
fn inner(x: T) {} // ERROR: can't use generic parameters from outer function
98+
}
99+
```
94100

95101
### Generic parameter shadowing
96102

97-
It is an error to shadow a generic parameter.
98-
Items declared within functions are allowed to reuse generic parameter names from the function because generic scopes do not extend to inner items.
103+
It is an error to shadow a generic parameter with the exception that items declared within functions are allowed to shadow generic parameter names from the function.
99104

100105
```rust
101-
fn example<'a>() {
102-
// Items within functions are allowed to reuse generic parameter in scope
103-
// because all generics are not in scope within inner items.
104-
fn inner<'a>() {} // OK
106+
fn example<'a, T, const N: usize>() {
107+
// Items within functions are allowed to shadow generic parameter in scope.
108+
fn inner_lifetime<'a>() {} // OK
109+
fn inner_type<T>() {} // OK
110+
fn inner_const<const N: usize>() {} // OK
105111
}
106112
```
107113

108114
```rust,compile_fail
109-
trait SomeTrait<'a> {
110-
fn example<'a>() {} // ERROR: 'a is already in scope
115+
trait SomeTrait<'a, T, const N: usize> {
116+
fn example_lifetime<'a>() {} // ERROR: 'a is already in use
117+
fn example_type<T>() {} // ERROR: T is already in use
118+
fn example_const<const N: usize>() {} // ERROR: N is already in use
119+
fn example_mixed<const T: usize>() {} // ERROR: T is already in use
111120
}
112121
```
113122

@@ -262,11 +271,27 @@ Helper attributes shadow other attributes of the same name in scope.
262271
Although [`Self`] is a keyword with special meaning, it interacts with name resolution in a way similar to normal names.
263272

264273
The implicit `Self` type in the definition of a [struct], [enum], [union], [trait], or [implementation] is treated similarly to a [generic parameter](#generic-parameter-scopes), and is in scope in the same way as a generic type parameter.
265-
For structs, enums, and unions, it is in scope starting after the generic parameters.
266-
For traits and implementations, it is in scope starting just before the generic parameters.
267274

268275
The implicit `Self` constructor in the value [namespace] of an [implementation] is in scope within the body of the implementation (the implementation's [associated items]).
269276

277+
```rust
278+
// Self type within struct definition.
279+
struct Recursive {
280+
f1: Option<Box<Self>>
281+
}
282+
283+
// Self type within generic parameters.
284+
struct SelfGeneric<T: Into<Self>>(T);
285+
286+
// Self value constructor within an implementation.
287+
struct ImplExample();
288+
impl ImplExample {
289+
fn example() -> Self { // Self type
290+
Self() // Self value constructor
291+
}
292+
}
293+
```
294+
270295
[_BareFunctionType_]: ../types/function-pointer.md
271296
[_GenericParams_]: ../items/generics.md
272297
[_TraitBound_]: ../trait-bounds.md

0 commit comments

Comments
 (0)