New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Printing AST in custom callback crashes compiler for stolen value #123163
Comments
Expansion steals the result of the parse query. You either need to access it in the |
Oh alright thanks! In case anyone else had the same question as me, I managed to print out the AST with this code: #![feature(rustc_private)]
extern crate rustc_ast;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate rustc_session;
//use rustc_ast::Crate;
use std::borrow::BorrowMut;
use rustc_data_structures::profiling::get_resident_set_size;
use rustc_driver::{args, catch_with_exit_code, install_ice_hook, DEFAULT_BUG_REPORT_URL};
use rustc_driver::{init_rustc_env_logger, RunCompiler};
use rustc_driver::{Callbacks, Compilation};
use rustc_session::config::ErrorOutputType;
use rustc_session::EarlyDiagCtxt;
use std::process;
use std::time::Instant;
pub struct AddCallbacks {}
impl Callbacks for AddCallbacks {
fn after_expansion<'tcx>(
&mut self,
_compiler: &rustc_interface::interface::Compiler,
queries: &'tcx rustc_interface::Queries<'tcx>,
) -> Compilation {
queries
.global_ctxt()
.expect("Global context stolen")
.borrow_mut()
.enter(|tcx| {
let krate = &tcx.resolver_for_lowering().borrow().1.clone().to_owned();
println!("Crate found!\n{krate:#?}");
for item in &krate.items {
println!("{item:#?}");
if let rustc_ast::ItemKind::Fn(body_id) = &item.kind {
println!("Found function!");
println!("{body_id:#?}");
}
}
});
Compilation::Continue
}
}
// Main code got from rustc_driver_impl/src/lib.rs/fn main()
// And modified to add custom callbacks
fn main() {
let _start_time = Instant::now();
let _start_rss = get_resident_set_size();
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
init_rustc_env_logger(&early_dcx);
let mut callbacks = AddCallbacks {};
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
let exit_code = catch_with_exit_code(|| {
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
.set_using_internal_features(using_internal_features)
.run()
});
process::exit(exit_code)
} (It compiles a rust file, printing out the AST after expansion) Is it possible to modify the AST now that I accessed it or is it a read-only view? |
I think it is technically possible, but I'm not sure if the resolver will like it or if it can cause an ICE or miscompilation due to inconsistencies between it's internal state and the AST. What kind of modification do you want to do? |
The idea was to add code to the start and end of each function (So creating a profile function, and adding a call, to that function at the start and end of every other function). The benefit from profiling functions with the AST is that cross-compatibility is much easier, and it feels more robust than modifying source code files (I think I can more easily modify the functions of cargo dependencies?) |
Hello,
I was trying to print and modify the Abstract syntax tree during the after_expansion callback.
When trying to print the AST during that callback, the compiler crashes for stolen value.
This is my first error report so let me know if I did anything wrong or am missing anything here
Code
My only file is
main.rs
:Also, my
Cargo.toml
looks like:Meta
rustc --version --verbose
:Error output
Backtrace
The text was updated successfully, but these errors were encountered: