rustc_attr_data_structures/
attributes.rs

1use rustc_abi::Align;
2use rustc_ast as ast;
3use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4use rustc_span::{Span, Symbol};
5
6use crate::RustcVersion;
7
8#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
9pub enum InlineAttr {
10    None,
11    Hint,
12    Always,
13    Never,
14    /// `#[rustc_force_inline]` forces inlining to happen in the MIR inliner - it reports an error
15    /// if the inlining cannot happen. It is limited to only free functions so that the calls
16    /// can always be resolved.
17    Force {
18        attr_span: Span,
19        reason: Option<Symbol>,
20    },
21}
22
23impl InlineAttr {
24    pub fn always(&self) -> bool {
25        match self {
26            InlineAttr::Always | InlineAttr::Force { .. } => true,
27            InlineAttr::None | InlineAttr::Hint | InlineAttr::Never => false,
28        }
29    }
30}
31
32#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic)]
33pub enum InstructionSetAttr {
34    ArmA32,
35    ArmT32,
36}
37
38#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic, Default)]
39pub enum OptimizeAttr {
40    /// No `#[optimize(..)]` attribute
41    #[default]
42    Default,
43    /// `#[optimize(none)]`
44    DoNotOptimize,
45    /// `#[optimize(speed)]`
46    Speed,
47    /// `#[optimize(size)]`
48    Size,
49}
50
51impl OptimizeAttr {
52    pub fn do_not_optimize(&self) -> bool {
53        matches!(self, Self::DoNotOptimize)
54    }
55}
56
57#[derive(Clone, Debug, Encodable, Decodable)]
58pub enum DiagnosticAttribute {
59    // tidy-alphabetical-start
60    DoNotRecommend,
61    OnUnimplemented,
62    // tidy-alphabetical-end
63}
64
65#[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone)]
66pub enum ReprAttr {
67    ReprInt(IntType),
68    ReprRust,
69    ReprC,
70    ReprPacked(Align),
71    ReprSimd,
72    ReprTransparent,
73    ReprAlign(Align),
74}
75pub use ReprAttr::*;
76
77pub enum TransparencyError {
78    UnknownTransparency(Symbol, Span),
79    MultipleTransparencyAttrs(Span, Span),
80}
81
82#[derive(Eq, PartialEq, Debug, Copy, Clone)]
83#[derive(Encodable, Decodable)]
84pub enum IntType {
85    SignedInt(ast::IntTy),
86    UnsignedInt(ast::UintTy),
87}
88
89#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
90pub struct Deprecation {
91    pub since: DeprecatedSince,
92    /// The note to issue a reason.
93    pub note: Option<Symbol>,
94    /// A text snippet used to completely replace any use of the deprecated item in an expression.
95    ///
96    /// This is currently unstable.
97    pub suggestion: Option<Symbol>,
98}
99
100/// Release in which an API is deprecated.
101#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
102pub enum DeprecatedSince {
103    RustcVersion(RustcVersion),
104    /// Deprecated in the future ("to be determined").
105    Future,
106    /// `feature(staged_api)` is off. Deprecation versions outside the standard
107    /// library are allowed to be arbitrary strings, for better or worse.
108    NonStandard(Symbol),
109    /// Deprecation version is unspecified but optional.
110    Unspecified,
111    /// Failed to parse a deprecation version, or the deprecation version is
112    /// unspecified and required. An error has already been emitted.
113    Err,
114}
115
116impl Deprecation {
117    /// Whether an item marked with #[deprecated(since = "X")] is currently
118    /// deprecated (i.e., whether X is not greater than the current rustc
119    /// version).
120    pub fn is_in_effect(&self) -> bool {
121        match self.since {
122            DeprecatedSince::RustcVersion(since) => since <= RustcVersion::CURRENT,
123            DeprecatedSince::Future => false,
124            // The `since` field doesn't have semantic purpose without `#![staged_api]`.
125            DeprecatedSince::NonStandard(_) => true,
126            // Assume deprecation is in effect if "since" field is absent or invalid.
127            DeprecatedSince::Unspecified | DeprecatedSince::Err => true,
128        }
129    }
130
131    pub fn is_since_rustc_version(&self) -> bool {
132        matches!(self.since, DeprecatedSince::RustcVersion(_))
133    }
134}