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