From 2aadaad918b1d0a887c6c513a0e9ca49aab1d74e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 27 Jun 2021 12:31:20 -0700 Subject: [PATCH 01/12] 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.) --- src/items/constant-items.md | 3 +++ src/items/enumerations.md | 49 ++++++++++++++++++++++++++++-------- src/items/extern-crates.md | 9 +++---- src/items/external-blocks.md | 3 +++ src/items/functions.md | 3 ++- src/items/modules.md | 10 ++++---- src/items/static-items.md | 3 +++ src/items/structs.md | 28 ++++++++++++--------- src/items/traits.md | 4 +++ src/items/type-aliases.md | 8 +++--- src/items/unions.md | 2 ++ src/patterns.md | 3 ++- src/procedural-macros.md | 7 +++++- 13 files changed, 93 insertions(+), 39 deletions(-) diff --git a/src/items/constant-items.md b/src/items/constant-items.md index 9d48762ef..9fb218519 100644 --- a/src/items/constant-items.md +++ b/src/items/constant-items.md @@ -11,6 +11,8 @@ context when used. This includes usage of constants from external crates, and non-[`Copy`] types. References to the same constant are not necessarily guaranteed to refer to the same memory address. +The constant declaration defines the constant value in the [value namespace] of the module or block where it is located. + Constants must be explicitly typed. The type must have a `'static` lifetime: any references in the initializer must have `'static` lifetimes. @@ -115,3 +117,4 @@ fn unused_generic_function() { [_Type_]: ../types.md#type-expressions [_Expression_]: ../expressions.md [`Copy`]: ../special-types-and-traits.md#copy +[value namespace]: ../names/namespaces.md diff --git a/src/items/enumerations.md b/src/items/enumerations.md index 0d42bfd05..043cbe962 100644 --- a/src/items/enumerations.md +++ b/src/items/enumerations.md @@ -30,6 +30,7 @@ nominal [enumerated type] as well as a set of *constructors*, that can be used to create or pattern-match values of the corresponding enumerated type. Enumerations are declared with the keyword `enum`. +The `enum` declaration defines the enumeration type in the [type namespace] of the module or block where it is located. An example of an `enum` item and its use: @@ -80,6 +81,27 @@ enum Enum { } ``` +Variant constructors are similar to [struct] definitions, and can be referenced by a path from the enumeration name, including in [use declarations]. +The constructors are defined in both the [type namespace] and [value namespace] within the enumeration. + +A struct-like variant can be instantiated with a [struct expression]. +A tuple-like variant can be instantiated with a [call expression]. +A unit-like variant can be instantiated with a [path expression]. +For example: + +```rust +enum Examples { + UnitLike, + TupleLike(i32), + StructLike { value: i32 }, +} + +use Examples::*; // Creates aliases to all variants. +let x = UnitLike; // Path expression of the const item. +let y = TupleLike(123); // Call expression. +let z = StructLike { value: 123 }; // Struct expression. +``` + ## Discriminants @@ -299,20 +321,27 @@ enum E { } ``` -[IDENTIFIER]: ../identifiers.md -[_GenericParams_]: generics.md -[_WhereClause_]: generics.md#where-clauses [_Expression_]: ../expressions.md -[_TupleFields_]: structs.md +[_GenericParams_]: generics.md [_StructFields_]: structs.md +[_TupleFields_]: structs.md [_Visibility_]: ../visibility-and-privacy.md -[enumerated type]: ../types/enum.md +[_WhereClause_]: generics.md#where-clauses +[`C` representation]: ../type-layout.md#the-c-representation [`mem::discriminant`]: ../../std/mem/fn.discriminant.html -[never type]: ../types/never.md -[unit-only]: #unit-only-enum -[numeric cast]: ../expressions/operator-expr.md#semantics +[call expression]: ../expressions/call-expr.md [constant expression]: ../const_eval.md#constant-expressions [default representation]: ../type-layout.md#the-default-representation -[primitive representation]: ../type-layout.md#primitive-representations -[`C` representation]: ../type-layout.md#the-c-representation +[enumerated type]: ../types/enum.md [Field-less enums]: #field-less-enum +[IDENTIFIER]: ../identifiers.md +[never type]: ../types/never.md +[numeric cast]: ../expressions/operator-expr.md#semantics +[path expression]: ../expressions/path-expr.md +[primitive representation]: ../type-layout.md#primitive-representations +[struct expression]: ../expressions/struct-expr.md +[struct]: structs.md +[type namespace]: ../names/namespaces.md +[unit-only]: #unit-only-enum +[use declarations]: use-declarations.md +[value namespace]: ../names/namespaces.md diff --git a/src/items/extern-crates.md b/src/items/extern-crates.md index 9dd6a5eb9..523e9720d 100644 --- a/src/items/extern-crates.md +++ b/src/items/extern-crates.md @@ -11,11 +11,9 @@ >    `as` ( [IDENTIFIER] | `_` ) An _`extern crate` declaration_ specifies a dependency on an external crate. -The external crate is then bound into the declaring scope as the [identifier] -provided in the `extern crate` declaration. 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. The `as` -clause can be used to bind the imported crate to a different name. +The external crate is then bound into the declaring scope as the given [identifier] in the [type namespace]. +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. +The `as` clause can be used to bind the imported crate to a different name. The external crate is resolved to a specific `soname` at compile time, and a runtime linkage requirement to that `soname` is passed to the linker for @@ -74,6 +72,7 @@ crate to access only its macros. [extern prelude]: ../names/preludes.md#extern-prelude [`macro_use` prelude]: ../names/preludes.md#macro_use-prelude [`crate_name` attributes]: ../crates-and-source-files.md#the-crate_name-attribute +[type namespace]: ../names/namespaces.md