Skip to content

Conversation

@oinoom
Copy link
Contributor

@oinoom oinoom commented Nov 10, 2025

canonicalize_refs used cx.hir_map().node_to_hir_id and unconditionally called typeck(parent). When MutVisitNodes descends into attribute tokens or items without bodies (e.g., extern decls) the HIR map lacks a node or typeck tables, so the walk panicked with span_bug!("can't type-check body …").

This PR swaps to opt_node_to_hir_id so attribute-only nodes short-circuit before we ever try to fetch HIR expressions, keeping the transform aligned with what rustc actually lowers, and checks maybe_body_owned_by(parent) before querying typeck tables; this skips extern items and other definitions without bodies, avoiding the primary_body_of unwrap panic while still running the transform over real bodies.

…o canonicalize_refs can see ExprIds whose owners lack bodies or typeck tables. Gate the visitor with opt_node_to_hir_id() and skip expressions whose parent items don't own bodies to avoid span bugs when calling typeck(parent).
@oinoom oinoom marked this pull request as ready for review November 10, 2025 22:23
fn transform(&self, krate: &mut Crate, _st: &CommandState, cx: &RefactorCtxt) {
MutVisitNodes::visit(krate, |expr: &mut P<Expr>| {
let hir_id = cx.hir_map().node_to_hir_id(expr.id);
// `MutVisitNodes` walks into attributes even though rustc never lowers their bodies to HIR.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these expressions inside attributes? Otherwise how would we hit this for P<Expr>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ones like #[c2rust::src_loc = "28:1"] have a rhs that's an expression

@oinoom oinoom merged commit 3b2788c into master Nov 17, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants