]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir/src/def.rs
bump version to 1.81.0+dfsg1-2~bpo12+pve1
[rustc.git] / compiler / rustc_hir / src / def.rs
CommitLineData
4b012472 1use crate::definitions::DefPathData;
e1599b0c 2use crate::hir;
e1599b0c 3
3dfed10e
XL
4use rustc_ast as ast;
5use rustc_ast::NodeId;
9ffffee4 6use rustc_data_structures::stable_hasher::ToStableHashKey;
4b012472 7use rustc_data_structures::unord::UnordMap;
31ef2f64 8use rustc_macros::{Decodable, Encodable, HashStable_Generic};
923072b8 9use rustc_span::def_id::{DefId, LocalDefId};
dfeec247 10use rustc_span::hygiene::MacroKind;
c620b35d 11use rustc_span::symbol::kw;
5869c6ff 12use rustc_span::Symbol;
1a4d82fc 13
1b1a35ee 14use std::array::IntoIter;
e1599b0c 15use std::fmt::Debug;
94b46f34 16
48663c56 17/// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
4b012472 18#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
532ac7d7 19pub enum CtorOf {
48663c56 20 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
532ac7d7 21 Struct,
48663c56 22 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
532ac7d7
XL
23 Variant,
24}
25
cdc7bbd5 26/// What kind of constructor something is.
4b012472 27#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
c30ab7b3 28pub enum CtorKind {
041b39d2 29 /// Constructor function automatically created by a tuple struct/variant.
c30ab7b3 30 Fn,
041b39d2 31 /// Constructor constant automatically created by a unit struct/variant.
c30ab7b3 32 Const,
c30ab7b3
SL
33}
34
cdc7bbd5 35/// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
4b012472 36#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
b7449926
XL
37pub enum NonMacroAttrKind {
38 /// Single-segment attribute defined by the language (`#[inline]`)
5869c6ff 39 Builtin(Symbol),
b7449926
XL
40 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
41 Tool,
42 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
43 DeriveHelper,
fc512014
XL
44 /// Single-segment custom attribute registered by a derive macro
45 /// but used before that derive macro was expanded (deprecated).
46 DeriveHelperCompat,
b7449926
XL
47}
48
cdc7bbd5 49/// What kind of definition something is; e.g., `mod` vs `struct`.
4b012472
FG
50/// `enum DefPathData` may need to be updated if a new variant is added here.
51#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
48663c56 52pub enum DefKind {
c30ab7b3 53 // Type namespace
48663c56 54 Mod,
cdc7bbd5 55 /// Refers to the struct itself, [`DefKind::Ctor`] refers to its constructor if it exists.
48663c56
XL
56 Struct,
57 Union,
58 Enum,
cdc7bbd5 59 /// Refers to the variant itself, [`DefKind::Ctor`] refers to its constructor if it exists.
48663c56
XL
60 Variant,
61 Trait,
cdc7bbd5 62 /// Type alias: `type Foo = Bar;`
781aab86 63 TyAlias,
cdc7bbd5 64 /// Type from an `extern` block.
48663c56 65 ForeignTy,
cdc7bbd5 66 /// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
48663c56 67 TraitAlias,
cdc7bbd5 68 /// Associated type: `trait MyTrait { type Assoc; }`
dc9dc135 69 AssocTy,
cdc7bbd5 70 /// Type parameter: the `T` in `struct Vec<T> { ... }`
48663c56
XL
71 TyParam,
72
73 // Value namespace
74 Fn,
75 Const,
cdc7bbd5 76 /// Constant generic parameter: `struct Foo<const N: usize> { ... }`
48663c56 77 ConstParam,
c620b35d 78 Static {
31ef2f64
FG
79 /// Whether it's a `unsafe static`, `safe static` (inside extern only) or just a `static`.
80 safety: hir::Safety,
c620b35d
FG
81 /// Whether it's a `static mut` or just a `static`.
82 mutability: ast::Mutability,
83 /// Whether it's an anonymous static generated for nested allocations.
84 nested: bool,
85 },
48663c56 86 /// Refers to the struct or enum variant's constructor.
cdc7bbd5
XL
87 ///
88 /// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
89 /// [`DefKind::Variant`] is because structs and enum variants exist
90 /// in the *type* namespace, whereas struct and enum variant *constructors*
91 /// exist in the *value* namespace.
92 ///
93 /// You may wonder why enum variants exist in the type namespace as opposed
94 /// to the value namespace. Check out [RFC 2593] for intuition on why that is.
95 ///
96 /// [RFC 2593]: https://github.com/rust-lang/rfcs/pull/2593
48663c56 97 Ctor(CtorOf, CtorKind),
cdc7bbd5 98 /// Associated function: `impl MyStruct { fn associated() {} }`
923072b8 99 /// or `trait Foo { fn associated() {} }`
ba9703b0 100 AssocFn,
cdc7bbd5 101 /// Associated constant: `trait MyTrait { const ASSOC: usize; }`
dc9dc135 102 AssocConst,
48663c56
XL
103
104 // Macro namespace
105 Macro(MacroKind),
f9f354fc
XL
106
107 // Not namespaced (or they are, but we don't treat them so)
108 ExternCrate,
109 Use,
cdc7bbd5 110 /// An `extern` block.
f9f354fc 111 ForeignMod,
3c0e092e 112 /// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
f9f354fc 113 AnonConst,
3c0e092e
XL
114 /// An inline constant, e.g. `const { 1 + 2 }`
115 InlineConst,
cdc7bbd5 116 /// Opaque type, aka `impl Trait`.
f035d41b 117 OpaqueTy,
e8be2606
FG
118 /// A field in a struct, enum or union. e.g.
119 /// - `bar` in `struct Foo { bar: u8 }`
120 /// - `Foo::Bar::0` in `enum Foo { Bar(u8) }`
f9f354fc 121 Field,
cdc7bbd5 122 /// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
f9f354fc 123 LifetimeParam,
a2a8927a 124 /// A use of `global_asm!`.
f9f354fc 125 GlobalAsm,
9ffffee4
FG
126 Impl {
127 of_trait: bool,
128 },
c620b35d
FG
129 /// A closure, coroutine, or coroutine-closure.
130 ///
131 /// These are all represented with the same `ExprKind::Closure` in the AST and HIR,
132 /// which makes it difficult to distinguish these during def collection. Therefore,
133 /// we treat them all the same, and code which needs to distinguish them can match
134 /// or `hir::ClosureKind` or `type_of`.
f9f354fc 135 Closure,
48663c56
XL
136}
137
138impl DefKind {
9ffffee4
FG
139 /// Get an English description for the item's kind.
140 ///
141 /// If you have access to `TyCtxt`, use `TyCtxt::def_descr` or
142 /// `TyCtxt::def_kind_descr` instead, because they give better
ed00b5ec 143 /// information for coroutines and associated functions.
416331ca 144 pub fn descr(self, def_id: DefId) -> &'static str {
48663c56
XL
145 match self {
146 DefKind::Fn => "function",
04454e1e 147 DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate",
48663c56 148 DefKind::Mod => "module",
c620b35d 149 DefKind::Static { .. } => "static",
48663c56
XL
150 DefKind::Enum => "enum",
151 DefKind::Variant => "variant",
152 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
153 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
48663c56
XL
154 DefKind::Struct => "struct",
155 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
156 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
416331ca 157 DefKind::OpaqueTy => "opaque type",
781aab86 158 DefKind::TyAlias => "type alias",
48663c56 159 DefKind::TraitAlias => "trait alias",
dc9dc135 160 DefKind::AssocTy => "associated type",
48663c56
XL
161 DefKind::Union => "union",
162 DefKind::Trait => "trait",
163 DefKind::ForeignTy => "foreign type",
ba9703b0 164 DefKind::AssocFn => "associated function",
48663c56 165 DefKind::Const => "constant",
dc9dc135 166 DefKind::AssocConst => "associated constant",
48663c56
XL
167 DefKind::TyParam => "type parameter",
168 DefKind::ConstParam => "const parameter",
169 DefKind::Macro(macro_kind) => macro_kind.descr(),
f9f354fc
XL
170 DefKind::LifetimeParam => "lifetime parameter",
171 DefKind::Use => "import",
172 DefKind::ForeignMod => "foreign module",
173 DefKind::AnonConst => "constant expression",
3c0e092e 174 DefKind::InlineConst => "inline constant",
f9f354fc 175 DefKind::Field => "field",
9ffffee4 176 DefKind::Impl { .. } => "implementation",
f9f354fc 177 DefKind::Closure => "closure",
f9f354fc
XL
178 DefKind::ExternCrate => "extern crate",
179 DefKind::GlobalAsm => "global assembly block",
48663c56
XL
180 }
181 }
182
e1599b0c 183 /// Gets an English article for the definition.
9ffffee4
FG
184 ///
185 /// If you have access to `TyCtxt`, use `TyCtxt::def_descr_article` or
186 /// `TyCtxt::def_kind_descr_article` instead, because they give better
ed00b5ec 187 /// information for coroutines and associated functions.
48663c56
XL
188 pub fn article(&self) -> &'static str {
189 match *self {
dc9dc135
XL
190 DefKind::AssocTy
191 | DefKind::AssocConst
ba9703b0 192 | DefKind::AssocFn
48663c56 193 | DefKind::Enum
f9f354fc 194 | DefKind::OpaqueTy
9ffffee4 195 | DefKind::Impl { .. }
f9f354fc 196 | DefKind::Use
3c0e092e 197 | DefKind::InlineConst
f9f354fc 198 | DefKind::ExternCrate => "an",
48663c56
XL
199 DefKind::Macro(macro_kind) => macro_kind.article(),
200 _ => "a",
201 }
202 }
60c5eb7d 203
3dfed10e 204 pub fn ns(&self) -> Option<Namespace> {
60c5eb7d
XL
205 match self {
206 DefKind::Mod
207 | DefKind::Struct
208 | DefKind::Union
209 | DefKind::Enum
210 | DefKind::Variant
211 | DefKind::Trait
781aab86 212 | DefKind::TyAlias
60c5eb7d
XL
213 | DefKind::ForeignTy
214 | DefKind::TraitAlias
215 | DefKind::AssocTy
3dfed10e 216 | DefKind::TyParam => Some(Namespace::TypeNS),
60c5eb7d
XL
217
218 DefKind::Fn
219 | DefKind::Const
220 | DefKind::ConstParam
c620b35d 221 | DefKind::Static { .. }
60c5eb7d 222 | DefKind::Ctor(..)
ba9703b0 223 | DefKind::AssocFn
3dfed10e 224 | DefKind::AssocConst => Some(Namespace::ValueNS),
60c5eb7d 225
3dfed10e 226 DefKind::Macro(..) => Some(Namespace::MacroNS),
f9f354fc
XL
227
228 // Not namespaced.
229 DefKind::AnonConst
3c0e092e 230 | DefKind::InlineConst
f9f354fc
XL
231 | DefKind::Field
232 | DefKind::LifetimeParam
233 | DefKind::ExternCrate
234 | DefKind::Closure
f9f354fc
XL
235 | DefKind::Use
236 | DefKind::ForeignMod
237 | DefKind::GlobalAsm
e8be2606
FG
238 | DefKind::Impl { .. }
239 | DefKind::OpaqueTy => None,
60c5eb7d
XL
240 }
241 }
04454e1e 242
4b012472
FG
243 pub fn def_path_data(self, name: Symbol) -> DefPathData {
244 match self {
c620b35d 245 DefKind::Struct | DefKind::Union if name == kw::Empty => DefPathData::AnonAdt,
4b012472
FG
246 DefKind::Mod
247 | DefKind::Struct
248 | DefKind::Union
249 | DefKind::Enum
250 | DefKind::Variant
251 | DefKind::Trait
252 | DefKind::TyAlias
253 | DefKind::ForeignTy
254 | DefKind::TraitAlias
255 | DefKind::AssocTy
256 | DefKind::TyParam
257 | DefKind::ExternCrate => DefPathData::TypeNs(name),
c620b35d
FG
258 // It's not exactly an anon const, but wrt DefPathData, there
259 // is no difference.
260 DefKind::Static { nested: true, .. } => DefPathData::AnonConst,
4b012472
FG
261 DefKind::Fn
262 | DefKind::Const
263 | DefKind::ConstParam
c620b35d 264 | DefKind::Static { .. }
4b012472
FG
265 | DefKind::AssocFn
266 | DefKind::AssocConst
267 | DefKind::Field => DefPathData::ValueNs(name),
268 DefKind::Macro(..) => DefPathData::MacroNs(name),
269 DefKind::LifetimeParam => DefPathData::LifetimeNs(name),
270 DefKind::Ctor(..) => DefPathData::Ctor,
271 DefKind::Use => DefPathData::Use,
272 DefKind::ForeignMod => DefPathData::ForeignMod,
273 DefKind::AnonConst => DefPathData::AnonConst,
274 DefKind::InlineConst => DefPathData::AnonConst,
275 DefKind::OpaqueTy => DefPathData::OpaqueTy,
276 DefKind::GlobalAsm => DefPathData::GlobalAsm,
277 DefKind::Impl { .. } => DefPathData::Impl,
278 DefKind::Closure => DefPathData::Closure,
279 }
280 }
281
04454e1e
FG
282 #[inline]
283 pub fn is_fn_like(self) -> bool {
4b012472 284 matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure)
04454e1e
FG
285 }
286
287 /// Whether `query get_codegen_attrs` should be used with this definition.
288 pub fn has_codegen_attrs(self) -> bool {
289 match self {
290 DefKind::Fn
291 | DefKind::AssocFn
292 | DefKind::Ctor(..)
293 | DefKind::Closure
c620b35d 294 | DefKind::Static { .. } => true,
04454e1e
FG
295 DefKind::Mod
296 | DefKind::Struct
297 | DefKind::Union
298 | DefKind::Enum
299 | DefKind::Variant
300 | DefKind::Trait
781aab86 301 | DefKind::TyAlias
04454e1e
FG
302 | DefKind::ForeignTy
303 | DefKind::TraitAlias
304 | DefKind::AssocTy
305 | DefKind::Const
306 | DefKind::AssocConst
307 | DefKind::Macro(..)
308 | DefKind::Use
309 | DefKind::ForeignMod
310 | DefKind::OpaqueTy
9ffffee4 311 | DefKind::Impl { .. }
04454e1e
FG
312 | DefKind::Field
313 | DefKind::TyParam
314 | DefKind::ConstParam
315 | DefKind::LifetimeParam
316 | DefKind::AnonConst
317 | DefKind::InlineConst
318 | DefKind::GlobalAsm
319 | DefKind::ExternCrate => false,
320 }
321 }
48663c56
XL
322}
323
ba9703b0 324/// The resolution of a path or export.
cdc7bbd5
XL
325///
326/// For every path or identifier in Rust, the compiler must determine
327/// what the path refers to. This process is called name resolution,
328/// and `Res` is the primary result of name resolution.
329///
330/// For example, everything prefixed with `/* Res */` in this example has
331/// an associated `Res`:
332///
333/// ```
334/// fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
335/// /* Res */ String::from(/* Res */ s)
336/// }
337///
338/// /* Res */ str_to_string("hello");
339/// ```
340///
341/// The associated `Res`s will be:
342///
343/// - `str` will resolve to [`Res::PrimTy`];
344/// - `String` will resolve to [`Res::Def`], and the `Res` will include the [`DefId`]
345/// for `String` as defined in the standard library;
346/// - `String::from` will also resolve to [`Res::Def`], with the [`DefId`]
347/// pointing to `String::from`;
348/// - `s` will resolve to [`Res::Local`];
349/// - the call to `str_to_string` will resolve to [`Res::Def`], with the [`DefId`]
350/// pointing to the definition of `str_to_string` in the current crate.
351//
4b012472 352#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
48663c56 353pub enum Res<Id = hir::HirId> {
cdc7bbd5
XL
354 /// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
355 ///
356 /// **Not bound to a specific namespace.**
48663c56
XL
357 Def(DefKind, DefId),
358
359 // Type namespace
cdc7bbd5
XL
360 /// A primitive type such as `i32` or `str`.
361 ///
362 /// **Belongs to the type namespace.**
7453a54e 363 PrimTy(hir::PrimTy),
f2b60f7d 364
2b03887a
FG
365 /// The `Self` type, as used within a trait.
366 ///
367 /// **Belongs to the type namespace.**
368 ///
369 /// See the examples on [`Res::SelfTyAlias`] for details.
370 SelfTyParam {
371 /// The trait this `Self` is a generic parameter for.
372 trait_: DefId,
373 },
374
375 /// The `Self` type, as used somewhere other than within a trait.
cdc7bbd5
XL
376 ///
377 /// **Belongs to the type namespace.**
378 ///
5099ac24 379 /// Examples:
cdc7bbd5 380 /// ```
2b03887a 381 /// struct Bar(Box<Self>); // SelfTyAlias
5099ac24 382 ///
cdc7bbd5 383 /// trait Foo {
2b03887a 384 /// fn foo() -> Box<Self>; // SelfTyParam
cdc7bbd5 385 /// }
cdc7bbd5
XL
386 ///
387 /// impl Bar {
5099ac24 388 /// fn blah() {
2b03887a 389 /// let _: Self; // SelfTyAlias
5099ac24 390 /// }
cdc7bbd5 391 /// }
cdc7bbd5 392 ///
cdc7bbd5 393 /// impl Foo for Bar {
2b03887a
FG
394 /// fn foo() -> Box<Self> { // SelfTyAlias
395 /// let _: Self; // SelfTyAlias
5099ac24
FG
396 ///
397 /// todo!()
398 /// }
cdc7bbd5
XL
399 /// }
400 /// ```
cdc7bbd5
XL
401 /// *See also [`Res::SelfCtor`].*
402 ///
2b03887a 403 SelfTyAlias {
5099ac24 404 /// The item introducing the `Self` type alias. Can be used in the `type_of` query
2b03887a
FG
405 /// to get the underlying type.
406 alias_to: DefId,
407
408 /// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
409 /// anonymous constant).
410 ///
411 /// HACK(min_const_generics): self types also have an optional requirement to **not**
412 /// mention any generic parameters to allow the following with `min_const_generics`:
413 /// ```
414 /// # struct Foo;
415 /// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
416 ///
417 /// struct Bar([u8; baz::<Self>()]);
418 /// const fn baz<T>() -> usize { 10 }
419 /// ```
420 /// We do however allow `Self` in repeat expression even if it is generic to not break code
421 /// which already works on stable while causing the `const_evaluatable_unchecked` future
422 /// compat lint:
423 /// ```
424 /// fn foo<T>() {
425 /// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
426 /// }
427 /// ```
428 // FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
429 forbid_generic: bool,
430
431 /// Is this within an `impl Foo for bar`?
432 is_trait_impl: bool,
5099ac24 433 },
f2b60f7d 434
c30ab7b3 435 // Value namespace
cdc7bbd5
XL
436 /// The `Self` constructor, along with the [`DefId`]
437 /// of the impl it is associated with.
438 ///
439 /// **Belongs to the value namespace.**
440 ///
2b03887a 441 /// *See also [`Res::SelfTyParam`] and [`Res::SelfTyAlias`].*
cdc7bbd5 442 SelfCtor(DefId),
f2b60f7d 443
cdc7bbd5
XL
444 /// A local variable or function parameter.
445 ///
446 /// **Belongs to the value namespace.**
48663c56 447 Local(Id),
c30ab7b3 448
2b03887a
FG
449 /// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
450 ///
451 /// **Belongs to the type namespace.**
452 ToolMod,
453
476ff2be 454 // Macro namespace
cdc7bbd5
XL
455 /// An attribute that is *not* implemented via macro.
456 /// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
457 /// as opposed to `#[test]`, which is a builtin macro.
458 ///
459 /// **Belongs to the macro namespace.**
0731742a 460 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
cc61c64b 461
48663c56 462 // All namespaces
cdc7bbd5
XL
463 /// Name resolution failed. We use a dummy `Res` variant so later phases
464 /// of the compiler won't crash and can instead report more errors.
465 ///
466 /// **Not bound to a specific namespace.**
7453a54e 467 Err,
c34b1796
AL
468}
469
48663c56
XL
470/// The result of resolving a path before lowering to HIR,
471/// with "module" segments resolved and associated item
472/// segments deferred to type checking.
473/// `base_res` is the resolution of the resolved part of the
8bb4bdeb
XL
474/// path, `unresolved_segments` is the number of unresolved
475/// segments.
c34b1796 476///
ff7c6d11
XL
477/// ```text
478/// module::Type::AssocX::AssocY::MethodOrAssocType
479/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48663c56 480/// base_res unresolved_segments = 3
ff7c6d11
XL
481///
482/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
483/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
48663c56 484/// base_res unresolved_segments = 2
ff7c6d11 485/// ```
c34b1796 486#[derive(Copy, Clone, Debug)]
48663c56
XL
487pub struct PartialRes {
488 base_res: Res<NodeId>,
8bb4bdeb 489 unresolved_segments: usize,
c34b1796
AL
490}
491
48663c56
XL
492impl PartialRes {
493 #[inline]
494 pub fn new(base_res: Res<NodeId>) -> Self {
495 PartialRes { base_res, unresolved_segments: 0 }
8bb4bdeb
XL
496 }
497
48663c56
XL
498 #[inline]
499 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
dfeec247
XL
500 if base_res == Res::Err {
501 unresolved_segments = 0
502 }
48663c56 503 PartialRes { base_res, unresolved_segments }
8bb4bdeb
XL
504 }
505
506 #[inline]
48663c56
XL
507 pub fn base_res(&self) -> Res<NodeId> {
508 self.base_res
8bb4bdeb
XL
509 }
510
511 #[inline]
512 pub fn unresolved_segments(&self) -> usize {
513 self.unresolved_segments
3157f602 514 }
2b03887a
FG
515
516 #[inline]
517 pub fn full_res(&self) -> Option<Res<NodeId>> {
518 (self.unresolved_segments == 0).then_some(self.base_res)
519 }
520
521 #[inline]
522 pub fn expect_full_res(&self) -> Res<NodeId> {
523 self.full_res().expect("unexpected unresolved segments")
524 }
1a4d82fc
JJ
525}
526
cdc7bbd5
XL
527/// Different kinds of symbols can coexist even if they share the same textual name.
528/// Therefore, they each have a separate universe (known as a "namespace").
9ffffee4
FG
529#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
530#[derive(HashStable_Generic)]
94b46f34 531pub enum Namespace {
cdc7bbd5
XL
532 /// The type namespace includes `struct`s, `enum`s, `union`s, `trait`s, and `mod`s
533 /// (and, by extension, crates).
534 ///
535 /// Note that the type namespace includes other items; this is not an
536 /// exhaustive list.
94b46f34 537 TypeNS,
cdc7bbd5 538 /// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
94b46f34 539 ValueNS,
cdc7bbd5
XL
540 /// The macro namespace includes `macro_rules!` macros, declarative `macro`s,
541 /// procedural macros, attribute macros, `derive` macros, and non-macro attributes
542 /// like `#[inline]` and `#[rustfmt::skip]`.
94b46f34
XL
543 MacroNS,
544}
545
8faf50e0 546impl Namespace {
cdc7bbd5 547 /// The English description of the namespace.
8faf50e0
XL
548 pub fn descr(self) -> &'static str {
549 match self {
dfeec247
XL
550 Self::TypeNS => "type",
551 Self::ValueNS => "value",
552 Self::MacroNS => "macro",
8faf50e0
XL
553 }
554 }
555}
556
9ffffee4
FG
557impl<CTX: crate::HashStableContext> ToStableHashKey<CTX> for Namespace {
558 type KeyType = Namespace;
559
560 #[inline]
561 fn to_stable_hash_key(&self, _: &CTX) -> Namespace {
562 *self
563 }
564}
565
94b46f34
XL
566/// Just a helper ‒ separate structure for each namespace.
567#[derive(Copy, Clone, Default, Debug)]
568pub struct PerNS<T> {
569 pub value_ns: T,
570 pub type_ns: T,
571 pub macro_ns: T,
572}
573
574impl<T> PerNS<T> {
575 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
dfeec247 576 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
94b46f34 577 }
1b1a35ee
XL
578
579 pub fn into_iter(self) -> IntoIter<T, 3> {
a2a8927a 580 [self.value_ns, self.type_ns, self.macro_ns].into_iter()
1b1a35ee
XL
581 }
582
583 pub fn iter(&self) -> IntoIter<&T, 3> {
a2a8927a 584 [&self.value_ns, &self.type_ns, &self.macro_ns].into_iter()
1b1a35ee 585 }
94b46f34
XL
586}
587
588impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
589 type Output = T;
0731742a 590
94b46f34
XL
591 fn index(&self, ns: Namespace) -> &T {
592 match ns {
dfeec247
XL
593 Namespace::ValueNS => &self.value_ns,
594 Namespace::TypeNS => &self.type_ns,
595 Namespace::MacroNS => &self.macro_ns,
94b46f34
XL
596 }
597 }
598}
599
600impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
601 fn index_mut(&mut self, ns: Namespace) -> &mut T {
602 match ns {
dfeec247
XL
603 Namespace::ValueNS => &mut self.value_ns,
604 Namespace::TypeNS => &mut self.type_ns,
605 Namespace::MacroNS => &mut self.macro_ns,
94b46f34
XL
606 }
607 }
608}
609
610impl<T> PerNS<Option<T>> {
9fa01778 611 /// Returns `true` if all the items in this collection are `None`.
94b46f34
XL
612 pub fn is_empty(&self) -> bool {
613 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
614 }
615
616 /// Returns an iterator over the items which are `Some`.
dfeec247 617 pub fn present_items(self) -> impl Iterator<Item = T> {
a2a8927a 618 [self.type_ns, self.value_ns, self.macro_ns].into_iter().flatten()
48663c56
XL
619 }
620}
621
c30ab7b3 622impl CtorKind {
487cf647 623 pub fn from_ast(vdata: &ast::VariantData) -> Option<(CtorKind, NodeId)> {
c30ab7b3 624 match *vdata {
487cf647
FG
625 ast::VariantData::Tuple(_, node_id) => Some((CtorKind::Fn, node_id)),
626 ast::VariantData::Unit(node_id) => Some((CtorKind::Const, node_id)),
4b012472 627 ast::VariantData::Struct { .. } => None,
c30ab7b3
SL
628 }
629 }
1a4d82fc
JJ
630}
631
b7449926 632impl NonMacroAttrKind {
69743fb6 633 pub fn descr(self) -> &'static str {
b7449926 634 match self {
5869c6ff 635 NonMacroAttrKind::Builtin(..) => "built-in attribute",
b7449926 636 NonMacroAttrKind::Tool => "tool attribute",
fc512014
XL
637 NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
638 "derive helper attribute"
639 }
60c5eb7d
XL
640 }
641 }
642
4b012472
FG
643 // Currently trivial, but exists in case a new kind is added in the future whose name starts
644 // with a vowel.
60c5eb7d 645 pub fn article(self) -> &'static str {
f2b60f7d 646 "a"
60c5eb7d
XL
647 }
648
649 /// Users of some attributes cannot mark them as used, so they are considered always used.
650 pub fn is_used(self) -> bool {
651 match self {
fc512014
XL
652 NonMacroAttrKind::Tool
653 | NonMacroAttrKind::DeriveHelper
654 | NonMacroAttrKind::DeriveHelperCompat => true,
f2b60f7d 655 NonMacroAttrKind::Builtin(..) => false,
b7449926
XL
656 }
657 }
658}
659
48663c56 660impl<Id> Res<Id> {
e1599b0c 661 /// Return the `DefId` of this `Def` if it has an ID, else panic.
48663c56
XL
662 pub fn def_id(&self) -> DefId
663 where
664 Id: Debug,
665 {
9c376795 666 self.opt_def_id().unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {self:?}"))
13cf67c4
XL
667 }
668
e1599b0c 669 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
13cf67c4 670 pub fn opt_def_id(&self) -> Option<DefId> {
1a4d82fc 671 match *self {
48663c56
XL
672 Res::Def(_, id) => Some(id),
673
dfeec247
XL
674 Res::Local(..)
675 | Res::PrimTy(..)
2b03887a
FG
676 | Res::SelfTyParam { .. }
677 | Res::SelfTyAlias { .. }
dfeec247
XL
678 | Res::SelfCtor(..)
679 | Res::ToolMod
680 | Res::NonMacroAttr(..)
681 | Res::Err => None,
1a4d82fc
JJ
682 }
683 }
684
48663c56 685 /// Return the `DefId` of this `Res` if it represents a module.
9fa01778
XL
686 pub fn mod_def_id(&self) -> Option<DefId> {
687 match *self {
48663c56 688 Res::Def(DefKind::Mod, id) => Some(id),
9fa01778
XL
689 _ => None,
690 }
691 }
692
48663c56
XL
693 /// A human readable name for the res kind ("function", "module", etc.).
694 pub fn descr(&self) -> &'static str {
54a0048b 695 match *self {
416331ca 696 Res::Def(kind, def_id) => kind.descr(def_id),
48663c56
XL
697 Res::SelfCtor(..) => "self constructor",
698 Res::PrimTy(..) => "builtin type",
699 Res::Local(..) => "local variable",
2b03887a 700 Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => "self type",
48663c56
XL
701 Res::ToolMod => "tool module",
702 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
703 Res::Err => "unresolved item",
54a0048b
SL
704 }
705 }
13cf67c4 706
e1599b0c 707 /// Gets an English article for the `Res`.
13cf67c4
XL
708 pub fn article(&self) -> &'static str {
709 match *self {
48663c56 710 Res::Def(kind, _) => kind.article(),
60c5eb7d 711 Res::NonMacroAttr(kind) => kind.article(),
48663c56 712 Res::Err => "an",
13cf67c4
XL
713 _ => "a",
714 }
715 }
48663c56
XL
716
717 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
718 match self {
719 Res::Def(kind, id) => Res::Def(kind, id),
720 Res::SelfCtor(id) => Res::SelfCtor(id),
721 Res::PrimTy(id) => Res::PrimTy(id),
722 Res::Local(id) => Res::Local(map(id)),
2b03887a
FG
723 Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
724 Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
725 Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
726 }
48663c56
XL
727 Res::ToolMod => Res::ToolMod,
728 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
729 Res::Err => Res::Err,
730 }
731 }
416331ca 732
5e7ed085
FG
733 pub fn apply_id<R, E>(self, mut map: impl FnMut(Id) -> Result<R, E>) -> Result<Res<R>, E> {
734 Ok(match self {
735 Res::Def(kind, id) => Res::Def(kind, id),
736 Res::SelfCtor(id) => Res::SelfCtor(id),
737 Res::PrimTy(id) => Res::PrimTy(id),
738 Res::Local(id) => Res::Local(map(id)?),
2b03887a
FG
739 Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
740 Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
741 Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
742 }
5e7ed085
FG
743 Res::ToolMod => Res::ToolMod,
744 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
745 Res::Err => Res::Err,
746 })
747 }
748
c295e0f8
XL
749 #[track_caller]
750 pub fn expect_non_local<OtherId>(self) -> Res<OtherId> {
923072b8
FG
751 self.map_id(
752 #[track_caller]
753 |_| panic!("unexpected `Res::Local`"),
754 )
c295e0f8
XL
755 }
756
416331ca
XL
757 pub fn macro_kind(self) -> Option<MacroKind> {
758 match self {
759 Res::Def(DefKind::Macro(kind), _) => Some(kind),
760 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
761 _ => None,
762 }
763 }
60c5eb7d 764
1b1a35ee
XL
765 /// Returns `None` if this is `Res::Err`
766 pub fn ns(&self) -> Option<Namespace> {
60c5eb7d 767 match self {
1b1a35ee 768 Res::Def(kind, ..) => kind.ns(),
2b03887a
FG
769 Res::PrimTy(..) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::ToolMod => {
770 Some(Namespace::TypeNS)
771 }
1b1a35ee
XL
772 Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
773 Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
774 Res::Err => None,
60c5eb7d
XL
775 }
776 }
1b1a35ee
XL
777
778 /// Always returns `true` if `self` is `Res::Err`
779 pub fn matches_ns(&self, ns: Namespace) -> bool {
780 self.ns().map_or(true, |actual_ns| actual_ns == ns)
781 }
29967ef6
XL
782
783 /// Returns whether such a resolved path can occur in a tuple struct/variant pattern
784 pub fn expected_in_tuple_struct_pat(&self) -> bool {
785 matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) | Res::SelfCtor(..))
786 }
04454e1e
FG
787
788 /// Returns whether such a resolved path can occur in a unit struct/variant pattern
789 pub fn expected_in_unit_struct_pat(&self) -> bool {
790 matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Const), _) | Res::SelfCtor(..))
791 }
1a4d82fc 792}
923072b8
FG
793
794/// Resolution for a lifetime appearing in a type.
064997fb 795#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
923072b8
FG
796pub enum LifetimeRes {
797 /// Successfully linked the lifetime to a generic parameter.
798 Param {
799 /// Id of the generic parameter that introduced it.
800 param: LocalDefId,
801 /// Id of the introducing place. That can be:
802 /// - an item's id, for the item's generic parameters;
803 /// - a TraitRef's ref_id, identifying the `for<...>` binder;
804 /// - a BareFn type's id.
805 ///
806 /// This information is used for impl-trait lifetime captures, to know when to or not to
807 /// capture any given lifetime.
808 binder: NodeId,
809 },
810 /// Created a generic parameter for an anonymous lifetime.
811 Fresh {
812 /// Id of the generic parameter that introduced it.
813 ///
814 /// Creating the associated `LocalDefId` is the responsibility of lowering.
815 param: NodeId,
816 /// Id of the introducing place. See `Param`.
817 binder: NodeId,
e8be2606
FG
818 /// Kind of elided lifetime
819 kind: hir::MissingLifetimeKind,
923072b8
FG
820 },
821 /// This variant is used for anonymous lifetimes that we did not resolve during
9c376795 822 /// late resolution. Those lifetimes will be inferred by typechecking.
064997fb 823 Infer,
923072b8
FG
824 /// Explicit `'static` lifetime.
825 Static,
826 /// Resolution failure.
827 Error,
828 /// HACK: This is used to recover the NodeId of an elided lifetime.
829 ElidedAnchor { start: NodeId, end: NodeId },
830}
9ffffee4 831
4b012472 832pub type DocLinkResMap = UnordMap<(Symbol, Namespace), Option<Res<NodeId>>>;