feat(codegen): switch the lowering walk from &Term to typed &MTerm (mir.1b)
Atomic completion of spec iteration mir.1 (docs/specs/0060-typed-mir.md): codegen now consumes the typed MIR produced by `lower_to_mir` instead of re-deriving types from the bare `ast::Term`. Every codegen helper that took `&Term` (lower_term, lower_app, drop.rs, match_lower.rs) takes `&MTerm` and reads each node's checker-proved type off `MTerm::ty()`. The build path is `Workspace -> elaborate_workspace -> MirWorkspace -> lower_workspace`; the public `lower_workspace*` entry points and their 18 call sites thread `&MirWorkspace`. The codegen-side type re-derivers `synth_with_extras` + `synth_arg_type` (and the `builtin_ail_type` / `builtin_effect_op_ret` mirror tables) are deleted — grep-clean. The three re-derivers the spec keeps until mir.2/mir.3 (`type_home_module`, `is_static_callee`, the second `infer_module_with_cross`) stay. The mechanical Term->MTerm match-arm conversion was straightforward and compiler-enforced. The substance was a set of producer-side correctness gaps that only surface once codegen reads `MTerm::ty()` and once the build path re-synthesises the post-mono AST through the canonical `synth` (which, unlike the old codegen, fully re-unifies). Each was root-caused against a failing e2e fixture: 1. `qualify_local_types` stripped fn-type modes (rebuilt `Type::Fn` with empty `param_modes` / `Implicit` `ret_mode`), so a monomorphised polymorphic intrinsic (`RawBuf.set`) lost its `Own` ret-mode and the owned temporary leaked at the call site. Made mode-preserving, like its sister `qualify_workspace_types` and `Subst::apply` (449df13). 2. `lower_to_mir::synth_pure` synthesises each node in isolation, so a nullary polymorphic ctor (`Nil : List<a>`) left its element type an unbound `$m` metavar that the canonical synth never pins. Codegen's mono unifier (`unify_for_subst`) already has a wildcard for exactly this — `$u`, the spelling the now-deleted codegen synth used — so the typed-MIR boundary normalises every residual `$m` to `$u` once (`wildcard_residual_metavars`), rather than teaching each consumer to tolerate a raw metavar. 3. The class-method mono arm (`synthesise_mono_fn`) substituted the registry-canonical *qualified* instance type into a method appended to the instance's own module, minting a `show_user_adt.IntBox` param against a bare-`IntBox` body. Localised to bare before substitution, symmetric to the free-fn arm (600565d). 4. Monomorphisation synthesises *downward* class-dispatch references — prelude's `print__<IntBox>` names the instance module `show_user_adt` that prelude never imports. The post-mono re-synth in `lower_module` seeds every workspace module name as an identity import (excluding the current module, to keep own types bare) so the qualified-var path resolves these; the canonical `synth` used by `check_workspace` stays strict. 5. Post-mono, a cross-module callee can name the consumer's *own* ADT qualified (`show_user_adt.IntBox`) where the consumer synthesises it bare — a spelling split that cannot exist pre-mono (the param is polymorphic there). synth's App arm strips the current module's own qualifier from both sides before unifying (`strip_own_module_qual`), a no-op pre-mono. Acceptance: whole workspace suite green (698 tests); `e2e` 98/98 and `show_print_e2e` 3/3; `synth_with_extras`/`synth_arg_type` grep-clean in codegen; #51/#53 fixtures build and run; lower_to_mir_ty pins green. The #49 heap-Str loop-binder leak remains ignored (lifts at mir.4). Builds on the standalone producer fix (600565d, free-fn own-ADT localisation) and the two standalone mode-preservation fixes (449df13Subst::apply); those landed separately as they are independently correct and inert on the old codegen.
This commit is contained in:
@@ -25,7 +25,8 @@
|
||||
//! this submodule into the parent's private `Emitter` fields works
|
||||
//! through the standard descendant-module privacy lane.
|
||||
|
||||
use ailang_core::ast::*;
|
||||
use ailang_core::ast::{ParamMode, Type, TypeDef};
|
||||
use ailang_mir::{Callee, MTerm};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use super::synth::llvm_type;
|
||||
@@ -454,16 +455,16 @@ impl<'a> Emitter<'a> {
|
||||
/// here. A `Term::Var` returning an RC-allocated box would already
|
||||
/// be tracked by an earlier let-binder; tracking it again here
|
||||
/// would double-dec.
|
||||
pub(crate) fn is_rc_heap_allocated(&self, value: &Term) -> bool {
|
||||
pub(crate) fn is_rc_heap_allocated(&self, value: &MTerm) -> bool {
|
||||
if !matches!(self.alloc, AllocStrategy::Rc) {
|
||||
return false;
|
||||
}
|
||||
match value {
|
||||
Term::Ctor { .. } | Term::Lam { .. } => {
|
||||
let term_ptr = (value as *const Term) as usize;
|
||||
MTerm::Ctor { .. } | MTerm::Lam { .. } => {
|
||||
let term_ptr = (value as *const MTerm) as usize;
|
||||
!self.non_escape.contains(&term_ptr)
|
||||
}
|
||||
Term::App { callee, .. } => {
|
||||
MTerm::App { callee, .. } => {
|
||||
// a call whose callee carries
|
||||
// `ret_mode == Own` hands a fresh heap allocation to
|
||||
// the caller's frame. Trackable. `Borrow` and
|
||||
@@ -475,7 +476,7 @@ impl<'a> Emitter<'a> {
|
||||
.map(|m| matches!(m, ParamMode::Own))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
Term::Loop { .. } => {
|
||||
MTerm::Loop { .. } => {
|
||||
// Loop result is owned-and-untracked (seeds are moved in).
|
||||
// Track iff its static type is boxed/heap (llvm `ptr`) AND not
|
||||
// Str. Str is excluded: a loop can return a *static* Str (a seed
|
||||
@@ -484,20 +485,16 @@ impl<'a> Emitter<'a> {
|
||||
// never return a static Str, which is why the App arm may track Str
|
||||
// but the Loop arm must not. Unboxed primitives (Int/Bool/Float/Unit)
|
||||
// lower to non-`ptr` and are correctly excluded by the `ptr` gate.
|
||||
match self.synth_arg_type(value) {
|
||||
Ok(t) => {
|
||||
let is_ptr = matches!(
|
||||
crate::synth::llvm_type(&t).as_deref(),
|
||||
Ok("ptr")
|
||||
);
|
||||
let is_str = matches!(
|
||||
&t,
|
||||
Type::Con { name, .. } if name == "Str"
|
||||
);
|
||||
is_ptr && !is_str
|
||||
}
|
||||
Err(_) => false,
|
||||
}
|
||||
let t = value.ty();
|
||||
let is_ptr = matches!(
|
||||
crate::synth::llvm_type(&t).as_deref(),
|
||||
Ok("ptr")
|
||||
);
|
||||
let is_str = matches!(
|
||||
&t,
|
||||
Type::Con { name, .. } if name == "Str"
|
||||
);
|
||||
is_ptr && !is_str
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
@@ -510,8 +507,11 @@ impl<'a> Emitter<'a> {
|
||||
/// is defensive). Used by [`Self::is_rc_heap_allocated`] and the
|
||||
/// [`Self::drop_symbol_for_binder`] App-arm to decide both
|
||||
/// trackability and the drop-fn symbol.
|
||||
fn synth_callee_ret_mode(&self, callee: &Term) -> Option<ParamMode> {
|
||||
let cty = self.synth_arg_type(callee).ok()?;
|
||||
fn synth_callee_ret_mode(&self, callee: &Callee) -> Option<ParamMode> {
|
||||
let cty = match callee {
|
||||
Callee::Indirect(inner) => inner.ty(),
|
||||
Callee::Static { .. } => return None,
|
||||
};
|
||||
match cty {
|
||||
Type::Fn { ret_mode, .. } => Some(ret_mode),
|
||||
_ => None,
|
||||
@@ -530,9 +530,9 @@ impl<'a> Emitter<'a> {
|
||||
/// unreachable since `is_rc_heap_allocated` only returns `true`
|
||||
/// for `Term::Ctor` / `Term::Lam`, but a defensive fallback
|
||||
/// keeps the IR well-formed even if the predicate ever widens.
|
||||
pub(crate) fn drop_symbol_for_binder(&self, value: &Term, val_ssa: &str) -> String {
|
||||
pub(crate) fn drop_symbol_for_binder(&self, value: &MTerm, val_ssa: &str) -> String {
|
||||
match value {
|
||||
Term::Ctor { type_name, .. } => {
|
||||
MTerm::Ctor { type_name, .. } => {
|
||||
if type_name.matches('.').count() == 1 {
|
||||
let (prefix, suffix) =
|
||||
type_name.split_once('.').expect("checked");
|
||||
@@ -543,7 +543,7 @@ impl<'a> Emitter<'a> {
|
||||
}
|
||||
format!("drop_{m}_{type_name}", m = self.module_name)
|
||||
}
|
||||
Term::Lam { .. } => self
|
||||
MTerm::Lam { .. } => self
|
||||
.closure_drops
|
||||
.get(val_ssa)
|
||||
.cloned()
|
||||
@@ -557,8 +557,8 @@ impl<'a> Emitter<'a> {
|
||||
// the ret-type is not a `Type::Con` (e.g. a bare type
|
||||
// var on an as-yet-unmonomorphised polymorphic call —
|
||||
// the monomorphised copies will resolve correctly).
|
||||
Term::App { .. } | Term::Loop { .. } => {
|
||||
if let Ok(Type::Con { name, .. }) = self.synth_arg_type(value) {
|
||||
MTerm::App { .. } | MTerm::Loop { .. } => {
|
||||
if let Type::Con { name, .. } = value.ty() {
|
||||
// Symmetric to `field_drop_call`'s Str arm: Str is a
|
||||
// built-in pointer type with no per-type drop fn. Both
|
||||
// heap-Str (rc_header at payload-8) and static-Str
|
||||
@@ -843,25 +843,22 @@ impl<'a> Emitter<'a> {
|
||||
/// the runtime tag and dec's only the unmoved fields.
|
||||
pub(crate) fn emit_inlined_partial_drop(
|
||||
&mut self,
|
||||
value: &Term,
|
||||
value: &MTerm,
|
||||
val_ssa: &str,
|
||||
moved: &BTreeSet<usize>,
|
||||
) -> Result<()> {
|
||||
let (type_name, ctor_name) = match value {
|
||||
Term::Ctor { type_name, ctor, .. } => (type_name.as_str(), ctor.as_str()),
|
||||
MTerm::Ctor { type_name, ctor, .. } => (type_name.as_str(), ctor.as_str()),
|
||||
_ => {
|
||||
// dynamic-tag partial-drop via the
|
||||
// per-type helper. `value` is `Term::App` (Own-
|
||||
// per-type helper. `value` is `MTerm::App` (Own-
|
||||
// returning) — the binder's static type is the App's
|
||||
// ret type, recovered through `synth_arg_type` /
|
||||
// `partial_drop_symbol_for_type`. `Term::Lam` shapes
|
||||
// ret type, read off `value.ty()` and mapped through
|
||||
// `partial_drop_symbol_for_type`. `MTerm::Lam` shapes
|
||||
// never reach here with a non-empty `moved` (you can't
|
||||
// pattern-match a closure-pair); the fallback below
|
||||
// handles them defensively.
|
||||
let sym = self
|
||||
.synth_arg_type(value)
|
||||
.ok()
|
||||
.and_then(|ty| self.partial_drop_symbol_for_type(&ty));
|
||||
let sym = self.partial_drop_symbol_for_type(&value.ty());
|
||||
if let (Some(sym), Some(mask)) =
|
||||
(sym, Self::build_moved_mask(moved))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user