Skip to content

Commit 02fcee9

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 1ae3dee commit 02fcee9

13 files changed

+93
-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

@@ -115,3 +117,4 @@ fn unused_generic_function<T>() {
115117
[_Type_]: ../types.md#type-expressions
116118
[_Expression_]: ../expressions.md
117119
[`Copy`]: ../special-types-and-traits.md#copy
120+
[value namespace]: ../names/namespaces.md

src/items/enumerations.md

Lines changed: 39 additions & 10 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

@@ -80,6 +81,27 @@ enum Enum {
8081
}
8182
```
8283

84+
Variant constructors are similar to [struct] definitions, and can be referenced by a path from the enumeration name, including in [use declarations].
85+
The constructors are defined in both the [type namespace] and [value namespace] within the enumeration.
86+
87+
A struct-like variant can be instantiated with a [struct expression].
88+
A tuple-like variant can be instantiated with a [call expression].
89+
A unit-like variant can be instantiated with a [path expression].
90+
For example:
91+
92+
```rust
93+
enum Examples {
94+
UnitLike,
95+
TupleLike(i32),
96+
StructLike { value: i32 },
97+
}
98+
99+
use Examples::*; // Creates aliases to all variants.
100+
let x = UnitLike; // Path expression of the const item.
101+
let y = TupleLike(123); // Call expression.
102+
let z = StructLike { value: 123 }; // Struct expression.
103+
```
104+
83105
<span id="custom-discriminant-values-for-fieldless-enumerations"></span>
84106
## Discriminants
85107

@@ -299,20 +321,27 @@ enum E {
299321
}
300322
```
301323

302-
[IDENTIFIER]: ../identifiers.md
303-
[_GenericParams_]: generics.md
304-
[_WhereClause_]: generics.md#where-clauses
305324
[_Expression_]: ../expressions.md
306-
[_TupleFields_]: structs.md
325+
[_GenericParams_]: generics.md
307326
[_StructFields_]: structs.md
327+
[_TupleFields_]: structs.md
308328
[_Visibility_]: ../visibility-and-privacy.md
309-
[enumerated type]: ../types/enum.md
329+
[_WhereClause_]: generics.md#where-clauses
330+
[`C` representation]: ../type-layout.md#the-c-representation
310331
[`mem::discriminant`]: ../../std/mem/fn.discriminant.html
311-
[never type]: ../types/never.md
312-
[unit-only]: #unit-only-enum
313-
[numeric cast]: ../expressions/operator-expr.md#semantics
332+
[call expression]: ../expressions/call-expr.md
314333
[constant expression]: ../const_eval.md#constant-expressions
315334
[default representation]: ../type-layout.md#the-default-representation
316-
[primitive representation]: ../type-layout.md#primitive-representations
317-
[`C` representation]: ../type-layout.md#the-c-representation
335+
[enumerated type]: ../types/enum.md
318336
[Field-less enums]: #field-less-enum
337+
[IDENTIFIER]: ../identifiers.md
338+
[never type]: ../types/never.md
339+
[numeric cast]: ../expressions/operator-expr.md#semantics
340+
[path expression]: ../expressions/path-expr.md
341+
[primitive representation]: ../type-layout.md#primitive-representations
342+
[struct expression]: ../expressions/struct-expr.md
343+
[struct]: structs.md
344+
[type namespace]: ../names/namespaces.md
345+
[unit-only]: #unit-only-enum
346+
[use declarations]: use-declarations.md
347+
[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
> &nbsp;&nbsp; `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
@@ -74,6 +72,7 @@ crate to access only its macros.
7472
[extern prelude]: ../names/preludes.md#extern-prelude
7573
[`macro_use` prelude]: ../names/preludes.md#macro_use-prelude
7674
[`crate_name` attributes]: ../crates-and-source-files.md#the-crate_name-attribute
75+
[type namespace]: ../names/namespaces.md
7776

7877
<script>
7978
(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 kinds 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
@@ -340,3 +342,4 @@ restrictions as [regular function parameters].
340342
[`verbatim` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-verbatim
341343
[`dylib` versus `raw-dylib`]: #dylib-versus-raw-dylib
342344
[PE Format]: https://learn.microsoft.com/windows/win32/debug/pe-format#import-name-type
345+
[value namespace]: ../names/namespaces.md

src/items/functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
4545
A _function_ consists of a [block], along with a name, a set of parameters, and an output type.
4646
Other than a name, all these are optional.
47-
Functions are declared with the keyword `fn`.
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.
4848
Functions may declare a set of *input* [*variables*][variables] as parameters, through which the caller passes arguments into the function, and the *output* [*type*][type] of the value the function will return to its caller on completion.
4949
If the output type is not explicitly stated, it is the [unit type].
5050

@@ -412,4 +412,5 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
412412
[method]: associated-items.md#methods
413413
[associated function]: associated-items.md#associated-functions-and-methods
414414
[implementation]: implementations.md
415+
[value namespace]: ../names/namespaces.md
415416
[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

@@ -129,3 +131,4 @@ following are true:
129131
[IDENTIFIER]: ../identifiers.md
130132
[_Type_]: ../types.md#type-expressions
131133
[_Expression_]: ../expressions.md
134+
[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
@@ -345,3 +348,4 @@ fn main() {
345348
[`Rc<Self>`]: ../special-types-and-traits.md#rct
346349
[`async`]: functions.md#async-functions
347350
[`const`]: functions.md#const-functions
351+
[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_] [_WhereClause_]<sup>?</sup>)<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:
@@ -53,3 +52,4 @@ the equals sign (like `type TypeAlias<T> = Bar<T> where T: Foo`) are preferred.
5352
[trait]: traits.md
5453
[type]: ../types.md
5554
[trait impl]: implementations.md#trait-implementations
55+
[type namespace]: ../names/namespaces.md

0 commit comments

Comments
 (0)