Skip to main content

rustc_data_structures/
lib.rs

1//! Various data structures used by the Rust compiler. The intention
2//! is that code in here should not be *specific* to rustc, so that
3//! it can be easily unit tested and so forth.
4//!
5//! # Note
6//!
7//! This API is completely unstable and subject to change.
8
9// tidy-alphabetical-start
10#![allow(internal_features)]
11#![allow(rustc::default_hash_types)]
12#![allow(rustc::potential_query_instability)]
13#![cfg_attr(bootstrap, feature(assert_matches))]
14#![cfg_attr(bootstrap, feature(cfg_select))]
15#![cfg_attr(bootstrap, feature(cold_path))]
16#![cfg_attr(test, feature(test))]
17#![deny(unsafe_op_in_unsafe_fn)]
18#![feature(allocator_api)]
19#![feature(ascii_char)]
20#![feature(ascii_char_variants)]
21#![feature(auto_traits)]
22#![feature(const_default)]
23#![feature(const_trait_impl)]
24#![feature(dropck_eyepatch)]
25#![feature(extend_one)]
26#![feature(file_buffered)]
27#![feature(map_try_insert)]
28#![feature(min_specialization)]
29#![feature(negative_impls)]
30#![feature(never_type)]
31#![feature(ptr_alignment_type)]
32#![feature(rustc_attrs)]
33#![feature(sized_hierarchy)]
34#![feature(thread_id_value)]
35#![feature(trusted_len)]
36#![feature(type_alias_impl_trait)]
37#![feature(unwrap_infallible)]
38// tidy-alphabetical-end
39
40// Temporarily re-export `assert_matches!`, so that the rest of the compiler doesn't
41// have to worry about it being moved to a different module in std during stabilization.
42// FIXME(#151359): Remove this when `feature(assert_matches)` is stable in stage0.
43// (This doesn't necessarily need to be fixed during the beta bump itself.)
44#[cfg(bootstrap)]
45pub use std::assert_matches::{assert_matches, debug_assert_matches};
46use std::fmt;
47#[cfg(not(bootstrap))]
48pub use std::{assert_matches, debug_assert_matches};
49
50// This allows derive macros to reference this crate
51extern crate self as rustc_data_structures;
52
53pub use atomic_ref::AtomicRef;
54pub use ena::{snapshot_vec, undo_log, unify};
55// Re-export `hashbrown::hash_table`, because it's part of our API
56// (via `ShardedHashMap`), and because it lets other compiler crates use the
57// lower-level `HashTable` API without a tricky `hashbrown` dependency.
58pub use hashbrown::hash_table;
59pub use rustc_index::static_assert_size;
60// Re-export some data-structure crates which are part of our public API.
61pub use {either, indexmap, smallvec, thin_vec};
62
63pub mod aligned;
64pub mod base_n;
65pub mod binary_search_util;
66pub mod fingerprint;
67pub mod flat_map_in_place;
68pub mod flock;
69pub mod frozen;
70pub mod fx;
71pub mod graph;
72pub mod intern;
73pub mod jobserver;
74pub mod marker;
75pub mod memmap;
76pub mod obligation_forest;
77pub mod owned_slice;
78pub mod packed;
79pub mod profiling;
80pub mod sharded;
81pub mod small_c_str;
82pub mod snapshot_map;
83pub mod sorted_map;
84pub mod sso;
85pub mod stable_hasher;
86pub mod stack;
87pub mod steal;
88pub mod svh;
89pub mod sync;
90pub mod tagged_ptr;
91pub mod temp_dir;
92pub mod thinvec;
93pub mod thousands;
94pub mod transitive_relation;
95pub mod unhash;
96pub mod union_find;
97pub mod unord;
98pub mod vec_cache;
99pub mod work_queue;
100
101mod atomic_ref;
102
103/// This calls the passed function while ensuring it won't be inlined into the caller.
104#[inline(never)]
105#[cold]
106pub fn outline<F: FnOnce() -> R, R>(f: F) -> R {
107    f()
108}
109
110/// Returns a structure that calls `f` when dropped.
111pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
112    OnDrop(Some(f))
113}
114
115pub struct OnDrop<F: FnOnce()>(Option<F>);
116
117impl<F: FnOnce()> OnDrop<F> {
118    /// Disables on-drop call.
119    #[inline]
120    pub fn disable(mut self) {
121        self.0.take();
122    }
123}
124
125impl<F: FnOnce()> Drop for OnDrop<F> {
126    #[inline]
127    fn drop(&mut self) {
128        if let Some(f) = self.0.take() {
129            f();
130        }
131    }
132}
133
134/// This is a marker for a fatal compiler error used with `resume_unwind`.
135pub struct FatalErrorMarker;
136
137/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
138pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
139    struct Printer<F> {
140        f: F,
141    }
142    impl<F> fmt::Display for Printer<F>
143    where
144        F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
145    {
146        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
147            (self.f)(fmt)
148        }
149    }
150
151    Printer { f }
152}
153
154// See comment in compiler/rustc_middle/src/tests.rs and issue #27438.
155#[doc(hidden)]
156pub fn __noop_fix_for_windows_dllimport_issue() {}
157
158#[macro_export]
159macro_rules! external_bitflags_debug {
160    ($Name:ident) => {
161        impl ::std::fmt::Debug for $Name {
162            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
163                ::bitflags::parser::to_writer(self, f)
164            }
165        }
166    };
167}