I am extracting some source code information for code mutation and generation based on rustc's library. I'm using the rustc_interface::run_compiler
closure to parse the MIR and get the type information I need, how can I store it and use outside the closure?
It looks like rustc_interface::run_compiler
is multithreaded, so how do I change it to single threaded? Or is there a better way to save Ty<'_>
and use it in other place?
Below is my code:
let tys = rustc_interface::run_compiler(config, |compiler| {
let tys = compiler.enter(|queries| {
let mut tys = Vec::new();
queries.global_ctxt().unwrap().enter(|tcx| {
let hir_krate = tcx.hir();
for id in hir_krate.items() {
let item = id.owner_id.def_id;
match tcx.def_kind(item) {
def::DefKind::Fn => {
let mir: &rustc_middle::mir::Body<'_> = tcx.optimized_mir(item);
let return_ty = mir.local_decls[rustc_middle::mir::Local::from_usize(0)].ty;
tys.push(return_ty);
println!("{:?}", return_ty);
},
_ => {}
}
}
});
tys
});
tys
});
This is the error information:
error[E0277]: Span
cannot be shared between threads safely
--> src/main.rs:49:15
| 49 | let tys = rustc_interface::run_compiler(config, |compiler| {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Span
cannot be shared between threads
safely
|
= help: within BoundVariableKind
, the trait std::marker::Sync
is not implemented for Span
= note: required because it appears within the type Option<Span>
= note: required because it appears within the type BoundRegionKind
= note: required because it appears within the type BoundVariableKind
= note: required for List<BoundVariableKind>
to implement std::marker::Sync
= note: required because it appears within the type &List<BoundVariableKind>
= note: required because it appears within the type Binder<'_, FnSig<'_>>
= note: required because it appears within the type TyKind<TyCtxt<'_>>
= note: required because it appears within the type
WithCachedTypeInfo<TyKind<TyCtxt<'_>>>
= note: required for &WithCachedTypeInfo<rustc_type_ir::TyKind<TyCtxt<'_>>>
to implement Send
= note: required because it appears within the type Interned<'_, WithCachedTypeInfo<TyKind<TyCtxt<'_>>>>
= note: required because it appears within the type Ty<'_>
= note: required for std::ptr::Unique<rustc_middle::ty::Ty<'_>>
to implement Send
= note: required because it appears within the type RawVec<Ty<'_>>
= note: required because it appears within the type Vec<Ty<'_>>
note: required by a bound in run_compiler