@@ -20,6 +20,7 @@ pub enum CtorOf {
20
20
Variant ,
21
21
}
22
22
23
+ /// What kind of constructor something is.
23
24
#[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , Hash , Debug ) ]
24
25
#[ derive( HashStable_Generic ) ]
25
26
pub enum CtorKind {
@@ -31,6 +32,7 @@ pub enum CtorKind {
31
32
Fictive ,
32
33
}
33
34
35
+ /// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
34
36
#[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , Hash , Debug ) ]
35
37
#[ derive( HashStable_Generic ) ]
36
38
pub enum NonMacroAttrKind {
@@ -47,33 +49,51 @@ pub enum NonMacroAttrKind {
47
49
Registered ,
48
50
}
49
51
52
+ /// What kind of definition something is; e.g., `mod` vs `struct`.
50
53
#[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , Hash , Debug ) ]
51
54
#[ derive( HashStable_Generic ) ]
52
55
pub enum DefKind {
53
56
// Type namespace
54
57
Mod ,
55
- /// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
58
+ /// Refers to the struct itself, [ `DefKind::Ctor`] refers to its constructor if it exists.
56
59
Struct ,
57
60
Union ,
58
61
Enum ,
59
- /// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
62
+ /// Refers to the variant itself, [ `DefKind::Ctor`] refers to its constructor if it exists.
60
63
Variant ,
61
64
Trait ,
62
- /// `type Foo = Bar;`
65
+ /// Type alias: `type Foo = Bar;`
63
66
TyAlias ,
67
+ /// Type from an `extern` block.
64
68
ForeignTy ,
69
+ /// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
65
70
TraitAlias ,
71
+ /// Associated type: `trait MyTrait { type Assoc; }`
66
72
AssocTy ,
73
+ /// Type parameter: the `T` in `struct Vec<T> { ... }`
67
74
TyParam ,
68
75
69
76
// Value namespace
70
77
Fn ,
71
78
Const ,
79
+ /// Constant generic parameter: `struct Foo<const N: usize> { ... }`
72
80
ConstParam ,
73
81
Static ,
74
82
/// Refers to the struct or enum variant's constructor.
83
+ ///
84
+ /// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
85
+ /// [`DefKind::Variant`] is because structs and enum variants exist
86
+ /// in the *type* namespace, whereas struct and enum variant *constructors*
87
+ /// exist in the *value* namespace.
88
+ ///
89
+ /// You may wonder why enum variants exist in the type namespace as opposed
90
+ /// to the value namespace. Check out [RFC 2593] for intuition on why that is.
91
+ ///
92
+ /// [RFC 2593]: https://github.com/rust-lang/rfcs/pull/2593
75
93
Ctor ( CtorOf , CtorKind ) ,
94
+ /// Associated function: `impl MyStruct { fn associated() {} }`
76
95
AssocFn ,
96
+ /// Associated constant: `trait MyTrait { const ASSOC: usize; }`
77
97
AssocConst ,
78
98
79
99
// Macro namespace
@@ -82,11 +102,16 @@ pub enum DefKind {
82
102
// Not namespaced (or they are, but we don't treat them so)
83
103
ExternCrate ,
84
104
Use ,
105
+ /// An `extern` block.
85
106
ForeignMod ,
107
+ /// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`, or `const { 1 + 2}`
86
108
AnonConst ,
109
+ /// Opaque type, aka `impl Trait`.
87
110
OpaqueTy ,
88
111
Field ,
112
+ /// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
89
113
LifetimeParam ,
114
+ /// A use of [`global_asm!`].
90
115
GlobalAsm ,
91
116
Impl ,
92
117
Closure ,
@@ -196,35 +221,130 @@ impl DefKind {
196
221
}
197
222
198
223
/// The resolution of a path or export.
224
+ ///
225
+ /// For every path or identifier in Rust, the compiler must determine
226
+ /// what the path refers to. This process is called name resolution,
227
+ /// and `Res` is the primary result of name resolution.
228
+ ///
229
+ /// For example, everything prefixed with `/* Res */` in this example has
230
+ /// an associated `Res`:
231
+ ///
232
+ /// ```
233
+ /// fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
234
+ /// /* Res */ String::from(/* Res */ s)
235
+ /// }
236
+ ///
237
+ /// /* Res */ str_to_string("hello");
238
+ /// ```
239
+ ///
240
+ /// The associated `Res`s will be:
241
+ ///
242
+ /// - `str` will resolve to [`Res::PrimTy`];
243
+ /// - `String` will resolve to [`Res::Def`], and the `Res` will include the [`DefId`]
244
+ /// for `String` as defined in the standard library;
245
+ /// - `String::from` will also resolve to [`Res::Def`], with the [`DefId`]
246
+ /// pointing to `String::from`;
247
+ /// - `s` will resolve to [`Res::Local`];
248
+ /// - the call to `str_to_string` will resolve to [`Res::Def`], with the [`DefId`]
249
+ /// pointing to the definition of `str_to_string` in the current crate.
250
+ //
199
251
#[ derive( Clone , Copy , PartialEq , Eq , Encodable , Decodable , Hash , Debug ) ]
200
252
#[ derive( HashStable_Generic ) ]
201
253
pub enum Res < Id = hir:: HirId > {
254
+ /// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
255
+ ///
256
+ /// **Not bound to a specific namespace.**
202
257
Def ( DefKind , DefId ) ,
203
258
204
259
// Type namespace
260
+ /// A primitive type such as `i32` or `str`.
261
+ ///
262
+ /// **Belongs to the type namespace.**
205
263
PrimTy ( hir:: PrimTy ) ,
206
- /// `Self`, with both an optional trait and impl `DefId`.
264
+ /// The `Self` type, optionally with the trait it is associated with
265
+ /// and optionally with the [`DefId`] of the impl it is associated with.
266
+ ///
267
+ /// **Belongs to the type namespace.**
268
+ ///
269
+ /// For example, the `Self` in
207
270
///
208
- /// HACK(min_const_generics): impl self types also have an optional requirement to not mention
271
+ /// ```
272
+ /// trait Foo {
273
+ /// fn foo() -> Box<Self>;
274
+ /// }
275
+ /// ```
276
+ ///
277
+ /// would have the [`DefId`] of `Foo` associated with it. The `Self` in
278
+ ///
279
+ /// ```
280
+ /// struct Bar;
281
+ ///
282
+ /// impl Bar {
283
+ /// fn new() -> Self { Bar }
284
+ /// }
285
+ /// ```
286
+ ///
287
+ /// would have the [`DefId`] of the impl associated with it. Finally, the `Self` in
288
+ ///
289
+ /// ```
290
+ /// impl Foo for Bar {
291
+ /// fn foo() -> Box<Self> { Box::new(Bar) }
292
+ /// }
293
+ /// ```
294
+ ///
295
+ /// would have both the [`DefId`] of `Foo` and the [`DefId`] of the impl
296
+ /// associated with it.
297
+ ///
298
+ /// *See also [`Res::SelfCtor`].*
299
+ ///
300
+ /// -----
301
+ ///
302
+ /// HACK(min_const_generics): impl self types also have an optional requirement to **not** mention
209
303
/// any generic parameters to allow the following with `min_const_generics`:
210
- /// ```rust
211
- /// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] {} }
304
+ /// ```
305
+ /// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
212
306
/// ```
213
307
/// We do however allow `Self` in repeat expression even if it is generic to not break code
214
308
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat lint.
215
309
///
216
310
/// FIXME(lazy_normalization_consts): Remove this bodge once that feature is stable.
217
- SelfTy ( Option < DefId > /* trait */ , Option < ( DefId , bool ) > /* impl */ ) ,
218
- ToolMod , // e.g., `rustfmt` in `#[rustfmt::skip]`
311
+ SelfTy (
312
+ /// Optionally, the trait associated with this `Self` type.
313
+ Option < DefId > ,
314
+ /// Optionally, the impl associated with this `Self` type.
315
+ Option < ( DefId , bool ) > ,
316
+ ) ,
317
+ /// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
318
+ ///
319
+ /// **Belongs to the type namespace.**
320
+ ToolMod ,
219
321
220
322
// Value namespace
221
- SelfCtor ( DefId /* impl */ ) , // `DefId` refers to the impl
323
+ /// The `Self` constructor, along with the [`DefId`]
324
+ /// of the impl it is associated with.
325
+ ///
326
+ /// **Belongs to the value namespace.**
327
+ ///
328
+ /// *See also [`Res::SelfTy`].*
329
+ SelfCtor ( DefId ) ,
330
+ /// A local variable or function parameter.
331
+ ///
332
+ /// **Belongs to the value namespace.**
222
333
Local ( Id ) ,
223
334
224
335
// Macro namespace
336
+ /// An attribute that is *not* implemented via macro.
337
+ /// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
338
+ /// as opposed to `#[test]`, which is a builtin macro.
339
+ ///
340
+ /// **Belongs to the macro namespace.**
225
341
NonMacroAttr ( NonMacroAttrKind ) , // e.g., `#[inline]` or `#[rustfmt::skip]`
226
342
227
343
// All namespaces
344
+ /// Name resolution failed. We use a dummy `Res` variant so later phases
345
+ /// of the compiler won't crash and can instead report more errors.
346
+ ///
347
+ /// **Not bound to a specific namespace.**
228
348
Err ,
229
349
}
230
350
@@ -275,17 +395,26 @@ impl PartialRes {
275
395
}
276
396
}
277
397
278
- /// Different kinds of symbols don't influence each other.
279
- ///
280
- /// Therefore, they have a separate universe (namespace).
398
+ /// Different kinds of symbols can coexist even if they share the same textual name.
399
+ /// Therefore, they each have a separate universe (known as a "namespace").
281
400
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
282
401
pub enum Namespace {
402
+ /// The type namespace includes `struct`s, `enum`s, `union`s, `trait`s, and `mod`s
403
+ /// (and, by extension, crates).
404
+ ///
405
+ /// Note that the type namespace includes other items; this is not an
406
+ /// exhaustive list.
283
407
TypeNS ,
408
+ /// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
284
409
ValueNS ,
410
+ /// The macro namespace includes `macro_rules!` macros, declarative `macro`s,
411
+ /// procedural macros, attribute macros, `derive` macros, and non-macro attributes
412
+ /// like `#[inline]` and `#[rustfmt::skip]`.
285
413
MacroNS ,
286
414
}
287
415
288
416
impl Namespace {
417
+ /// The English description of the namespace.
289
418
pub fn descr ( self ) -> & ' static str {
290
419
match self {
291
420
Self :: TypeNS => "type" ,
0 commit comments