Skip to main content

rustc_utils/
lib.rs

1//! `rustc_utils` provides a wide variety of utilities for working with the Rust compiler.
2//! We developed these functions in the course of building various research projects with
3//! rustc.
4//!
5//! Most of the functionality is organized into extension traits implemented for types
6//! in the compiler, such as one for MIR control-flow graphs ([`BodyExt`]) or one for
7//! text ranges ([`SpanExt`]).
8//!
9//! This crate is pinned to a specific nightly version of the Rust compiler.
10//! See the [`rustc_plugin` README](https://github.com/cognitive-engineering-lab/rustc_plugin)
11//! for details on how to add `rustc_utils` as a dependency.
12
13#![feature(
14  rustc_private,
15  min_specialization,    // for rustc_index::newtype_index 
16  box_patterns,          // for ergonomics
17)]
18#![warn(clippy::pedantic)]
19#![allow(
20  clippy::len_zero,
21  clippy::len_without_is_empty,
22  clippy::must_use_candidate,
23  clippy::return_self_not_must_use,
24  clippy::missing_panics_doc,
25  clippy::missing_errors_doc,
26  clippy::doc_markdown,
27  clippy::single_match_else,
28  clippy::if_not_else,
29  clippy::map_unwrap_or,
30  clippy::match_wildcard_for_single_variants,
31  clippy::items_after_statements,
32  clippy::implicit_hasher,
33  clippy::wildcard_imports
34)]
35
36extern crate either;
37extern crate rustc_abi;
38extern crate rustc_borrowck;
39extern crate rustc_data_structures;
40extern crate rustc_driver;
41extern crate rustc_errors;
42extern crate rustc_graphviz;
43extern crate rustc_hir;
44extern crate rustc_index;
45extern crate rustc_infer;
46extern crate rustc_interface;
47extern crate rustc_lint_defs;
48extern crate rustc_macros;
49extern crate rustc_middle;
50extern crate rustc_mir_dataflow;
51extern crate rustc_mir_transform;
52extern crate rustc_serialize;
53extern crate rustc_session;
54extern crate rustc_span;
55extern crate rustc_target;
56extern crate rustc_trait_selection;
57extern crate rustc_type_ir;
58extern crate smallvec;
59
60pub mod cache;
61pub mod hir;
62pub mod mir;
63pub mod source_map;
64#[cfg(feature = "test")]
65pub mod test_utils;
66pub mod timer;
67
68pub use crate::{
69  hir::ty::TyExt,
70  mir::{
71    adt_def::AdtDefExt, body::BodyExt, mutability::MutabilityExt, operand::OperandExt,
72    place::PlaceExt,
73  },
74  source_map::span::{SpanDataExt, SpanExt},
75};
76
77/// Utility for hashset literals. Same as `maplit::hashset` but works with `FxHasher`.
78#[macro_export]
79macro_rules! hashset {
80  (@single $($x:tt)*) => (());
81  (@count $($rest:expr),*) => (<[()]>::len(&[$(hashset!(@single $rest)),*]));
82
83  ($($key:expr,)+) => { hashset!($($key),+) };
84  ($($key:expr),*) => {
85      {
86          let _cap = hashset!(@count $($key),*);
87          let mut _set = ::rustc_data_structures::fx::FxHashSet::default();
88          let _ = _set.try_reserve(_cap);
89          $(
90              let _ = _set.insert($key);
91          )*
92          _set
93      }
94  };
95}