Skip to content

Commit b0601ea

Browse files
committed
Specify how names are introduced.
This is just a first pass to specifying how each thing introduces a name, and where it is introduced. This is missing `use` and `Self`. (And a few things I don't feel like need elaboration, like loop labels, but those can be added if desired.)
1 parent 91fb8ee commit b0601ea

13 files changed

+94
-39
lines changed

src/items/constant-items.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ context when used. This includes usage of constants from external crates, and
1111
non-[`Copy`] types. References to the same constant are not necessarily
1212
guaranteed to refer to the same memory address.
1313

14+
The constant declaration defines the constant value in the [value namespace] of the module or block where it is located.
15+
1416
Constants must be explicitly typed. The type must have a `'static` lifetime: any
1517
references in the initializer must have `'static` lifetimes.
1618

@@ -99,3 +101,4 @@ m!(const _: () = (););
99101
[_Type_]: ../types.md#type-expressions
100102
[_Expression_]: ../expressions.md
101103
[`Copy`]: ../special-types-and-traits.md#copy
104+
[value namespace]: ../names/namespaces.md

src/items/enumerations.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ nominal [enumerated type] as well as a set of *constructors*, that can be used
3030
to create or pattern-match values of the corresponding enumerated type.
3131

3232
Enumerations are declared with the keyword `enum`.
33+
The `enum` declaration defines the enumeration type in the [type namespace] of the module or block where it is located.
3334

3435
An example of an `enum` item and its use:
3536

@@ -61,6 +62,27 @@ integer associated to it that is used to determine which variant it holds. An
6162
opaque reference to this discriminant can be obtained with the
6263
[`mem::discriminant`] function.
6364

65+
Variant constructors are similar to [struct] definitions, and can be referenced by a path from the enumeration name, including in [use declarations].
66+
The constructors are defined in both the [type namespace] and [value namespace] within the enumeration.
67+
68+
A struct-like variant can be instantiated with a [struct expression].
69+
A tuple-like variant can be instantiated with a [call expression].
70+
A unit-like variant can be instantiated with a [path expression].
71+
For example:
72+
73+
```rust
74+
enum Examples {
75+
UnitLike,
76+
TupleLike(i32),
77+
StructLike { value: i32 },
78+
}
79+
80+
use Examples::*; // Creates aliases to all variants.
81+
let x = UnitLike; // Path expression of the const item.
82+
let y = TupleLike(123); // Call expression.
83+
let z = StructLike { value: 123 }; // Struct expression.
84+
```
85+
6486
## Custom Discriminant Values for Fieldless Enumerations
6587

6688
If there is no data attached to *any* of the variants of an enumeration,
@@ -171,18 +193,25 @@ enum E {
171193
}
172194
```
173195

174-
[IDENTIFIER]: ../identifiers.md
175-
[_GenericParams_]: generics.md
176-
[_WhereClause_]: generics.md#where-clauses
177196
[_Expression_]: ../expressions.md
178-
[_TupleFields_]: structs.md
197+
[_GenericParams_]: generics.md
179198
[_StructFields_]: structs.md
199+
[_TupleFields_]: structs.md
180200
[_Visibility_]: ../visibility-and-privacy.md
181-
[enumerated type]: ../types/enum.md
201+
[_WhereClause_]: generics.md#where-clauses
202+
[`C` representation]: ../type-layout.md#the-c-representation
182203
[`mem::discriminant`]: ../../std/mem/fn.discriminant.html
183-
[never type]: ../types/never.md
184-
[numeric cast]: ../expressions/operator-expr.md#semantics
204+
[call expression]: ../expressions/call-expr.md
185205
[constant expression]: ../const_eval.md#constant-expressions
186206
[default representation]: ../type-layout.md#the-default-representation
207+
[enumerated type]: ../types/enum.md
208+
[IDENTIFIER]: ../identifiers.md
209+
[never type]: ../types/never.md
210+
[numeric cast]: ../expressions/operator-expr.md#semantics
211+
[path expression]: ../expressions/path-expr.md
187212
[primitive representation]: ../type-layout.md#primitive-representations
188-
[`C` representation]: ../type-layout.md#the-c-representation
213+
[struct expression]: ../expressions/struct-expr.md
214+
[struct]: structs.md
215+
[type namespace]: ../names/namespaces.md
216+
[use declarations]: use-declarations.md
217+
[value namespace]: ../names/namespaces.md

src/items/extern-crates.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
>    `as` ( [IDENTIFIER] | `_` )
1212
1313
An _`extern crate` declaration_ specifies a dependency on an external crate.
14-
The external crate is then bound into the declaring scope as the [identifier]
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.
14+
The external crate is then bound into the declaring scope as the given [identifier] in the [type namespace].
15+
Additionally, if the `extern crate` appears in the crate root, then the crate name is also added to the [extern prelude], making it automatically in scope in all modules.
16+
The `as` clause can be used to bind the imported crate to a different name.
1917

2018
The external crate is resolved to a specific `soname` at compile time, and a
2119
runtime linkage requirement to that `soname` is passed to the linker for
@@ -78,6 +76,7 @@ crate to access only its macros.
7876
[`macro_use` attribute]: ../macros-by-example.md#the-macro_use-attribute
7977
[extern prelude]: ../names/preludes.md#extern-prelude
8078
[`macro_use` prelude]: ../names/preludes.md#macro_use-prelude
79+
[type namespace]: ../names/namespaces.md
8180

8281
<script>
8382
(function() {

src/items/external-blocks.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Two kind of item _declarations_ are allowed in external blocks: [functions] and
2121
[statics]. Calling functions or accessing statics that are declared in external
2222
blocks is only allowed in an `unsafe` context.
2323

24+
The external block defines its functions and statics in the [value namespace] of the module or block where it is located.
25+
2426
The `unsafe` keyword is syntactically allowed to appear before the `extern`
2527
keyword, but it is rejected at a semantic level. This allows macros to consume
2628
the syntax and make use of the `unsafe` keyword, before removing it from the
@@ -186,3 +188,4 @@ restrictions as [regular function parameters].
186188
[_Visibility_]: ../visibility-and-privacy.md
187189
[attributes]: ../attributes.md
188190
[regular function parameters]: functions.md#attributes-on-function-parameters
191+
[value namespace]: ../names/namespaces.md

src/items/functions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
> in an associated function of a [trait item] in the 2015 edition.
4444
4545
A _function_ consists of a [block], along with a name and a set of parameters.
46-
Other than a name, all these are optional. Functions are declared with the
47-
keyword `fn`. Functions may declare a set of *input* [*variables*][variables]
46+
Other than a name, all these are optional.
47+
Functions are declared with the keyword `fn` which defines the given name in the [value namespace] of the module or block where it is located.
48+
Functions may declare a set of *input* [*variables*][variables]
4849
as parameters, through which the caller passes arguments into the function, and
4950
the *output* [*type*][type] of the value the function will return to its caller
5051
on completion.
@@ -412,4 +413,5 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
412413
[method]: associated-items.md#methods
413414
[associated function]: associated-items.md#associated-functions-and-methods
414415
[implementation]: implementations.md
416+
[value namespace]: ../names/namespaces.md
415417
[variadic function]: external-blocks.md#variadic-functions

src/items/modules.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ mod math {
3434
}
3535
```
3636

37-
Modules and types share the same namespace. Declaring a named type with the
38-
same name as a module in scope is forbidden: that is, a type definition, trait,
39-
struct, enumeration, union, type parameter or crate can't shadow the name of a
40-
module in scope, or vice versa. Items brought into scope with `use` also have
41-
this restriction.
37+
Modules are defined in the [type namespace] of the module or block where it is located.
38+
It is an error to define multiple items with the same name in the same namespace within a module.
39+
See the [scopes chapter] for more details on restrictions and shadowing behavior.
4240

4341
The `unsafe` keyword is syntactically allowed to appear before the `mod`
4442
keyword, but it is rejected at a semantic level. This allows macros to consume
@@ -149,7 +147,9 @@ The built-in attributes that have meaning on a module are [`cfg`],
149147
[attribute]: ../attributes.md
150148
[items]: ../items.md
151149
[module path]: ../paths.md
150+
[scopes chapter]: ../names/scopes.md
152151
[the lint check attributes]: ../attributes/diagnostics.md#lint-check-attributes
152+
[type namespace]: ../names/namespaces.md
153153

154154
<script>
155155
(function() {

src/items/static-items.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ memory location. Static items have the `static` lifetime, which outlives all
1111
other lifetimes in a Rust program. Static items do not call [`drop`] at the
1212
end of the program.
1313

14+
The static declaration defines the static value in the [value namespace] of the module or block where it is located.
15+
1416
The static initializer is a [constant expression] evaluated at compile time.
1517
Static initializers may refer to other statics.
1618

@@ -81,3 +83,4 @@ following are true:
8183
[IDENTIFIER]: ../identifiers.md
8284
[_Type_]: ../types.md#type-expressions
8385
[_Expression_]: ../expressions.md
86+
[value namespace]: ../names/namespaces.md

src/items/structs.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
> &nbsp;&nbsp; [_Type_]
3838
3939
A _struct_ is a nominal [struct type] defined with the keyword `struct`.
40+
A struct declaration defines the given name in the [type namespace] of the module or block where it is located.
4041

4142
An example of a `struct` item and its use:
4243

@@ -46,11 +47,10 @@ let p = Point {x: 10, y: 11};
4647
let px: i32 = p.x;
4748
```
4849

49-
A _tuple struct_ is a nominal [tuple type], also defined with the keyword
50-
`struct`. For example:
51-
52-
[struct type]: ../types/struct.md
53-
[tuple type]: ../types/tuple.md
50+
A _tuple struct_ is a nominal [tuple type], also defined with the keyword `struct`.
51+
In addition to defining a type, it also defines a constructor of the same name in the [value namespace].
52+
The constructor is a function which can be called to create a new instance of the struct.
53+
For example:
5454

5555
```rust
5656
struct Point(i32, i32);
@@ -59,7 +59,7 @@ let px: i32 = match p { Point(x, _) => x };
5959
```
6060

6161
A _unit-like struct_ is a struct without any fields, defined by leaving off the
62-
list of fields entirely. Such a struct implicitly defines a constant of its
62+
list of fields entirely. Such a struct implicitly defines a [constant] of its
6363
type with the same name. For example:
6464

6565
```rust
@@ -78,11 +78,15 @@ let c = [Cookie, Cookie {}, Cookie, Cookie {}];
7878
The precise memory layout of a struct is not specified. One can specify a
7979
particular layout using the [`repr` attribute].
8080

81-
[`repr` attribute]: ../type-layout.md#representations
82-
83-
[_OuterAttribute_]: ../attributes.md
84-
[IDENTIFIER]: ../identifiers.md
8581
[_GenericParams_]: generics.md
86-
[_WhereClause_]: generics.md#where-clauses
87-
[_Visibility_]: ../visibility-and-privacy.md
82+
[_OuterAttribute_]: ../attributes.md
8883
[_Type_]: ../types.md#type-expressions
84+
[_Visibility_]: ../visibility-and-privacy.md
85+
[_WhereClause_]: generics.md#where-clauses
86+
[`repr` attribute]: ../type-layout.md#representations
87+
[IDENTIFIER]: ../identifiers.md
88+
[constant]: constant-items.md
89+
[struct type]: ../types/struct.md
90+
[tuple type]: ../types/tuple.md
91+
[type namespace]: ../names/namespaces.md
92+
[value namespace]: ../names/namespaces.md

src/items/traits.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ interface consists of [associated items], which come in three varieties:
1717
- [types](associated-items.md#associated-types)
1818
- [constants](associated-items.md#associated-constants)
1919

20+
The trait declaration defines the trait in the [type namespace] of the module or block where it is located.
21+
Associated items are defined as members of the trait within their respective namespaces: type namespace for associated types, and value namespace for constants and functions.
22+
2023
All traits define an implicit type parameter `Self` that refers to "the type
2124
that is implementing this interface". Traits may also contain additional type
2225
parameters. These type parameters, including `Self`, may be constrained by
@@ -341,3 +344,4 @@ fn main() {
341344
[`Rc<Self>`]: ../special-types-and-traits.md#rct
342345
[`async`]: functions.md#async-functions
343346
[`const`]: functions.md#const-functions
347+
[type namespace]: ../names/namespaces.md

src/items/type-aliases.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
> ( `:` [_TypeParamBounds_] )<sup>?</sup>
77
> [_WhereClause_]<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> `;`
88
9-
A _type alias_ defines a new name for an existing [type]. Type aliases are
10-
declared with the keyword `type`. Every value has a single, specific type, but
11-
may implement several different traits, or be compatible with several different
12-
type constraints.
9+
A _type alias_ defines a new name for an existing [type] in the [type namespace] of the module or block where it is located.
10+
Type aliases are declared with the keyword `type`.
11+
Every value has a single, specific type, but may implement several different traits, or be compatible with several different type constraints.
1312

1413
For example, the following defines the type `Point` as a synonym for the type
1514
`(u8, u8)`, the type of pairs of unsigned 8 bit integers:
@@ -45,3 +44,4 @@ an [associated type] in a [trait].
4544
[associated type]: associated-items.md#associated-types
4645
[trait]: traits.md
4746
[type]: ../types.md
47+
[type namespace]: ../names/namespaces.md

0 commit comments

Comments
 (0)