Skip to content

Commit 0fcf4ed

Browse files
authored
Merge pull request #1052 from ehuss/namespace-defines
Add details on how names are introduced.
2 parents 2e19181 + 585407f commit 0fcf4ed

18 files changed

+400
-122
lines changed

src/attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ active. All other attributes are inert.
174174
## Tool attributes
175175

176176
The compiler may allow attributes for external tools where each tool resides
177-
in its own namespace in the [tool prelude]. The first segment of the attribute
177+
in its own module in the [tool prelude]. The first segment of the attribute
178178
path is the name of the tool, with one or more additional segments whose
179179
interpretation is up to the tool.
180180

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: 42 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,30 @@ 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+
Each variant defines its type in the [type namespace], though that type cannot be used as a type specifier.
86+
Tuple-like and unit-like variants also define a constructor in the [value namespace].
87+
88+
A struct-like variant can be instantiated with a [struct expression].
89+
A tuple-like variant can be instantiated with a [call expression] or a [struct expression].
90+
A unit-like variant can be instantiated with a [path expression] or a [struct expression].
91+
For example:
92+
93+
```rust
94+
enum Examples {
95+
UnitLike,
96+
TupleLike(i32),
97+
StructLike { value: i32 },
98+
}
99+
100+
use Examples::*; // Creates aliases to all variants.
101+
let x = UnitLike; // Path expression of the const item.
102+
let x = UnitLike {}; // Struct expression.
103+
let y = TupleLike(123); // Call expression.
104+
let y = TupleLike { 0: 123 }; // Struct expression using integer field names.
105+
let z = StructLike { value: 123 }; // Struct expression.
106+
```
107+
83108
<span id="custom-discriminant-values-for-fieldless-enumerations"></span>
84109
## Discriminants
85110

@@ -299,20 +324,27 @@ enum E {
299324
}
300325
```
301326

302-
[IDENTIFIER]: ../identifiers.md
303-
[_GenericParams_]: generics.md
304-
[_WhereClause_]: generics.md#where-clauses
305327
[_Expression_]: ../expressions.md
306-
[_TupleFields_]: structs.md
328+
[_GenericParams_]: generics.md
307329
[_StructFields_]: structs.md
330+
[_TupleFields_]: structs.md
308331
[_Visibility_]: ../visibility-and-privacy.md
309-
[enumerated type]: ../types/enum.md
332+
[_WhereClause_]: generics.md#where-clauses
333+
[`C` representation]: ../type-layout.md#the-c-representation
310334
[`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
335+
[call expression]: ../expressions/call-expr.md
314336
[constant expression]: ../const_eval.md#constant-expressions
315337
[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
338+
[enumerated type]: ../types/enum.md
318339
[Field-less enums]: #field-less-enum
340+
[IDENTIFIER]: ../identifiers.md
341+
[never type]: ../types/never.md
342+
[numeric cast]: ../expressions/operator-expr.md#semantics
343+
[path expression]: ../expressions/path-expr.md
344+
[primitive representation]: ../type-layout.md#primitive-representations
345+
[struct expression]: ../expressions/struct-expr.md
346+
[struct]: structs.md
347+
[type namespace]: ../names/namespaces.md
348+
[unit-only]: #unit-only-enum
349+
[use declarations]: use-declarations.md
350+
[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
@@ -342,3 +344,4 @@ restrictions as [regular function parameters].
342344
[`verbatim` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-verbatim
343345
[`dylib` versus `raw-dylib`]: #dylib-versus-raw-dylib
344346
[PE Format]: https://learn.microsoft.com/windows/win32/debug/pe-format#import-name-type
347+
[value namespace]: ../names/namespaces.md

src/items/functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
A _function_ consists of a [block] (that's the _body_ of the function),
4646
along with a name, a set of parameters, and an output type.
4747
Other than a name, all these are optional.
48-
Functions are declared with the keyword `fn`.
48+
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.
4949
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.
5050
If the output type is not explicitly stated, it is the [unit type].
5151

@@ -413,4 +413,5 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
413413
[method]: associated-items.md#methods
414414
[associated function]: associated-items.md#associated-functions-and-methods
415415
[implementation]: implementations.md
416+
[value namespace]: ../names/namespaces.md
416417
[variadic function]: external-blocks.md#variadic-functions

src/items/generics.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ referred to with path syntax.
4848

4949
### Const generics
5050

51-
*Const generic parameters* allow items to be generic over constant values. The
52-
const identifier introduces a name for the constant parameter, and all
53-
instances of the item must be instantiated with a value of the given type.
54-
55-
<!-- TODO: update above to say "introduces a name in the [value namespace]"
56-
once namespaces are added. -->
51+
*Const generic parameters* allow items to be generic over constant values.
52+
The const identifier introduces a name in the [value namespace] for the constant parameter, and all instances of the item must be instantiated with a value of the given type.
5753

5854
The only allowed types of const parameters are `u8`, `u16`, `u32`, `u64`, `u128`, `usize`,
5955
`i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `char` and `bool`.
@@ -282,6 +278,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
282278
[slices]: ../types/slice.md
283279
[associated const]: associated-items.md#associated-constants
284280
[associated type]: associated-items.md#associated-types
281+
[attributes]: ../attributes.md
285282
[block]: ../expressions/block-expr.md
286283
[const contexts]: ../const_eval.md#const-context
287284
[const expression]: ../const_eval.md#constant-expressions
@@ -307,4 +304,4 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
307304
[type aliases]: type-aliases.md
308305
[type]: ../types.md
309306
[unions]: unions.md
310-
[attributes]: ../attributes.md
307+
[value namespace]: ../names/namespaces.md

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 they are 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 a 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

@@ -136,3 +138,4 @@ following are true:
136138
[IDENTIFIER]: ../identifiers.md
137139
[_Type_]: ../types.md#type-expressions
138140
[_Expression_]: ../expressions.md
141+
[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], and is 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

0 commit comments

Comments
 (0)