rustc_ast_ir/
lib.rs

1//! Common utilities shared by both `rustc_ast` and `rustc_type_ir`.
2//!
3//! Don't depend on this crate directly; both of those crates should re-export
4//! the functionality. Additionally, if you're in scope of `rustc_middle`, then
5//! prefer imports via that too, to avoid needing to directly depend on (e.g.)
6//! `rustc_type_ir` for a single import.
7
8// tidy-alphabetical-start
9#![cfg_attr(feature = "nightly", allow(internal_features))]
10#![cfg_attr(feature = "nightly", feature(never_type))]
11#![cfg_attr(feature = "nightly", feature(rustc_attrs))]
12// tidy-alphabetical-end
13
14#[cfg(feature = "nightly")]
15use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
16
17pub mod visit;
18
19/// The movability of a coroutine / closure literal:
20/// whether a coroutine contains self-references, causing it to be `!Unpin`.
21#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
22#[cfg_attr(
23    feature = "nightly",
24    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
25)]
26pub enum Movability {
27    /// May contain self-references, `!Unpin`.
28    Static,
29    /// Must not contain self-references, `Unpin`.
30    Movable,
31}
32
33#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
34#[cfg_attr(
35    feature = "nightly",
36    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
37)]
38pub enum Mutability {
39    // N.B. Order is deliberate, so that Not < Mut
40    Not,
41    Mut,
42}
43
44impl Mutability {
45    pub fn invert(self) -> Self {
46        match self {
47            Mutability::Mut => Mutability::Not,
48            Mutability::Not => Mutability::Mut,
49        }
50    }
51
52    /// Returns `""` (empty string) or `"mut "` depending on the mutability.
53    pub fn prefix_str(self) -> &'static str {
54        match self {
55            Mutability::Mut => "mut ",
56            Mutability::Not => "",
57        }
58    }
59
60    /// Returns `"&"` or `"&mut "` depending on the mutability.
61    pub fn ref_prefix_str(self) -> &'static str {
62        match self {
63            Mutability::Not => "&",
64            Mutability::Mut => "&mut ",
65        }
66    }
67
68    /// Returns `"const"` or `"mut"` depending on the mutability.
69    pub fn ptr_str(self) -> &'static str {
70        match self {
71            Mutability::Not => "const",
72            Mutability::Mut => "mut",
73        }
74    }
75
76    /// Returns `""` (empty string) or `"mutably "` depending on the mutability.
77    pub fn mutably_str(self) -> &'static str {
78        match self {
79            Mutability::Not => "",
80            Mutability::Mut => "mutably ",
81        }
82    }
83
84    /// Return `true` if self is mutable
85    pub fn is_mut(self) -> bool {
86        matches!(self, Self::Mut)
87    }
88
89    /// Return `true` if self is **not** mutable
90    pub fn is_not(self) -> bool {
91        matches!(self, Self::Not)
92    }
93}
94
95#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
96#[cfg_attr(
97    feature = "nightly",
98    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
99)]
100pub enum Pinnedness {
101    Not,
102    Pinned,
103}