Skip to content

Commit 37cc523

Browse files
authored
Merge pull request #132 from Havvy/follow-the-path
Canonical Paths
2 parents 7e3f99e + e675bb9 commit 37cc523

File tree

2 files changed

+85
-22
lines changed

2 files changed

+85
-22
lines changed

src/items/constant-items.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@ wherever they are used, meaning that they are copied directly into the relevant
66
context when used. References to the same constant are not necessarily
77
guaranteed to refer to the same memory address.
88

9-
[constant value]: expressions.html#constant-expressions
10-
119
Constant values must not have destructors, and otherwise permit most forms of
1210
data. Constants may refer to the address of other constants, in which case the
1311
address will have elided lifetimes where applicable, otherwise – in most cases
1412
– defaulting to the `static` lifetime. (See below on [static lifetime
1513
elision].) The compiler is, however, still at liberty to translate the constant
1614
many times, so the address referred to may not be stable.
1715

18-
[static lifetime elision]: items/static-items.html#static-lifetime-elision
19-
2016
Constants must be explicitly typed. The type may be any type that doesn't
2117
implement [`Drop`] and has a `'static` lifetime: any references it contains
2218
must have `'static` lifetimes.
2319

24-
[`Drop`]: the-drop-trait.html
25-
2620
```rust
2721
const BIT1: u32 = 1 << 0;
2822
const BIT2: u32 = 1 << 1;
@@ -40,3 +34,7 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
4034
mystring: STRING,
4135
};
4236
```
37+
38+
[constant value]: expressions.html#constant-expressions
39+
[static lifetime elision]: items/static-items.html#static-lifetime-elision
40+
[`Drop`]: the-drop-trait.html

src/paths.md

+81-16
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,18 @@ a namespace qualifier (`::`). If a path consists of only one component, it may
55
refer to either an [item] or a [variable] in a local control
66
scope. If a path has multiple components, it refers to an item.
77

8-
[item]: items.html
9-
[variable]: variables.html
10-
11-
Every item has a _canonical path_ within its crate, but the path naming an item
12-
is only meaningful within a given crate. There is no global namespace across
13-
crates; an item's canonical path merely identifies it within the crate.
14-
158
Two examples of simple paths consisting of only identifier components:
169

1710
```rust,ignore
1811
x;
1912
x::y::z;
2013
```
2114

22-
Path components are usually [identifiers], but they may
23-
also include angle-bracket-enclosed lists of type arguments. In
24-
[expression] context, the type argument list is given
25-
after a `::` namespace qualifier in order to disambiguate it from a
26-
relational expression involving the less-than symbol (`<`). In type
27-
expression context, the final namespace qualifier is omitted.
28-
29-
[identifiers]: identifiers.html
30-
[expression]: expressions.html
15+
Path components are usually [identifiers], but they may also include
16+
angle-bracket-enclosed lists of type arguments. In [expression] context, the
17+
type argument list is given after a `::` namespace qualifier in order to
18+
disambiguate it from a relational expression involving the less-than symbol
19+
(`<`). In type expression context, the final namespace qualifier is omitted.
3120

3221
Two examples of paths with type arguments:
3322

@@ -103,3 +92,79 @@ mod a {
10392
}
10493
# fn main() {}
10594
```
95+
96+
## Canonical paths
97+
98+
Items defined in a module or implementation have a *canonical path* that
99+
corresponds to where within its crate it is defined. All other paths to these
100+
items are aliases. The canonical path is defined as a *path prefix* appended by
101+
the path component the item itself defines.
102+
103+
[Implementations] and [use declarations] do not have canonical paths, although
104+
the items that implementations define do have them. Items defined in
105+
block expressions do not have canonical paths. Items defined in a module that
106+
does not have a canonical path do not have a canonical path. Associated items
107+
defined in an implementation that refers to an item without a canonical path,
108+
e.g. as the implementing type, the trait being implemented, a type parameter or
109+
bound on a type parameter, do not have canonical paths.
110+
111+
The path prefix for modules is the canonical path to that module. For bare
112+
implementations, it is the canonical path of the item being implemented
113+
surrounded by angle (`<>`) brackets. For trait implementations, it is the
114+
canonical path of the item being implemented followed by `as` followed by the
115+
canonical path to the trait all surrounded in angle (`<>`) brackets.
116+
117+
The canonical path is only meaningful within a given crate. There is no global
118+
namespace across crates; an item's canonical path merely identifies it within
119+
the crate.
120+
121+
```rust
122+
// Comments show the canonical path of the item.
123+
124+
mod a { // ::a
125+
pub struct Struct; // ::a::Struct
126+
127+
pub trait Trait { // ::a::Trait
128+
fn f(&self); // a::Trait::f
129+
}
130+
131+
impl Trait for Struct {
132+
fn f(&self) {} // <::a::Struct as ::a::Trait>::f
133+
}
134+
135+
impl Struct {
136+
fn g(&self) {} // <::a::Struct>::g
137+
}
138+
}
139+
140+
mod without { // ::without
141+
fn canonicals() { // ::without::canonicals
142+
struct OtherStruct; // None
143+
144+
trait OtherTrait { // None
145+
fn g(&self); // None
146+
}
147+
148+
impl OtherTrait for OtherStruct {
149+
fn g(&self) {} // None
150+
}
151+
152+
impl OtherTrait for ::a::Struct {
153+
fn g(&self) {} // None
154+
}
155+
156+
impl ::a::Trait for OtherStruct {
157+
fn f(&self) {} // None
158+
}
159+
}
160+
}
161+
162+
# fn main() {}
163+
```
164+
[item]: items.html
165+
[variable]: variables.html
166+
[identifiers]: identifiers.html
167+
[expression]: expressions.html
168+
[implementations]: items/implementations.html
169+
[modules]: items/modules.html
170+
[use declarations]: items/use_declarations.html

0 commit comments

Comments
 (0)