rustc_public/rustc_internal/
mod.rs

1//! Module that implements the bridge between rustc_public's IR and internal compiler MIR.
2//!
3//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
4//! until rustc_public's IR is complete.
5
6use std::cell::RefCell;
7
8use rustc_middle::ty::TyCtxt;
9use rustc_public_bridge::Tables;
10use rustc_public_bridge::context::CompilerCtxt;
11use rustc_span::def_id::CrateNum;
12
13use crate::Error;
14use crate::compiler_interface::{BridgeTys, CompilerInterface, with};
15use crate::unstable::{RustcInternal, Stable};
16
17pub mod pretty;
18
19/// Convert an internal Rust compiler item into its stable counterpart, if one exists.
20///
21/// # Warning
22///
23/// This function is unstable, and its behavior may change at any point.
24/// E.g.: Items that were previously supported, may no longer be supported, or its translation may
25/// change.
26///
27/// # Panics
28///
29/// This function will panic if rustc_public has not been properly initialized.
30pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
31    with_bridge(|tables, cx| item.stable(tables, cx))
32}
33
34/// Convert a stable item into its internal Rust compiler counterpart, if one exists.
35///
36/// # Warning
37///
38/// This function is unstable, and it's behavior may change at any point.
39/// Not every stable item can be converted to an internal one.
40/// Furthermore, items that were previously supported, may no longer be supported in newer versions.
41///
42/// # Panics
43///
44/// This function will panic if rustc_public has not been properly initialized.
45pub fn internal<'tcx, S>(tcx: TyCtxt<'tcx>, item: S) -> S::T<'tcx>
46where
47    S: RustcInternal,
48{
49    // The tcx argument ensures that the item won't outlive the type context.
50    // See https://github.com/rust-lang/rust/pull/120128/commits/9aace6723572438a94378451793ca37deb768e72
51    // for more details.
52    with_bridge(|tables, _| item.internal(tables, tcx))
53}
54
55pub fn crate_num(item: &crate::Crate) -> CrateNum {
56    item.id.into()
57}
58
59/// Loads the current context and calls a function with it.
60/// Do not nest these, as that will ICE.
61pub(crate) fn with_bridge<R>(
62    f: impl for<'tcx> FnOnce(&mut Tables<'tcx, BridgeTys>, &CompilerCtxt<'tcx, BridgeTys>) -> R,
63) -> R {
64    with(|compiler| {
65        let mut tables = compiler.tables.borrow_mut();
66        let cx = compiler.cx.borrow();
67        f(&mut *tables, &*cx)
68    })
69}
70
71pub fn run<F, T>(tcx: TyCtxt<'_>, f: F) -> Result<T, Error>
72where
73    F: FnOnce() -> T,
74{
75    let compiler_cx = RefCell::new(CompilerCtxt::new(tcx));
76    let compiler_interface =
77        CompilerInterface { tables: RefCell::new(Tables::default()), cx: compiler_cx };
78
79    crate::compiler_interface::run(&compiler_interface, || f())
80}
81
82/// Instantiate and run the compiler with the provided arguments and callback.
83///
84/// The callback will be invoked after the compiler ran all its analyses, but before code generation.
85/// Note that this macro accepts two different formats for the callback:
86/// 1. An ident that resolves to a function that accepts no argument and returns `ControlFlow<B, C>`
87/// ```ignore(needs-extern-crate)
88/// # extern crate rustc_driver;
89/// # extern crate rustc_interface;
90/// # extern crate rustc_middle;
91/// # #[macro_use]
92/// # extern crate rustc_public;
93/// #
94/// # fn main() {
95/// #   use std::ops::ControlFlow;
96/// #   use rustc_public::CompilerError;
97///     fn analyze_code() -> ControlFlow<(), ()> {
98///         // Your code goes in here.
99/// #       ControlFlow::Continue(())
100///     }
101/// #   let args = &["--verbose".to_string()];
102///     let result = run!(args, analyze_code);
103/// #   assert_eq!(result, Err(CompilerError::Skipped))
104/// # }
105/// ```
106/// 2. A closure expression:
107/// ```ignore(needs-extern-crate)
108/// # extern crate rustc_driver;
109/// # extern crate rustc_interface;
110/// # extern crate rustc_middle;
111/// # #[macro_use]
112/// # extern crate rustc_public;
113/// #
114/// # fn main() {
115/// #   use std::ops::ControlFlow;
116/// #   use rustc_public::CompilerError;
117///     fn analyze_code(extra_args: Vec<String>) -> ControlFlow<(), ()> {
118/// #       let _ = extra_args;
119///         // Your code goes in here.
120/// #       ControlFlow::Continue(())
121///     }
122/// #   let args = &["--verbose".to_string()];
123/// #   let extra_args = vec![];
124///     let result = run!(args, || analyze_code(extra_args));
125/// #   assert_eq!(result, Err(CompilerError::Skipped))
126/// # }
127/// ```
128#[macro_export]
129macro_rules! run {
130    ($args:expr, $callback_fn:ident) => {
131        $crate::run_driver!($args, || $callback_fn())
132    };
133    ($args:expr, $callback:expr) => {
134        $crate::run_driver!($args, $callback)
135    };
136}
137
138/// Instantiate and run the compiler with the provided arguments and callback.
139///
140/// This is similar to `run` but it invokes the callback with the compiler's `TyCtxt`,
141/// which can be used to invoke internal APIs.
142#[macro_export]
143macro_rules! run_with_tcx {
144    ($args:expr, $callback_fn:ident) => {
145        $crate::run_driver!($args, |tcx| $callback_fn(tcx), with_tcx)
146    };
147    ($args:expr, $callback:expr) => {
148        $crate::run_driver!($args, $callback, with_tcx)
149    };
150}
151
152/// Optionally include an ident. This is needed due to macro hygiene.
153#[macro_export]
154#[doc(hidden)]
155macro_rules! optional {
156    (with_tcx $ident:ident) => {
157        $ident
158    };
159}
160
161/// Prefer using [run!] and [run_with_tcx] instead.
162///
163/// This macro implements the instantiation of a rustc_public driver, and it will invoke
164/// the given callback after the compiler analyses.
165///
166/// The third argument determines whether the callback requires `tcx` as an argument.
167#[macro_export]
168#[doc(hidden)]
169macro_rules! run_driver {
170    ($args:expr, $callback:expr $(, $with_tcx:ident)?) => {{
171        use rustc_driver::{Callbacks, Compilation, run_compiler};
172        use rustc_middle::ty::TyCtxt;
173        use rustc_interface::interface;
174        use rustc_public::rustc_internal;
175        use rustc_public::CompilerError;
176        use std::ops::ControlFlow;
177
178        pub struct RustcPublic<B = (), C = (), F = fn($($crate::optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C>>
179        where
180            B: Send,
181            C: Send,
182            F: FnOnce($($crate::optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
183        {
184            callback: Option<F>,
185            result: Option<ControlFlow<B, C>>,
186        }
187
188        impl<B, C, F> RustcPublic<B, C, F>
189        where
190            B: Send,
191            C: Send,
192            F: FnOnce($($crate::optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
193        {
194            /// Creates a new `RustcPublic` instance, with given test_function and arguments.
195            pub fn new(callback: F) -> Self {
196                RustcPublic { callback: Some(callback), result: None }
197            }
198
199            /// Runs the compiler against given target and tests it with `test_function`
200            pub fn run(&mut self, args: &[String]) -> Result<C, CompilerError<B>> {
201                let compiler_result = rustc_driver::catch_fatal_errors(|| -> interface::Result::<()> {
202                    run_compiler(&args, self);
203                    Ok(())
204                });
205                match (compiler_result, self.result.take()) {
206                    (Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value),
207                    (Ok(Ok(())), Some(ControlFlow::Break(value))) => {
208                        Err(CompilerError::Interrupted(value))
209                    }
210                    (Ok(Ok(_)), None) => Err(CompilerError::Skipped),
211                    // Two cases here:
212                    // - `run` finished normally and returned `Err`
213                    // - `run` panicked with `FatalErr`
214                    // You might think that normal compile errors cause the former, and
215                    // ICEs cause the latter. But some normal compiler errors also cause
216                    // the latter. So we can't meaningfully distinguish them, and group
217                    // them together.
218                    (Ok(Err(_)), _) | (Err(_), _) => Err(CompilerError::Failed),
219                }
220            }
221        }
222
223        impl<B, C, F> Callbacks for RustcPublic<B, C, F>
224        where
225            B: Send,
226            C: Send,
227            F: FnOnce($($crate::optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
228        {
229            /// Called after analysis. Return value instructs the compiler whether to
230            /// continue the compilation afterwards (defaults to `Compilation::Continue`)
231            fn after_analysis<'tcx>(
232                &mut self,
233                _compiler: &interface::Compiler,
234                tcx: TyCtxt<'tcx>,
235            ) -> Compilation {
236                if let Some(callback) = self.callback.take() {
237                    rustc_internal::run(tcx, || {
238                        self.result = Some(callback($($crate::optional!($with_tcx tcx))?));
239                    })
240                    .unwrap();
241                    if self.result.as_ref().is_some_and(|val| val.is_continue()) {
242                        Compilation::Continue
243                    } else {
244                        Compilation::Stop
245                    }
246                } else {
247                    Compilation::Continue
248                }
249            }
250        }
251
252        RustcPublic::new($callback).run($args)
253    }};
254}