Skip to content

Update the rustc pin #1391

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

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
watch_file rust-toolchain.toml
use flake
16 changes: 9 additions & 7 deletions cli/driver/src/callbacks_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use hax_types::cli_options::{Command, Options, ENV_VAR_OPTIONS_FRONTEND};

use rustc_ast::Crate;
use rustc_driver::{Callbacks, Compilation};
use rustc_interface::{interface, Queries};
use rustc_interface::interface;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::Symbol;

/// Wraps a [Callbacks] structure, and injects some cache-related
Expand Down Expand Up @@ -34,22 +36,22 @@ impl<'a> Callbacks for CallbacksWrapper<'a> {
fn after_crate_root_parsing<'tcx>(
&mut self,
compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
krate: &mut Crate,
) -> Compilation {
self.sub.after_crate_root_parsing(compiler, queries)
self.sub.after_crate_root_parsing(compiler, krate)
}
fn after_expansion<'tcx>(
&mut self,
compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
self.sub.after_expansion(compiler, queries)
self.sub.after_expansion(compiler, tcx)
}
fn after_analysis<'tcx>(
&mut self,
compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
self.sub.after_analysis(compiler, queries)
self.sub.after_analysis(compiler, tcx)
}
}
5 changes: 3 additions & 2 deletions cli/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_feature;
extern crate rustc_hashes;
extern crate rustc_hir;
extern crate rustc_hir_analysis;
extern crate rustc_index;
Expand All @@ -39,7 +40,7 @@ use features::*;
use hax_types::cli_options::{BackendOptions, Command, ENV_VAR_OPTIONS_FRONTEND};

use rustc_driver::{Callbacks, Compilation};
use rustc_interface::{interface, Queries};
use rustc_interface::interface;
use rustc_span::symbol::Symbol;

fn rustc_sysroot() -> String {
Expand Down Expand Up @@ -174,7 +175,7 @@ fn main() {

let exit_code = rustc_driver::catch_with_exit_code({
let rustc_args = rustc_args.clone();
move || rustc_driver::RunCompiler::new(&rustc_args, &mut callbacks).run()
move || rustc_driver::run_compiler(&rustc_args, &mut callbacks)
});

std::process::exit(
Expand Down
228 changes: 113 additions & 115 deletions cli/driver/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hax_frontend_exporter::SInto;
use hax_types::cli_options::{Backend, PathOrDash, ENV_VAR_OPTIONS_FRONTEND};
use rustc_driver::{Callbacks, Compilation};
use rustc_interface::interface;
use rustc_interface::{interface::Compiler, Queries};
use rustc_interface::interface::Compiler;
use rustc_middle::middle::region::Scope;
use rustc_middle::ty::TyCtxt;
use rustc_middle::{
Expand Down Expand Up @@ -76,27 +76,35 @@ fn precompute_local_thir_bodies(
}

use itertools::Itertools;
tcx.hir().body_owners()
.filter(|ldid| {
match tcx.def_kind(ldid.to_def_id()) {
InlineConst | AnonConst => {
fn is_fn(tcx: TyCtxt<'_>, did: rustc_span::def_id::DefId) -> bool {
use rustc_hir::def::DefKind;
matches!(
tcx.def_kind(did),
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) | DefKind::Closure
)
}
!is_fn(tcx, tcx.local_parent(*ldid).to_def_id())
},
_ => true
tcx.hir_body_owners()
.filter(|ldid| match tcx.def_kind(ldid.to_def_id()) {
InlineConst | AnonConst => {
fn is_fn(tcx: TyCtxt<'_>, did: rustc_span::def_id::DefId) -> bool {
use rustc_hir::def::DefKind;
matches!(
tcx.def_kind(did),
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) | DefKind::Closure
)
}
!is_fn(tcx, tcx.local_parent(*ldid).to_def_id())
}
_ => true,
})
.sorted_by_key(|ldid| const_level_of(tcx, *ldid))
.filter(move |ldid| tcx.hir().maybe_body_owned_by(*ldid).is_some())
.filter(move |ldid| tcx.hir_maybe_body_owned_by(*ldid).is_some())
.map(move |ldid| {
tracing::debug!("⏳ Type-checking THIR body for {:#?}", ldid);
let span = tcx.hir().span(tcx.local_def_id_to_hir_id(ldid));
let hir_id = tcx.local_def_id_to_hir_id(ldid);
// The `type_of` anon constants isn't available directly, it needs to be fed by some
// other query. This hack ensures this happens, otherwise `thir_body` returns an error.
// See https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Change.20in.20THIR.20of.20anonymous.20constants.3F/near/509764021 .
for (parent_id, parent) in tcx.hir_parent_iter(hir_id) {
if let rustc_hir::Node::Item(..) = parent {
let _ = tcx.check_well_formed(parent_id.owner.def_id);
break;
}
}
let span = tcx.hir().span(hir_id);
let (thir, expr) = match tcx.thir_body(ldid) {
Ok(x) => x,
Err(e) => {
Expand All @@ -107,15 +115,21 @@ fn precompute_local_thir_bodies(
return (ldid, dummy_thir_body(tcx, span, guar));
}
};
let thir = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
thir.borrow().clone()
})) {
Ok(x) => x,
Err(e) => {
let guar = tcx.dcx().span_err(span, format!("The THIR body of item {:?} was stolen.\nThis is not supposed to happen.\nThis is a bug in Hax's frontend.\nThis is discussed in issue https://github.com./hacspec/hax/issues/27.\nPlease comment this issue if you see this error message!", ldid));
return (ldid, dummy_thir_body(tcx, span, guar));
}
};
if thir.is_stolen() {
let guar = tcx.dcx().span_err(
span,
format!(
"The THIR body of item {:?} was stolen.\n\
This is not supposed to happen.\n\
This is a bug in Hax's frontend.\n\
This is discussed in issue https://github.com./hacspec/hax/issues/27.\n\
Please comment this issue if you see this error message!",
ldid
),
);
return (ldid, dummy_thir_body(tcx, span, guar));
}
let thir = thir.borrow().clone();
tracing::debug!("✅ Type-checked THIR body for {:#?}", ldid);
(ldid, (Rc::new(thir), expr))
})
Expand Down Expand Up @@ -145,9 +159,8 @@ fn convert_thir<'tcx, Body: hax_frontend_exporter::IsBody>(
}

let result = tcx
.hir()
.items()
.map(|id| tcx.hir().item(id).sinto(&state))
.hir_free_items()
.map(|id| tcx.hir_item(id).sinto(&state))
.collect();
let impl_infos = hax_frontend_exporter::impl_def_ids_to_impled_types_and_bounds(&state)
.into_iter()
Expand Down Expand Up @@ -184,102 +197,87 @@ impl From<ExtractionCallbacks> for hax_frontend_exporter_options::Options {
}

impl Callbacks for ExtractionCallbacks {
fn after_crate_root_parsing<'tcx>(
&mut self,
compiler: &Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
let parse_ast = queries.parse().unwrap();
let parse_ast = parse_ast.borrow();
Compilation::Continue
}
fn after_expansion<'tcx>(
&mut self,
compiler: &Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
fn after_expansion<'tcx>(&mut self, compiler: &Compiler, tcx: TyCtxt<'tcx>) -> Compilation {
use std::ops::{Deref, DerefMut};

queries.global_ctxt().unwrap().enter(|tcx| {
use hax_frontend_exporter::ThirBody;
use hax_types::cli_options::Command;
use rustc_session::config::CrateType;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::BufWriter;
use hax_frontend_exporter::ThirBody;
use hax_types::cli_options::Command;
use rustc_session::config::CrateType;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::BufWriter;

use std::path::PathBuf;
use std::path::PathBuf;

let opts = &compiler.sess.opts;
let externs: Vec<_> = opts
.externs
.iter()
.flat_map(|(_, ext)| match &ext.location {
rustc_session::config::ExternLocation::ExactPaths(set) => set
.iter()
.map(|cp| cp.canonicalized())
.collect::<Vec<_>>()
.into_iter(),
_ => vec![].into_iter(),
})
.map(|path| path.with_extension("haxmeta"))
.collect();
let opts = &compiler.sess.opts;
let externs: Vec<_> = opts
.externs
.iter()
.flat_map(|(_, ext)| match &ext.location {
rustc_session::config::ExternLocation::ExactPaths(set) => set
.iter()
.map(|cp| cp.canonicalized())
.collect::<Vec<_>>()
.into_iter(),
_ => vec![].into_iter(),
})
.map(|path| path.with_extension("haxmeta"))
.collect();

let cg_metadata = opts.cg.metadata[0].clone();
let crate_name = opts.crate_name.clone().unwrap();
let cg_metadata = opts.cg.metadata[0].clone();
let crate_name = opts.crate_name.clone().unwrap();

let output_dir = compiler.sess.io.output_dir.clone().unwrap();
let haxmeta_path = output_dir.join(format!("{crate_name}-{cg_metadata}.haxmeta",));
let output_dir = compiler.sess.io.output_dir.clone().unwrap();
let haxmeta_path = output_dir.join(format!("{crate_name}-{cg_metadata}.haxmeta",));

let mut file = BufWriter::new(File::create(&haxmeta_path).unwrap());
let mut file = BufWriter::new(File::create(&haxmeta_path).unwrap());

use hax_types::driver_api::{with_kind_type, HaxMeta};
with_kind_type!(
self.body_types.clone(),
<Body>|| {
let (spans, def_ids, impl_infos, items, cache_map) =
convert_thir(&self.clone().into(), tcx);
let files: HashSet<PathBuf> = HashSet::from_iter(
items
.iter()
.flat_map(|item| item.span.filename.to_path().map(|path| path.to_path_buf()))
);
let haxmeta: HaxMeta<Body> = HaxMeta {
crate_name,
cg_metadata,
externs,
impl_infos,
items,
comments: files.into_iter()
.flat_map(|path|hax_frontend_exporter::comments::comments_of_file(path).ok())
.flatten()
.collect(),
def_ids,
hax_version: hax_types::HAX_VERSION.into(),
};
haxmeta.write(&mut file, cache_map);
}
);
use hax_types::driver_api::{with_kind_type, HaxMeta};
with_kind_type!(
self.body_types.clone(),
<Body>|| {
let (spans, def_ids, impl_infos, items, cache_map) =
convert_thir(&self.clone().into(), tcx);
let files: HashSet<PathBuf> = HashSet::from_iter(
items
.iter()
.flat_map(|item| item.span.filename.to_path().map(|path| path.to_path_buf()))
);
let haxmeta: HaxMeta<Body> = HaxMeta {
crate_name,
cg_metadata,
externs,
impl_infos,
items,
comments: files.into_iter()
.flat_map(|path|hax_frontend_exporter::comments::comments_of_file(path).ok())
.flatten()
.collect(),
def_ids,
hax_version: hax_types::HAX_VERSION.into(),
};
haxmeta.write(&mut file, cache_map);
}
);

let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let manifest_dir = std::path::Path::new(&manifest_dir);
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let manifest_dir = std::path::Path::new(&manifest_dir);

let data = hax_types::driver_api::EmitHaxMetaMessage {
manifest_dir: manifest_dir.to_path_buf(),
working_dir: opts
.working_dir
.to_path(rustc_span::FileNameDisplayPreference::Local)
.to_path_buf(),
path: haxmeta_path,
};
eprintln!(
"{}{}",
hax_types::driver_api::HAX_DRIVER_STDERR_PREFIX,
&serde_json::to_string(&hax_types::driver_api::HaxDriverMessage::EmitHaxMeta(data))
.unwrap()
);
let data = hax_types::driver_api::EmitHaxMetaMessage {
manifest_dir: manifest_dir.to_path_buf(),
working_dir: opts
.working_dir
.to_path(rustc_span::FileNameDisplayPreference::Local)
.to_path_buf(),
path: haxmeta_path,
};
eprintln!(
"{}{}",
hax_types::driver_api::HAX_DRIVER_STDERR_PREFIX,
&serde_json::to_string(&hax_types::driver_api::HaxDriverMessage::EmitHaxMeta(data))
.unwrap()
);

Compilation::Stop
})
Compilation::Stop
}
}
22 changes: 10 additions & 12 deletions cli/driver/src/features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashSet;

use rustc_driver::{Callbacks, Compilation};
use rustc_interface::{interface, Queries};
use rustc_interface::interface;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::Symbol;

use crate::callbacks_wrapper::CallbacksWrapper;
Expand Down Expand Up @@ -86,31 +87,28 @@ impl Features {
fn after_expansion<'tcx>(
&mut self,
compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
self.features = tcx.features().into();
self.features.registered_tools = tcx
.registered_tools(())
.iter()
.map(|x| x.name.to_ident_string())
.collect();
});
self.features = tcx.features().into();
self.features.registered_tools = tcx
.registered_tools(())
.iter()
.map(|x| x.name.to_ident_string())
.collect();
rustc_driver::Compilation::Stop
}
}
let mut callbacks = CollectFeatures {
features: Features::default(),
};
let exit_code = rustc_driver::catch_with_exit_code(|| {
rustc_driver::RunCompiler::new(
rustc_driver::run_compiler(
rustc_args,
&mut CallbacksWrapper {
sub: &mut callbacks,
options: options.clone(),
},
)
.run()
});
if exit_code != 0 {
std::process::exit(exit_code);
Expand Down
Loading
Loading