Add more SMIR internal impl and callback return value
In cases like Kani, we will invoke the rustc_internal run command directly for now. It would be handly to be able to have a callback that can return a value. We also need extra methods to convert stable constructs into internal ones, so we can break down the transition into finer grain commits.
This commit is contained in:
parent
0ea7ddcc35
commit
e70839ac84
3 changed files with 159 additions and 21 deletions
|
@ -47,7 +47,7 @@ pub type Symbol = String;
|
|||
pub type CrateNum = usize;
|
||||
|
||||
/// A unique identification number for each item accessible for the current compilation unit.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct DefId(usize);
|
||||
|
||||
impl Debug for DefId {
|
||||
|
@ -240,12 +240,16 @@ pub trait Context {
|
|||
// datastructures and stable MIR datastructures
|
||||
scoped_thread_local! (static TLV: Cell<*const ()>);
|
||||
|
||||
pub fn run(context: &dyn Context, f: impl FnOnce()) {
|
||||
assert!(!TLV.is_set());
|
||||
let ptr: *const () = &context as *const &_ as _;
|
||||
TLV.set(&Cell::new(ptr), || {
|
||||
f();
|
||||
});
|
||||
pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
if TLV.is_set() {
|
||||
Err(Error::from("StableMIR already running"))
|
||||
} else {
|
||||
let ptr: *const () = &context as *const &_ as _;
|
||||
TLV.set(&Cell::new(ptr), || Ok(f()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads the current context and calls a function with it.
|
||||
|
@ -260,7 +264,7 @@ pub fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
|
|||
}
|
||||
|
||||
/// A type that provides internal information but that can still be used for debug purpose.
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Opaque(String);
|
||||
|
||||
impl std::fmt::Display for Opaque {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue