Commit 89b75f0
authored
Refactor/printer (#122)
This PR splits `src/printer.rs` (~1700 lines) into a `src/printer/`
directory module with focused submodules, then follows up with doc
comments, idiomatic Rust cleanup, and a structural improvement to the
phase boundary enforcement.
All integration and UI tests pass unchanged.
## Structure
The split creates nine submodules, each with a single responsibility:
| Module | Lines | Responsibility |
|--------|------:|----------------|
| `schema.rs` | ~510 | Data model types (`SmirJson`, `Item`,
`AllocInfo`, `TypeMetadata`, `LinkMapKey`, etc.) |
| `mir_visitor.rs` | ~430 | `BodyAnalyzer`: single-pass MIR body
traversal collecting calls, allocs, types, spans |
| `collect.rs` | ~230 | Three-phase pipeline: collect items, analyze
bodies, assemble final output |
| `items.rs` | ~225 | `Item` construction from `MonoItem` with optional
debug-level details |
| `types.rs` | ~170 | `TypeMetadata` construction from `TyKind` + layout
|
| `ty_visitor.rs` | ~120 | Recursive type visitor collecting all
reachable types with layout info |
| `link_map.rs` | ~107 | Link-time function resolution map (type +
instance kind to symbol name) |
| `mod.rs` | ~83 | Module glue: macros, env-var helpers, re-exports,
`emit_smir` |
| `util.rs` | ~64 | Name resolution, attribute queries, small helpers |
## Changes by commit
1. **Split printer.rs into a directory module**: mechanical extraction;
no code changes beyond moving `use` / `extern crate` declarations into
each submodule and adjusting visibility.
2. **Add module-level doc comments to all printer submodules**: `//!`
doc headers describing each module's purpose and key types.
3. **Idiomatic Rust cleanup across printer submodules**:
- `if let Some(x) = y { x } else { z }` to `y.unwrap_or_else(|| z)`
- `if opt.is_none() { return; } let val = opt.unwrap()` to `let
Some(val) = opt else { return; }`
- Remove unnecessary `.clone()` calls on `key` in `get_mut` / `insert`
- Change `collect_alloc` to take `offset: usize` by value (was `&usize`)
- Extract `opaque_placeholder_ty()` helper for the `Ty::to_val(0)`
sentinel
- Doc comments on `FnSymType`, `LinkMapKey`, `AllocInfo`,
`TypeMetadata`, `SourceData`, `SmirJson`, `SmirJsonDebugInfo`
4. **Address review feedback and fix doc comment inaccuracies**.
5. **Remove `MonoItem` from `Item` for structural phase enforcement**:
The three-phase collection pipeline has an invariant: phase 3
(`assemble_smir`) must not call `inst.body()` or otherwise re-enter
rustc. Previously this was enforced by convention only, because `Item`
carried a `pub(super) mono_item: MonoItem` field that gave any code with
an `&Item` full access to `Instance::body()`.
This commit removes `mono_item` from `Item` entirely, so the invariant
is structural: phase 3 code literally cannot call `inst.body()` because
it never has a `MonoItem` to call it on. The type system enforces what
discipline previously had to.
Concretely: `mk_item` now returns `(MonoItem, Item)` instead of `Item`,
and the phase 1+2 maps carry `(MonoItem, Item)` tuples. The `MonoItem`
lives alongside the `Item` during collection and analysis, gets passed
directly to `maybe_add_to_link_map` and `warn_missing_body` (now a free
function in `collect.rs`), and is naturally dropped when only the `Item`
is pushed into `all_items`. By the time `assemble_smir` runs, no
`MonoItem` values exist.
As a bonus, this fixes a latent `PartialEq`/`Ord` inconsistency:
`PartialEq` was comparing `mono_item` while `Ord` compared `symbol_name`
+ item-kind name. `PartialEq` now delegates to `Ord`, so the two are
consistent.
## Test plan
- [x] `cargo build` succeeds
- [x] `cargo clippy` clean
- [x] `make integration-test` passes (28/28)
- [x] `make test-ui` passes (2875/2875, 0 failures)1 parent e875066 commit 89b75f0
12 files changed
Lines changed: 2001 additions & 1715 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
75 | 81 | | |
76 | 82 | | |
77 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| |||
409 | 410 | | |
410 | 411 | | |
411 | 412 | | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
412 | 417 | | |
413 | 418 | | |
414 | 419 | | |
| |||
0 commit comments