Skip to content

Factor: collapse triplicated alloc recovery into one arm#135

Merged
automergerpr-permission-manager[bot] merged 3 commits intomasterfrom
cds/refactor-mir-visitor
Mar 3, 2026
Merged

Factor: collapse triplicated alloc recovery into one arm#135
automergerpr-permission-manager[bot] merged 3 commits intomasterfrom
cds/refactor-mir-visitor

Conversation

@cds-amal
Copy link
Collaborator

@cds-amal cds-amal commented Mar 3, 2026

This is a follow-up to #131. That PR fixed a real panic (non-builtin-deref Static/VTable allocations), but the fix landed as three nearly identical match arms: Static, VTable, and Function, each repeating the same "try get_prov_ty, fall back to opaque placeholder" logic. The only meaningful difference between them was a single predicate ("is this type usable directly?") and the debug log string. This PR collapses them back into one arm, makes the predicate explicit, and documents a previously unexplained jq filter.

What changed

src/printer/mir_visitor.rs

The three arms now share a single code path. The predicate that actually differs between them is captured in a needs_recovery variable:

  • Static/VTable: builtin_deref(true).is_none() (not a reference, raw pointer, or Box)
  • Function: !kind.is_fn_ptr() (outer type isn't already a function pointer)

Worth noting: these two predicates are not equivalent (a function pointer passes builtin_deref but fails is_fn_ptr), which is why a naive "just combine the arms" without the inner match would have changed behavior for Function allocs. The inner match on global_alloc makes this asymmetry visible rather than hiding it across separate arms.

The rest of the logic (try get_prov_ty, fall back to opaque placeholder) is written once. Also dropped an unnecessary .clone() on the builtin_deref call; it takes &self.

tests/integration/normalise-filter.jq

The def_id filter had a terse comment ("unrelated to this regression") that didn't explain why it exists or why it's safe to strip globally. Replaced it with a proper explanation: def_id values are interned compiler indices (same class as alloc_id and adt_def) that are consistent within a single rustc invocation but non-deterministic across runs. Downstream consumers need them as cross-reference keys to join AggregateKind::Adt in MIR bodies with type metadata entries, so they can't be dropped from the output itself; we only strip them here for golden-file comparison. (See #125 for the full picture on interned-index non-determinism.)

Test plan

  • cargo build compiles cleanly
  • make integration-test passes (no behavioral change; this is a pure refactor)

cds-amal added 3 commits March 3, 2026 00:00
builtin_deref takes &self, so the .clone() on kind before calling
it is unnecessary.
Replace the terse "unrelated to this regression" comment with a
detailed explanation: def_id values are interned compiler indices
(same class as alloc_id and adt_def) that are stable within a run
but non-deterministic across runs. Downstream consumers need them
as cross-reference keys for AggregateKind::Adt joins, so they
can't be dropped from the output; the filter strips them only for
golden-file comparison.
@cds-amal cds-amal requested review from a team, Stevengre and dkcumming March 3, 2026 05:27
Copy link
Collaborator

@dkcumming dkcumming left a comment

Choose a reason for hiding this comment

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

Nice! Tested downstream with mir-semantics (682df6b2dfec4ec0516e246f22b70dff09cf57a6) test suite which is passing

@automergerpr-permission-manager automergerpr-permission-manager bot merged commit 6d02243 into master Mar 3, 2026
5 checks passed
@automergerpr-permission-manager automergerpr-permission-manager bot deleted the cds/refactor-mir-visitor branch March 3, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants