Phase 6A: .aji layer 2 (structs, enums, exhaustive match)

Owner rules: struct/enum declarations in Rust form; one construction
form each (named struct literal, Enum::Variant); struct fields lowered
in declaration order, so field order in the source does not change the
AIR (golden test with two orders, one AIR); match over enums exhaustive
with no '_' (E7004 lists missing variants, E7006 for '_'), integer
match needs '_'; payload names scoped to their arm (enum.switch block
parameters); match as an expression with a merge block; arm-level
source map; no destructuring, guards or nested patterns. Type inference
limits written into spec 15 (one function, monomorphic, no search,
E7007). aji lower --stats: source/AIR size and expansion ratio.

Tests: 4 run (one shared golden), 7 rejects incl. verifier-caught E1003.
This commit is contained in:
dev committed 2026-10-05 11:31:56 +03:00
1 parent ba3e9b3719
commit 0fdd04179b
22 files changed
+702 -121

No files matched your search

@@ -0,0 +1,29 @@
// Enum construction with Enum::Variant, exhaustive match as an expression, payloads scoped to their arm.
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.messages
enum Message {
Stop,
Value(u64),
Pair(u64, u64),
}
fn weight(m: Message) -> u64
effects []
{
let w: u64 = match m {
Message::Stop => 0,
Message::Value(x) => x,
Message::Pair(a, b) => a * b,
};
return w;
}
fn main() -> u64
effects []
{
return weight(Message::Stop) + weight(Message::Value(12)) + weight(Message::Pair(5, 6));
}
@@ -0,0 +1,21 @@
// An integer match needs `_`; arms may diverge; variables are carried across the arms.
// expect: run
// result: 111
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.digits
fn main() -> u64
effects []
{
var total: u64 = 0;
for d in 0..4u8 {
match d {
0 => { total = total + 1; }
1 => { total = total + 10; }
2 => { total = total + 100; }
_ => { continue; }
}
}
return total;
}
@@ -0,0 +1,14 @@
// expect: reject E7004
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.nodefault
fn main() -> u64
effects []
{
let d: u8 = 1;
return match d {
0 => 10,
1 => 20,
};
}
@@ -0,0 +1,16 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.missing
struct Point {
x: i64,
y: i64,
}
fn main() -> i64
effects []
{
let p = Point { x: 1 };
return p.x;
}
@@ -0,0 +1,21 @@
// Every variant must be covered; the diagnostic names the missing ones.
// expect: reject E7004
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.partial
enum State {
Ready,
Busy(u64),
Done,
}
fn main() -> u64
effects []
{
let s = State::Busy(3);
return match s {
State::Ready => 0,
State::Busy(n) => n,
};
}
@@ -0,0 +1,21 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.otherenum
enum A {
One,
}
enum B {
One,
}
fn main() -> u8
effects []
{
let a = A::One;
return match a {
B::One => 1,
};
}
@@ -0,0 +1,16 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.unknownfield
struct Point {
x: i64,
y: i64,
}
fn main() -> i64
effects []
{
let p = Point { x: 1, y: 2 };
return p.z;
}
@@ -0,0 +1,16 @@
// The verifier, not the frontend, rejects a struct that contains itself by value (E1003).
// expect: reject E1003
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.recursive
struct Node {
value: u64,
next: Node,
}
fn main() -> u64
effects []
{
return 0;
}
@@ -0,0 +1,20 @@
// `_` is not allowed in a match over an enum: a new variant must not slip through silently.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.wild
enum State {
Ready,
Done,
}
fn main() -> u64
effects []
{
let s = State::Done;
return match s {
State::Ready => 0,
_ => 1,
};
}
@@ -0,0 +1,19 @@
// Fields are lowered in declaration order: this file and struct_order_b.aji write the
// fields in different orders and share one golden AIR.
// expect: run
// result: 3
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.point
struct Point {
x: i64,
y: i64,
}
fn main() -> i64
effects []
{
let p = Point { x: 1, y: 2 };
return p.x + p.y;
}
@@ -0,0 +1,17 @@
module demo.point
air 0
lang 0.1
features [effects]
type @Point = struct { x: i64, y: i64 }
fn @main() -> i64
effects []
entry:
%c_1 = const.i64 1
%c_2 = const.i64 2
%s_3 = struct.make @Point(%c_1, %c_2)
%x_4 = struct.get @Point.x, %s_3
%y_5 = struct.get @Point.y, %s_3
%t_6 = add.checked.i64 %x_4, %y_5
ret %t_6
@@ -0,0 +1,18 @@
// The same program as struct_order_a.aji with the fields written in the other order.
// expect: run
// result: 3
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.point
struct Point {
x: i64,
y: i64,
}
fn main() -> i64
effects []
{
let p = Point { y: 2, x: 1 };
return p.x + p.y;
}
@@ -0,0 +1,17 @@
module demo.point
air 0
lang 0.1
features [effects]
type @Point = struct { x: i64, y: i64 }
fn @main() -> i64
effects []
entry:
%c_1 = const.i64 1
%c_2 = const.i64 2
%s_3 = struct.make @Point(%c_1, %c_2)
%x_4 = struct.get @Point.x, %s_3
%y_5 = struct.get @Point.y, %s_3
%t_6 = add.checked.i64 %x_4, %y_5
ret %t_6