Skip to main content

rustc_attr_ir/
target.rs

1//! This module lists attribute targets, with conversions from other types.
2
3use std::fmt::{self, Display};
4
5use rustc_ast::visit::AssocCtxt;
6use rustc_ast::{AssocItemKind, ForeignItemKind, ast};
7use rustc_macros::StableHash;
8
9#[derive(#[automatically_derived]
impl ::core::marker::Copy for GenericParamKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for GenericParamKind {
    #[inline]
    fn clone(&self) -> GenericParamKind { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for GenericParamKind {
    #[inline]
    fn eq(&self, other: &GenericParamKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                GenericParamKind::Type => "Type",
                GenericParamKind::Lifetime => "Lifetime",
                GenericParamKind::Const => "Const",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for GenericParamKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            GenericParamKind {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    GenericParamKind::Type => {}
                    GenericParamKind::Lifetime => {}
                    GenericParamKind::Const => {}
                }
            }
        }
    };StableHash)]
10pub enum GenericParamKind {
11    Type,
12    Lifetime,
13    Const,
14}
15
16#[derive(#[automatically_derived]
impl ::core::marker::Copy for MethodKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for MethodKind {
    #[inline]
    fn clone(&self) -> MethodKind {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for MethodKind {
    #[inline]
    fn eq(&self, other: &MethodKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (MethodKind::Trait { body: __self_0 }, MethodKind::Trait {
                    body: __arg1_0 }) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for MethodKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            MethodKind::Trait { body: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Trait",
                    "body", &__self_0),
            MethodKind::TraitImpl =>
                ::core::fmt::Formatter::write_str(f, "TraitImpl"),
            MethodKind::Inherent =>
                ::core::fmt::Formatter::write_str(f, "Inherent"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for MethodKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<bool>;
    }
}Eq, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for MethodKind {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    MethodKind::Trait { body: ref __binding_0 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                    MethodKind::TraitImpl => {}
                    MethodKind::Inherent => {}
                }
            }
        }
    };StableHash)]
17pub enum MethodKind {
18    /// Method in a `trait Trait` block
19    Trait {
20        /// Whether a default is provided for this method
21        body: bool,
22    },
23    /// Method in a `impl Trait for Type` block
24    TraitImpl,
25    /// Method in a `impl Type` block
26    Inherent,
27}
28
29#[derive(#[automatically_derived]
impl ::core::marker::Copy for Target { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Target {
    #[inline]
    fn clone(&self) -> Target {
        let _: ::core::clone::AssertParamIsClone<bool>;
        let _: ::core::clone::AssertParamIsClone<MethodKind>;
        let _: ::core::clone::AssertParamIsClone<GenericParamKind>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Target {
    #[inline]
    fn eq(&self, other: &Target) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Target::Impl { of_trait: __self_0 }, Target::Impl {
                    of_trait: __arg1_0 }) => __self_0 == __arg1_0,
                (Target::Method(__self_0), Target::Method(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Target::GenericParam { kind: __self_0, has_default: __self_1
                    }, Target::GenericParam {
                    kind: __arg1_0, has_default: __arg1_1 }) =>
                    __self_1 == __arg1_1 && __self_0 == __arg1_0,
                (Target::Delegation { mac: __self_0 }, Target::Delegation {
                    mac: __arg1_0 }) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for Target {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Target::ExternCrate =>
                ::core::fmt::Formatter::write_str(f, "ExternCrate"),
            Target::Use => ::core::fmt::Formatter::write_str(f, "Use"),
            Target::Static => ::core::fmt::Formatter::write_str(f, "Static"),
            Target::Const => ::core::fmt::Formatter::write_str(f, "Const"),
            Target::Fn => ::core::fmt::Formatter::write_str(f, "Fn"),
            Target::Closure =>
                ::core::fmt::Formatter::write_str(f, "Closure"),
            Target::Mod => ::core::fmt::Formatter::write_str(f, "Mod"),
            Target::ForeignMod =>
                ::core::fmt::Formatter::write_str(f, "ForeignMod"),
            Target::GlobalAsm =>
                ::core::fmt::Formatter::write_str(f, "GlobalAsm"),
            Target::TyAlias =>
                ::core::fmt::Formatter::write_str(f, "TyAlias"),
            Target::Enum => ::core::fmt::Formatter::write_str(f, "Enum"),
            Target::Variant =>
                ::core::fmt::Formatter::write_str(f, "Variant"),
            Target::Struct => ::core::fmt::Formatter::write_str(f, "Struct"),
            Target::Field => ::core::fmt::Formatter::write_str(f, "Field"),
            Target::Union => ::core::fmt::Formatter::write_str(f, "Union"),
            Target::Trait => ::core::fmt::Formatter::write_str(f, "Trait"),
            Target::TraitAlias =>
                ::core::fmt::Formatter::write_str(f, "TraitAlias"),
            Target::Impl { of_trait: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Impl",
                    "of_trait", &__self_0),
            Target::Expression =>
                ::core::fmt::Formatter::write_str(f, "Expression"),
            Target::Statement =>
                ::core::fmt::Formatter::write_str(f, "Statement"),
            Target::Arm => ::core::fmt::Formatter::write_str(f, "Arm"),
            Target::AssocConst =>
                ::core::fmt::Formatter::write_str(f, "AssocConst"),
            Target::Method(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Method",
                    &__self_0),
            Target::AssocTy =>
                ::core::fmt::Formatter::write_str(f, "AssocTy"),
            Target::ForeignFn =>
                ::core::fmt::Formatter::write_str(f, "ForeignFn"),
            Target::ForeignStatic =>
                ::core::fmt::Formatter::write_str(f, "ForeignStatic"),
            Target::ForeignTy =>
                ::core::fmt::Formatter::write_str(f, "ForeignTy"),
            Target::GenericParam { kind: __self_0, has_default: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "GenericParam", "kind", __self_0, "has_default", &__self_1),
            Target::MacroDef =>
                ::core::fmt::Formatter::write_str(f, "MacroDef"),
            Target::Param => ::core::fmt::Formatter::write_str(f, "Param"),
            Target::PatField =>
                ::core::fmt::Formatter::write_str(f, "PatField"),
            Target::ExprField =>
                ::core::fmt::Formatter::write_str(f, "ExprField"),
            Target::WherePredicate =>
                ::core::fmt::Formatter::write_str(f, "WherePredicate"),
            Target::MacroCall =>
                ::core::fmt::Formatter::write_str(f, "MacroCall"),
            Target::Crate => ::core::fmt::Formatter::write_str(f, "Crate"),
            Target::Delegation { mac: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Delegation", "mac", &__self_0),
            Target::ForLoop =>
                ::core::fmt::Formatter::write_str(f, "ForLoop"),
            Target::While => ::core::fmt::Formatter::write_str(f, "While"),
            Target::Loop => ::core::fmt::Formatter::write_str(f, "Loop"),
            Target::Break => ::core::fmt::Formatter::write_str(f, "Break"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for Target {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<bool>;
        let _: ::core::cmp::AssertParamIsEq<MethodKind>;
        let _: ::core::cmp::AssertParamIsEq<GenericParamKind>;
    }
}Eq, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for Target {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    Target::ExternCrate => {}
                    Target::Use => {}
                    Target::Static => {}
                    Target::Const => {}
                    Target::Fn => {}
                    Target::Closure => {}
                    Target::Mod => {}
                    Target::ForeignMod => {}
                    Target::GlobalAsm => {}
                    Target::TyAlias => {}
                    Target::Enum => {}
                    Target::Variant => {}
                    Target::Struct => {}
                    Target::Field => {}
                    Target::Union => {}
                    Target::Trait => {}
                    Target::TraitAlias => {}
                    Target::Impl { of_trait: ref __binding_0 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                    Target::Expression => {}
                    Target::Statement => {}
                    Target::Arm => {}
                    Target::AssocConst => {}
                    Target::Method(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                    Target::AssocTy => {}
                    Target::ForeignFn => {}
                    Target::ForeignStatic => {}
                    Target::ForeignTy => {}
                    Target::GenericParam {
                        kind: ref __binding_0, has_default: ref __binding_1 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                    Target::MacroDef => {}
                    Target::Param => {}
                    Target::PatField => {}
                    Target::ExprField => {}
                    Target::WherePredicate => {}
                    Target::MacroCall => {}
                    Target::Crate => {}
                    Target::Delegation { mac: ref __binding_0 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                    Target::ForLoop => {}
                    Target::While => {}
                    Target::Loop => {}
                    Target::Break => {}
                }
            }
        }
    };StableHash)]
30pub enum Target {
31    ExternCrate,
32    Use,
33    Static,
34    Const,
35    Fn,
36    Closure,
37    Mod,
38    ForeignMod,
39    GlobalAsm,
40    TyAlias,
41    Enum,
42    Variant,
43    Struct,
44    Field,
45    Union,
46    Trait,
47    TraitAlias,
48    Impl { of_trait: bool },
49    Expression,
50    Statement,
51    Arm,
52    AssocConst,
53    Method(MethodKind),
54    AssocTy,
55    ForeignFn,
56    ForeignStatic,
57    ForeignTy,
58    GenericParam { kind: GenericParamKind, has_default: bool },
59    MacroDef,
60    Param,
61    PatField,
62    ExprField,
63    WherePredicate,
64    MacroCall,
65    Crate,
66    Delegation { mac: bool },
67    ForLoop,
68    While,
69    Loop,
70    Break,
71}
72
73impl Display for Target {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.write_fmt(format_args!("{0}", Self::name(*self)))write!(f, "{}", Self::name(*self))
76    }
77}
78
79impl ::rustc_error_messages::IntoDiagArg for Target {
    fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>)
        -> ::rustc_error_messages::DiagArgValue {
        self.to_string().into_diag_arg(path)
    }
}rustc_error_messages::into_diag_arg_using_display!(Target);
80
81impl Target {
82    pub fn is_associated_item(self) -> bool {
83        match self {
84            Target::AssocConst | Target::AssocTy | Target::Method(_) => true,
85            Target::ExternCrate
86            | Target::Use
87            | Target::Static
88            | Target::Const
89            | Target::Fn
90            | Target::Closure
91            | Target::Mod
92            | Target::ForeignMod
93            | Target::GlobalAsm
94            | Target::TyAlias
95            | Target::Enum
96            | Target::Variant
97            | Target::Struct
98            | Target::Field
99            | Target::Union
100            | Target::Trait
101            | Target::TraitAlias
102            | Target::Impl { .. }
103            | Target::Expression
104            | Target::Statement
105            | Target::Arm
106            | Target::ForeignFn
107            | Target::ForeignStatic
108            | Target::ForeignTy
109            | Target::GenericParam { .. }
110            | Target::MacroDef
111            | Target::Param
112            | Target::PatField
113            | Target::ExprField
114            | Target::MacroCall
115            | Target::Crate
116            | Target::WherePredicate
117            | Target::Delegation { .. }
118            | Target::Loop
119            | Target::While
120            | Target::ForLoop
121            | Target::Break => false,
122        }
123    }
124    pub fn from_ast_item(item: &ast::Item) -> Target {
125        match item.kind {
126            ast::ItemKind::ExternCrate(..) => Target::ExternCrate,
127            ast::ItemKind::Use(..) => Target::Use,
128            ast::ItemKind::Static { .. } => Target::Static,
129            ast::ItemKind::Const(..) => Target::Const,
130            ast::ItemKind::ConstBlock(..) => Target::Const,
131            ast::ItemKind::Fn { .. } => Target::Fn,
132            ast::ItemKind::Mod(..) => Target::Mod,
133            ast::ItemKind::ForeignMod { .. } => Target::ForeignMod,
134            ast::ItemKind::GlobalAsm { .. } => Target::GlobalAsm,
135            ast::ItemKind::TyAlias(..) => Target::TyAlias,
136            ast::ItemKind::Enum(..) => Target::Enum,
137            ast::ItemKind::Struct(..) => Target::Struct,
138            ast::ItemKind::Union(..) => Target::Union,
139            ast::ItemKind::Trait(..) => Target::Trait,
140            ast::ItemKind::TraitAlias(..) => Target::TraitAlias,
141            ast::ItemKind::Impl(ref i) => Target::Impl { of_trait: i.of_trait.is_some() },
142            ast::ItemKind::MacCall(..) => Target::MacroCall,
143            ast::ItemKind::MacroDef(..) => Target::MacroDef,
144            ast::ItemKind::Delegation(..) => Target::Delegation { mac: false },
145            ast::ItemKind::DelegationMac(..) => Target::Delegation { mac: true },
146        }
147    }
148
149    pub fn from_foreign_item_kind(kind: &ast::ForeignItemKind) -> Target {
150        match kind {
151            ForeignItemKind::Static(_) => Target::ForeignStatic,
152            ForeignItemKind::Fn(_) => Target::ForeignFn,
153            ForeignItemKind::TyAlias(_) => Target::ForeignTy,
154            ForeignItemKind::MacCall(_) => Target::MacroCall,
155        }
156    }
157
158    pub fn from_assoc_item_kind(kind: &ast::AssocItemKind, assoc_ctxt: AssocCtxt) -> Target {
159        match kind {
160            AssocItemKind::Const(_) => Target::AssocConst,
161            AssocItemKind::Fn(f) => Target::Method(match assoc_ctxt {
162                AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() },
163                AssocCtxt::Impl { of_trait, .. } => {
164                    if of_trait {
165                        MethodKind::TraitImpl
166                    } else {
167                        MethodKind::Inherent
168                    }
169                }
170            }),
171            AssocItemKind::Type(_) => Target::AssocTy,
172            AssocItemKind::Delegation(_) => Target::Delegation { mac: false },
173            AssocItemKind::DelegationMac(_) => Target::Delegation { mac: true },
174            AssocItemKind::MacCall(_) => Target::MacroCall,
175        }
176    }
177
178    pub fn from_expr(expr: &ast::Expr) -> Self {
179        match &expr.kind {
180            ast::ExprKind::Closure(..) | ast::ExprKind::Gen(..) => Self::Closure,
181            ast::ExprKind::Paren(e) => Self::from_expr(&e),
182            ast::ExprKind::ForLoop { .. } => Self::ForLoop,
183            ast::ExprKind::Loop(..) => Self::Loop,
184            ast::ExprKind::While(..) => Self::While,
185            ast::ExprKind::Break(..) => Self::Break,
186            _ => Self::Expression,
187        }
188    }
189
190    pub fn name(self) -> &'static str {
191        match self {
192            Target::ExternCrate => "extern crate",
193            Target::Use => "use",
194            Target::Static => "static",
195            Target::Const => "constant",
196            Target::Fn => "function",
197            Target::Closure => "closure",
198            Target::Mod => "module",
199            Target::ForeignMod => "foreign module",
200            Target::GlobalAsm => "global asm",
201            Target::TyAlias => "type alias",
202            Target::Enum => "enum",
203            Target::Variant => "enum variant",
204            Target::Struct => "struct",
205            Target::Field => "struct field",
206            Target::Union => "union",
207            Target::Trait => "trait",
208            Target::TraitAlias => "trait alias",
209            Target::Impl { .. } => "implementation block",
210            Target::Expression => "expression",
211            Target::Statement => "statement",
212            Target::Arm => "match arm",
213            Target::AssocConst => "associated const",
214            Target::Method(kind) => match kind {
215                MethodKind::Inherent => "inherent method",
216                MethodKind::Trait { body: false } => "required trait method",
217                MethodKind::Trait { body: true } => "provided trait method",
218                MethodKind::TraitImpl => "trait method in an impl block",
219            },
220            Target::AssocTy => "associated type",
221            Target::ForeignFn => "foreign function",
222            Target::ForeignStatic => "foreign static item",
223            Target::ForeignTy => "foreign type",
224            Target::GenericParam { kind, .. } => match kind {
225                GenericParamKind::Type => "type parameter",
226                GenericParamKind::Lifetime => "lifetime parameter",
227                GenericParamKind::Const => "const parameter",
228            },
229            Target::MacroDef => "macro def",
230            Target::Param => "function param",
231            Target::PatField => "pattern field",
232            Target::ExprField => "struct field",
233            Target::WherePredicate => "where predicate",
234            Target::MacroCall => "macro call",
235            Target::Crate => "crate",
236            Target::Delegation { .. } => "delegation",
237            Target::Loop => "loop",
238            Target::ForLoop => "for loop",
239            Target::While => "while loop",
240            Target::Break => "break expression",
241        }
242    }
243
244    pub fn plural_name(self) -> &'static str {
245        match self {
246            Target::ExternCrate => "extern crates",
247            Target::Use => "use statements",
248            Target::Static => "statics",
249            Target::Const => "constants",
250            Target::Fn => "functions",
251            Target::Closure => "closures",
252            Target::Mod => "modules",
253            Target::ForeignMod => "foreign modules",
254            Target::GlobalAsm => "global asms",
255            Target::TyAlias => "type aliases",
256            Target::Enum => "enums",
257            Target::Variant => "enum variants",
258            Target::Struct => "structs",
259            Target::Field => "struct fields",
260            Target::Union => "unions",
261            Target::Trait => "traits",
262            Target::TraitAlias => "trait aliases",
263            Target::Impl { of_trait: false } => "inherent impl blocks",
264            Target::Impl { of_trait: true } => "trait impl blocks",
265            Target::Expression => "expressions",
266            Target::Statement => "statements",
267            Target::Arm => "match arms",
268            Target::AssocConst => "associated consts",
269            Target::Method(kind) => match kind {
270                MethodKind::Inherent => "inherent methods",
271                MethodKind::Trait { body: false } => "required trait methods",
272                MethodKind::Trait { body: true } => "provided trait methods",
273                MethodKind::TraitImpl => "trait methods in impl blocks",
274            },
275            Target::AssocTy => "associated types",
276            Target::ForeignFn => "foreign functions",
277            Target::ForeignStatic => "foreign statics",
278            Target::ForeignTy => "foreign types",
279            Target::GenericParam { kind, has_default: _ } => match kind {
280                GenericParamKind::Type => "type parameters",
281                GenericParamKind::Lifetime => "lifetime parameters",
282                GenericParamKind::Const => "const parameters",
283            },
284            Target::MacroDef => "macro defs",
285            Target::Param => "function params",
286            Target::PatField => "pattern fields",
287            Target::ExprField => "struct fields",
288            Target::WherePredicate => "where predicates",
289            Target::MacroCall => "macro calls",
290            Target::Crate => "crates",
291            Target::Delegation { .. } => "delegations",
292            Target::ForLoop => "for loops",
293            Target::Loop => "loops",
294            Target::While => "while loops",
295            Target::Break => "break expressions",
296        }
297    }
298}