Skip to content

Commit 621fc16

Browse files
authored
Fix NodeId->HirId lookups by carrying structural context (#1445)
c2rust-refactor would fail due to invalid AST node <-> HIR mappings. This was caused by insufficient disambiguation due to all c2rust-bitfield-derived expanded code sharing the same span with call_site. This PR addresses the aforemented issue by threading structural context through the AST and HIR walkers so NodeId-HirId lookups stay precise even when spans collide. - Added `child_slot`, `NodeContextKey`, and `StructuralContext` helpers to record hierarchical positions for AST nodes; mirrored the same traversal on the HIR side and stitched the maps together during `HirMap` construction - Collapsed wrapper nodes derived accessors and helper shims would erase array/tuple shells on the expression side, leaving `Leaf` nodes that conflicted with the HIR/type view where the same values remained wrapped (e.g. `[u8; 3]` -> `Node([Leaf(u8)])`). We now detect those single-child wrappers, peel just enough layers to expose the matching field list, and otherwise panic with a targeted message if the arity still disagrees. I tried going down another path with more-specific spans, such as using quote_spanned!(field.field_name.span()), using fresh spans with Span::def_site().located_at(old_span), and using attribute metadata (with meta_name_value.lit.span(), but kept running into issues and so abandoned that path.
2 parents 12dfbc7 + 2f8649a commit 621fc16

File tree

4 files changed

+790
-45
lines changed

4 files changed

+790
-45
lines changed

c2rust-refactor/src/ast_manip/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub use self::load_modules::load_modules;
3535
pub use self::output_exprs::fold_output_exprs;
3636
pub use self::remove_paren::remove_paren;
3737
pub use self::seq_edit::{fold_blocks, fold_modules};
38-
pub use self::span_maps::{AstSpanMaps, NodeSpan, SpanNodeKind};
38+
pub use self::span_maps::{
39+
child_slot, AstSpanMaps, NodeContextKey, NodeSpan, SpanNodeKind, StructuralContext,
40+
};
3941
pub use self::visit::Visit;
4042
pub use self::visit_node::{visit_nodes, visit_nodes_post, VisitNode};
4143

0 commit comments

Comments
 (0)