Skip to content

Commit 49d6c3c

Browse files
Merge pull request #431 from ehuss/grammar-paths
Grammar: Paths
2 parents ac6a74f + ad497a3 commit 49d6c3c

10 files changed

+328
-83
lines changed

src/attributes.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
>    `#[` MetaItem `]`
1212
>
1313
> _MetaItem_ :\
14-
>       IDENTIFIER\
15-
>    | IDENTIFIER `=` LITERAL\
16-
>    | IDENTIFIER `(` _MetaSeq_ `)`
14+
>       [_SimplePath_]\
15+
> &nbsp;&nbsp; | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\
16+
> &nbsp;&nbsp; | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)`
1717
>
1818
> _MetaSeq_ :\
19-
> &nbsp;&nbsp; &nbsp;&nbsp; EMPTY\
20-
> &nbsp;&nbsp; | _MetaItem_\
21-
> &nbsp;&nbsp; | LITERAL\
22-
> &nbsp;&nbsp; | _MetaItem_ `,` _MetaSeq_
19+
> &nbsp;&nbsp; _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup>
20+
>
21+
> _MetaItemInner_ :\
22+
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
23+
> &nbsp;&nbsp; | [_LiteralExpression_]<sub>_without suffix_</sub>
2324
2425
An _attribute_ is a general, free-form metadatum that is interpreted according
2526
to name, convention, and language and compiler version. Attributes are modeled
@@ -33,6 +34,8 @@ Attributes may appear as any of:
3334
* An identifier followed by a parenthesized list of sub-attribute arguments
3435
which include literals
3536

37+
Literal values must not include integer or float type suffixes.
38+
3639
_Inner attributes_, written with a bang ("!") after the hash ("#"), apply to the
3740
item that the attribute is declared within. _Outer attributes_, written without
3841
the bang after the hash, apply to the thing that follows the attribute.
@@ -522,6 +525,8 @@ impl<T: PartialEq> PartialEq for Foo<T> {
522525

523526
You can implement `derive` for your own traits through [procedural macros].
524527

528+
[_LiteralExpression_]: expressions/literal-expr.html
529+
[_SimplePath_]: paths.html#simple-paths
525530
[Doc comments]: comments.html#doc-comments
526531
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
527532
[procedural macros]: procedural-macros.html

src/expressions/method-call-expr.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Method-call expressions
22

3+
> **<sup>Syntax</sup>**\
4+
> _MethodCallExpression_ :\
5+
> &nbsp;&nbsp; [_Expression_] `.` [_PathExprSegment_] `(`[_CallParams_]<sup>?</sup> `)`
6+
37
A _method call_ consists of an expression (the *receiver*) followed by a single
4-
dot, an [identifier], and a parenthesized expression-list. Method calls are
8+
dot, an expression path segment, and a parenthesized expression-list. Method calls are
59
resolved to associated [methods] on specific traits, either statically
610
dispatching to a method if the exact `self`-type of the left-hand-side is known,
711
or dynamically dispatching if the left-hand-side expression is an indirect
@@ -93,7 +97,9 @@ method and you'll be fine.
9397
9498
</div>
9599
96-
[IDENTIFIER]: identifiers.html
100+
[_CallParams_]: expressions/call-expr.html
101+
[_Expression_]: expressions.html
102+
[_PathExprSegment_]: paths.html#paths-in-expressions
97103
[visible]: visibility-and-privacy.html
98104
[array]: types.html#array-and-slice-types
99105
[trait objects]: types.html#trait-objects

src/expressions/operator-expr.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`
312312

313313
> **<sup>Syntax</sup>**\
314314
> _TypeCastExpression_ :\
315-
> &nbsp;&nbsp; [_Expression_] `as` [_PathInExpression_]
315+
> &nbsp;&nbsp; [_Expression_] `as` [_TypeNoBounds_]
316316
317317
A type cast expression is denoted with the binary operator `as`.
318318

@@ -460,4 +460,4 @@ assert_eq!(x, 14);
460460
[_CompoundAssignmentExpression_]: #compound-assignment-expressions
461461

462462
[_Expression_]: expressions.html
463-
[_PathInExpression_]: paths.html
463+
[_TypeNoBounds_]: types.html

src/expressions/path-expr.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Path expressions
22

3+
> **<sup>Syntax</sup>**\
4+
> _PathExpression_ :\
5+
> &nbsp;&nbsp; &nbsp;&nbsp; [_PathInExpression_]\
6+
> &nbsp;&nbsp; | [_QualifiedPathInExpression_]
7+
38
A [path] used as an expression context denotes either a local
49
variable or an item. Path expressions that resolve to local or static variables
510
are [place expressions], other paths are [value expressions]. Using a
@@ -19,8 +24,10 @@ let push_integer = Vec::<i32>::push;
1924
let slice_reverse = <[i32]>::reverse;
2025
```
2126

27+
[_PathInExpression_]: paths.html#paths-in-expressions
28+
[_QualifiedPathInExpression_]: paths.html#qualified-paths
2229
[place expressions]: expressions.html#place-expressions-and-value-expressions
2330
[value expressions]: expressions.html#place-expressions-and-value-expressions
2431
[path]: paths.html
2532
[`static mut`]: items/static-items.html#mutable-statics
26-
[`unsafe` block]: expressions/block-expr.html#unsafe-blocks
33+
[`unsafe` block]: expressions/block-expr.html#unsafe-blocks

src/items/structs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
>
2626
> _StructField_ :\
2727
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
28-
> &nbsp;&nbsp; [_Visibility_]
28+
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup>\
2929
> &nbsp;&nbsp; [IDENTIFIER] `:` [_Type_]
3030
>
3131
> _TupleFields_ :\
3232
> &nbsp;&nbsp; _TupleField_ (`,` _TupleField_)<sup>\*</sup> `,`<sup>?</sup>
3333
>
3434
> _TupleField_ :\
3535
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
36-
> &nbsp;&nbsp; [_Visibility_]
36+
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup>\
3737
> &nbsp;&nbsp; [_Type_]
3838
3939
A _struct_ is a nominal [struct type] defined with the keyword `struct`.

src/items/use-declarations.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax:</sup>**\
44
> _UseDeclaration_ :\
5-
> &nbsp;&nbsp; ([_Visibility_])<sup>?</sup> `use` _UseTree_ `;`
5+
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup> `use` _UseTree_ `;`
66
>
77
> _UseTree_ :\
88
> &nbsp;&nbsp; &nbsp;&nbsp; ([_SimplePath_]<sup>?</sup> `::`)<sup>?</sup> `*`\
@@ -57,6 +57,8 @@ fn main() {
5757
}
5858
```
5959

60+
## `use` Visibility
61+
6062
Like items, `use` declarations are private to the containing module, by
6163
default. Also like items, a `use` declaration can be public, if qualified by
6264
the `pub` keyword. Such a `use` declaration serves to _re-export_ a name. A
@@ -82,32 +84,32 @@ mod quux {
8284
In this example, the module `quux` re-exports two public names defined in
8385
`foo`.
8486

85-
Also note that the paths contained in `use` items are relative to the crate
86-
root. So, in the previous example, the `use` refers to `quux::foo::{bar, baz}`,
87-
and not simply to `foo::{bar, baz}`. This also means that top-level module
88-
declarations should be at the crate root if direct usage of the declared
89-
modules within `use` items is desired. It is also possible to use `self` and
90-
`super` at the beginning of a `use` item to refer to the current and direct
91-
parent modules respectively. All rules regarding accessing declared modules in
92-
`use` declarations apply to both module declarations and `extern crate`
93-
declarations.
87+
## `use` Paths
88+
89+
Paths in `use` items must start with a crate name or one of the [path
90+
qualifiers] `crate`, `self`, `super`, or `::`. `crate` refers to the current
91+
crate. `self` refers to the current module. `super` refers to the parent
92+
module. `::` can be used to explicitly refer to a crate, requiring an extern
93+
crate name to follow.
9494

9595
An example of what will and will not work for `use` items:
96+
<!-- Note: This example works as-is in either 2015 or 2018. -->
9697

9798
```rust
9899
# #![allow(unused_imports)]
99-
use foo::baz::foobaz; // good: foo is at the root of the crate
100+
use std::path::{self, Path, PathBuf}; // good: std is a crate name
101+
use crate::foo::baz::foobaz; // good: foo is at the root of the crate
100102

101103
mod foo {
102104

103105
mod example {
104106
pub mod iter {}
105107
}
106108

107-
use foo::example::iter; // good: foo is at crate root
108-
// use example::iter; // bad: example is not at the crate root
109+
use crate::foo::example::iter; // good: foo is at crate root
110+
// use example::iter; // bad: relative paths are not allowed without `self`
109111
use self::baz::foobaz; // good: self refers to module 'foo'
110-
use foo::bar::foobar; // good: foo is at crate root
112+
use crate::foo::bar::foobar; // good: foo is at crate root
111113

112114
pub mod bar {
113115
pub fn foobar() { }
@@ -122,6 +124,33 @@ mod foo {
122124
fn main() {}
123125
```
124126

127+
> **Edition Differences**: In the 2015 Edition, `use` paths also allow
128+
> accessing items in the crate root. Using the example above, the following
129+
> `use` paths work in 2015 but not 2018:
130+
>
131+
> ```rust,ignore
132+
> use foo::example::iter;
133+
> use ::foo::baz::foobaz;
134+
> ```
135+
>
136+
> In the 2018 Edition, if an in-scope item has the same name as an external
137+
> crate, then `use` of that crate name requires a leading `::` to
138+
> unambiguously select the crate name. This is to retain compatibility with
139+
> potential future changes. <!-- uniform_paths future-proofing -->
140+
>
141+
> ```rust,edition2018
142+
> // use std::fs; // Error, this is ambiguous.
143+
> use ::std::fs; // Imports from the `std` crate, not the module below.
144+
> use self::std::fs as self_fs; // Imports the module below.
145+
>
146+
> mod std {
147+
> pub mod fs {}
148+
> }
149+
> # fn main() {}
150+
> ```
151+
152+
125153
[IDENTIFIER]: identifiers.html
126-
[_SimplePath_]: paths.html
154+
[_SimplePath_]: paths.html#simple-paths
127155
[_Visibility_]: visibility-and-privacy.html
156+
[path qualifiers]: paths.html#path-qualifiers

0 commit comments

Comments
 (0)