Skip to content

Commit

Permalink
Merge #139
Browse files Browse the repository at this point in the history
139: Fix steal bug r=W95Psp a=W95Psp

This is an attempt at fixing the frontend exporter bug described in #27.

The fix is really simple: it's all about processing `const`-like items first.

`@franziskuskiefer:` I tested a bit this fix on a reduced version of the example you posted in #27, but I think you've been hitting that much more, can you try this patch?

EDIT: also tried on `@geonnave's` example of #27, seems to be fine as well!

Co-authored-by: Lucas Franceschino <lucas.franceschino@inria.fr>
  • Loading branch information
bors[bot] and W95Psp authored Jun 13, 2023
2 parents 12131c1 + a4eec06 commit d44a0e3
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions cli/driver/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,38 @@ fn convert_thir<'tcx>(
tcx: TyCtxt<'tcx>,
) -> (Vec<rustc_span::Span>, Vec<hax_frontend_exporter::Item>) {
let hir = tcx.hir();
let bodies = hir.body_owners();

let mut bodies = bodies.collect::<Vec<_>>();
// we first visit `AnonConst`s, otherwise the thir body might be stolen
bodies.sort_by(|a, b| {
use std::cmp::Ordering::*;
let is_anon_const = |x: &rustc_span::def_id::LocalDefId| {
matches!(hir.get_by_def_id(x.clone()), rustc_hir::Node::AnonConst(_))
let bodies = {
// Here, we partition the bodies so that constant items appear
// first.
let mut is_const = |x: &rustc_span::def_id::LocalDefId| {
matches!(
hir.get_by_def_id(x.clone()),
rustc_hir::Node::AnonConst(_)
| rustc_hir::Node::Item(rustc_hir::Item {
kind: rustc_hir::ItemKind::Const(..),
..
})
| rustc_hir::Node::TraitItem(rustc_hir::TraitItem {
kind: rustc_hir::TraitItemKind::Const(..),
..
})
| rustc_hir::Node::ImplItem(rustc_hir::ImplItem {
kind: rustc_hir::ImplItemKind::Const(..),
..
})
)
};
if is_anon_const(a) {
Less
} else if is_anon_const(b) {
Equal
} else {
Greater
}
});

let (consts, others): (Vec<rustc_span::def_id::LocalDefId>, _) =
hir.body_owners().partition(is_const);
consts.into_iter().chain(others.into_iter())
};

let thirs: std::collections::HashMap<
rustc_span::def_id::LocalDefId,
(rustc_middle::thir::Thir<'tcx>, ExprId),
> = bodies
.into_iter()
.map(|did| {
let (thir, expr) = tcx
.thir_body(rustc_middle::ty::WithOptConstParam {
Expand Down

0 comments on commit d44a0e3

Please sign in to comment.