iter 24.3: fn print + E2E + 3 compiler-path repairs; milestone 24 close
Ships fn print : forall a. Show a => (a borrow) -> () !IO in the
prelude with explicit-let body \\x -> let s = show x in do
io/print_str s. Three new E2E fixtures + tests verify the full
path:
- show_print_smoke: 4 primitives smoke (print 42 / true / 'hello' /
3.14) — compile, run, expected stdout
- show_user_adt: data IntBox + instance prelude.Show IntBox +
print (MkIntBox 7) — stdout '7'
- show_no_instance: let f : Int -> Int = \\x -> x in do print f —
fires Show-aware NoInstance with DESIGN.md §Prelude(built-in)
classes cross-reference
IR-shape pin in print_mono_body_shape.rs asserts post-mono
print__Int.body is structurally Term::Let → App(show__Int) →
Do(io/print_str). Protects the explicit let-binder for the heap-Str
RC discipline per eob.1 Str carve-out.
Three plan-defects-fixed-inline surfaced during user-ADT E2E,
necessary repairs to make the spec's stated user-ADT trajectory
work end-to-end:
(a) mono.rs (2 sites): MonoTarget::FreeFn::type_args were carrying
bare type-cons references; normalised to canonical
<owner>.<bare> form via workspace_registry.normalize_type_for_lookup
so synthesised cross-module bodies' post-mono walks reach the
registry-keyed instance entries. Symmetric to the existing
class-method-arm normalisation.
(b) codegen/lib.rs (3 sites: resolve_top_level_fn, lower_app
cross-module arm, synth_with_extras Var arm): post-mono
synthesised bodies may carry cross-module references to modules
their source template didn't import (prelude.print__<UserType>
references show_user_adt.show__<UserType> even though prelude
does not import user modules). Fall back to direct
module_user_fns lookup when prefix not in import_map. Both
ends were independently typechecked before mono ran; the
cross-module ref is created by mono not by source.
(c) lib.rs synth FreeFnCall arm: walked Type::Forall.constraints,
substituted rigid vars with fresh metavars, pushed one
ResidualConstraint per declared constraint. Without this,
print f at Int -> Int would silently typecheck and fail with
a confusing 'unknown variable: show' at codegen rather than
fire the right typecheck-time NoInstance diagnostic.
NoInstance Float-aware arm in check/lib.rs:770-779 extended with
a parallel class == 'prelude.Show' branch that cross-references
DESIGN.md §Prelude(built-in) classes verbatim. Negative-fixture
test asserts code 'no-instance' + Show substring + Prelude-(built-in)-
classes substring.
DESIGN.md §Prelude(built-in) classes milestone-24 paragraph flips
fn print from 'ships in 24.3' to 'shipped in iter 24.3' with body
shape + pin file reference. §Float semantics gains a Show-Float
NaN-spelling cross-reference paragraph linking show 1.5 / show nan /
show inf to instance Show Float via float_to_str.
Roadmap P1 'Post-22 Prelude — Show + print rewire' flipped to [x]
with closing summary naming all three shipped iters (24.1 / 24.2 /
24.3). New P2 entry 'Retire io/print_int|bool|float effect-ops +
migrate example corpus to print' inserted at top of P2.
Tests: 556 passed (was 552 + 4 new). bench/cross_lang exit 0;
bench/compile_check + bench/check exit 1 on documented noise-class
metrics per the audit-cma lineage envelope (8th consecutive
audit-grade observation, baseline pristine per conservative-call).
Milestone 24 closes structurally with this iter. Standard audit
pipeline next.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
//! IR-shape pin for milestone 24.3's `print` polymorphic free fn.
|
||||
//!
|
||||
//! Property protected: the post-mono `print__Int` `Def::Fn.body` is
|
||||
//! structurally `Term::Let { name: "s", value: App(show__Int, [x]),
|
||||
//! body: Term::Do { op: "io/print_str", args: [s] } }`. The explicit
|
||||
//! let-binder around `show__Int x` is load-bearing for the heap-Str RC
|
||||
//! discipline (eob.1 Str carve-out at `drop_symbol_for_binder` requires
|
||||
//! a let-binder to attach the rc-dec to). If a future codegen / mono
|
||||
//! refactor inlines the let-binder away, this pin fires and surfaces
|
||||
//! the regression BEFORE the E2E runtime stats produce a confusing
|
||||
//! "memory leak" diagnostic.
|
||||
//!
|
||||
//! Note: `FnDef.body: Term` is the *inner* function body — top-level
|
||||
//! fns carry their parameter list separately on `FnDef.params`, so no
|
||||
//! outer `Term::Lam` wraps the body. This contrasts with instance
|
||||
//! method bodies (which carry a `Term::Lam` because they are anonymous
|
||||
//! function values inside `InstanceMethod.body`).
|
||||
|
||||
use ailang_core::ast::{Def, Term};
|
||||
use ailang_core::workspace::load_workspace;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn fixture_path() -> PathBuf {
|
||||
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
d.pop();
|
||||
d.pop();
|
||||
d.join("examples").join("show_print_smoke.ail.json")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_int_body_preserves_explicit_let_binder() {
|
||||
let ws = load_workspace(&fixture_path()).expect("workspace loads");
|
||||
let diags = ailang_check::check_workspace(&ws);
|
||||
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
|
||||
let post_mono = ailang_check::monomorphise_workspace(&ws).expect("mono green");
|
||||
|
||||
let prelude_mod = post_mono
|
||||
.modules
|
||||
.get("prelude")
|
||||
.expect("prelude module present");
|
||||
|
||||
let print_int = prelude_mod
|
||||
.defs
|
||||
.iter()
|
||||
.find_map(|d| match d {
|
||||
Def::Fn(f) if f.name == "print__Int" => Some(f),
|
||||
_ => None,
|
||||
})
|
||||
.expect("print__Int mono symbol not found in prelude module");
|
||||
|
||||
// Body shape: Term::Let { name: "s",
|
||||
// value: Term::App(show__Int, [x]),
|
||||
// body: Term::Do { op: "io/print_str", args: [s] } }
|
||||
let (let_value, let_body) = match &print_int.body {
|
||||
Term::Let { name, value, body } => {
|
||||
assert_eq!(name, "s", "let-binder name expected 's', got {name:?}");
|
||||
(value.as_ref(), body.as_ref())
|
||||
}
|
||||
other => panic!(
|
||||
"print__Int.body is not Term::Let — let-binder was optimised away or never inserted. Got: {other:?}"
|
||||
),
|
||||
};
|
||||
|
||||
// Inner App should reference show__Int (post-mono symbol).
|
||||
let app_fn = match let_value {
|
||||
Term::App { callee, .. } => callee.as_ref(),
|
||||
other => panic!("print__Int let-value is not Term::App, got: {other:?}"),
|
||||
};
|
||||
let app_fn_name = match app_fn {
|
||||
Term::Var { name } => name,
|
||||
other => panic!("let-value's fn position is not Term::Var, got: {other:?}"),
|
||||
};
|
||||
assert_eq!(
|
||||
app_fn_name, "show__Int",
|
||||
"post-mono let-value should call show__Int, got: {app_fn_name}"
|
||||
);
|
||||
|
||||
// Inner body should be a Do invoking io/print_str.
|
||||
match let_body {
|
||||
Term::Do { op, args, .. } => {
|
||||
assert_eq!(op, "io/print_str", "let-body Do op expected 'io/print_str', got {op:?}");
|
||||
assert_eq!(args.len(), 1, "io/print_str expects 1 arg, got {}", args.len());
|
||||
}
|
||||
other => panic!("print__Int let-body is not Term::Do, got: {other:?}"),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
//! Pins the Show-aware `no-instance` diagnostic shipped in milestone 24.3.
|
||||
//!
|
||||
//! Property protected: calling `print` on a function type fires the
|
||||
//! `no-instance` diagnostic with a Show-aware message that
|
||||
//! cross-references DESIGN.md §"Prelude (built-in) classes", so the
|
||||
//! LLM author immediately learns which types ship with built-in Show
|
||||
//! and how to declare their own instance for a user type.
|
||||
|
||||
use ailang_check::check_workspace;
|
||||
use ailang_core::workspace::load_workspace;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn fixture(name: &str) -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../examples")
|
||||
.join(name)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_on_fn_type_fires_show_aware_no_instance() {
|
||||
let ws = load_workspace(&fixture("show_no_instance.ail.json")).expect("load");
|
||||
let diags = check_workspace(&ws);
|
||||
|
||||
assert!(
|
||||
!diags.is_empty(),
|
||||
"expected NoInstance Show diagnostic, got no diagnostics"
|
||||
);
|
||||
|
||||
let no_inst = diags
|
||||
.iter()
|
||||
.find(|d| d.code == "no-instance")
|
||||
.unwrap_or_else(|| {
|
||||
panic!("expected diagnostic with code 'no-instance', got: {diags:#?}")
|
||||
});
|
||||
|
||||
// Must mention Show.
|
||||
assert!(
|
||||
no_inst.message.contains("Show"),
|
||||
"expected Show-aware NoInstance diagnostic, got message: {:?}",
|
||||
no_inst.message
|
||||
);
|
||||
|
||||
// Must cross-reference DESIGN.md §"Prelude (built-in) classes"
|
||||
// verbatim — that's the canonical anchor naming which types
|
||||
// ship with Show.
|
||||
assert!(
|
||||
no_inst.message.contains("Prelude (built-in) classes"),
|
||||
"expected DESIGN.md §Prelude (built-in) classes cross-reference, got message: {:?}",
|
||||
no_inst.message
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
//! End-to-end tests for the `print` polymorphic helper shipped in
|
||||
//! milestone 24.3.
|
||||
//!
|
||||
//! Each test compiles a `.ail.json` workspace via the `ail build`
|
||||
//! subcommand, runs the resulting native binary, and asserts on stdout.
|
||||
//!
|
||||
//! Properties protected:
|
||||
//! - `print_primitives_smoke_runs_end_to_end`: `print` at four primitive
|
||||
//! types (Int / Bool / Str / Float) emits the expected textual
|
||||
//! representation of each value via the prelude Show instances +
|
||||
//! `io/print_str`. The four `print__T` mono symbols synthesise from
|
||||
//! the prelude `instance prelude.Show T` bodies and route through the
|
||||
//! corresponding `<T>_to_str` runtime primitive.
|
||||
//! - `print_user_adt_runs_end_to_end`: `print` composes with a
|
||||
//! user-declared `instance prelude.Show IntBox` to emit a user-ADT's
|
||||
//! structural representation. Confirms that the post-mono synthesised
|
||||
//! `print__<UserType>` body's nested `show x` call rewrites to the
|
||||
//! user instance's `show__<UserType>` mono symbol and that the
|
||||
//! cross-module reference from `prelude.print__<UserType>` to
|
||||
//! `<user_module>.show__<UserType>` lowers cleanly through codegen.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
fn fixture_path(name: &str) -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../examples")
|
||||
.join(name)
|
||||
}
|
||||
|
||||
fn build_and_run(fixture: &str) -> String {
|
||||
let src = fixture_path(fixture);
|
||||
let out = std::env::temp_dir().join(format!("ail_{}.bin", fixture.replace('.', "_")));
|
||||
|
||||
let build = Command::new(env!("CARGO_BIN_EXE_ail"))
|
||||
.args(["build", src.to_str().unwrap(), "-o", out.to_str().unwrap()])
|
||||
.output()
|
||||
.expect("ail build");
|
||||
assert!(
|
||||
build.status.success(),
|
||||
"ail build failed for {fixture}:\nstdout: {}\nstderr: {}",
|
||||
String::from_utf8_lossy(&build.stdout),
|
||||
String::from_utf8_lossy(&build.stderr),
|
||||
);
|
||||
|
||||
let run = Command::new(&out).output().expect("run binary");
|
||||
assert!(
|
||||
run.status.success(),
|
||||
"binary exited non-zero:\nstdout: {}\nstderr: {}",
|
||||
String::from_utf8_lossy(&run.stdout),
|
||||
String::from_utf8_lossy(&run.stderr),
|
||||
);
|
||||
String::from_utf8_lossy(&run.stdout).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_primitives_smoke_runs_end_to_end() {
|
||||
let stdout = build_and_run("show_print_smoke.ail.json");
|
||||
// print 42 → "42\n" (int_to_str + puts)
|
||||
// print true → "true\n" (bool_to_str + puts)
|
||||
// print "hello" → "hello\n" (str_clone + puts)
|
||||
// print 3.14 → "3.14\n" (float_to_str / libc %g + puts)
|
||||
// puts emits each Str + a trailing newline; trimmed output is
|
||||
// "42\ntrue\nhello\n3.14".
|
||||
assert_eq!(stdout, "42\ntrue\nhello\n3.14", "got: {stdout:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_user_adt_runs_end_to_end() {
|
||||
let stdout = build_and_run("show_user_adt.ail.json");
|
||||
// print (MkIntBox 7) → "7\n" (via user instance prelude.Show
|
||||
// IntBox that unwraps via match + int_to_str).
|
||||
assert_eq!(stdout, "7", "got: {stdout:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user