Coercion classification now lives in src/ir/conversions.zig behind a *Lowering facade (CoercionResolver), mirroring CallResolver / GenericResolver / ProtocolResolver. Two pure classifiers: - classify(src, dst) -> CoercionPlan (15 kinds: no_op / unbox_any / box_any / closure_to_fn_reject / tuple_elementwise / optional_unwrap / void_to_optional / optional_wrap / erase_protocol / int_to_float / float_to_int / ptr_int_bitcast / widen / narrow / none) — the built-in coercion ladder. - classifyXX(src, dst) -> XXPlan (unbox_any / no_op / erase_protocol / protocol_to_pointer / coerce) — the xx-operator head. coerceToType and lowerXX now `switch (classify…)` then emit; branch order mirrors the originals exactly and every arm reproduces the prior lowering — the f32/f64 Any match dispatch, buildProtocolErasure (lowerXX) vs buildProtocolValue (coerceToType), tuple/optional recursion, and the user-Into fallback + pointer materialization + recursion-guard/diagnostics (which stay in lowerXX / tryUserConversion). IR emission stays entirely in Lowering; the classifiers are pure. lowerXX keeps the operand's lowered Ref type as src_ty. `.none` means no built-in applies (pass through; the Into fallback runs) — no silent default. New pub: isFloat / isIntEx / typeBitsEx / resolveConcreteTypeName (the classifier reads them); coercionResolver() accessor. lower.zig net -54 lines. conversions.test.zig drives CoercionResolver directly: the full classify ladder (no-op, Any box/unbox, widen/narrow, int<->float, ptr<->int, optional wrap/unwrap, void->optional, tuple, closure-reject, .none for two unrelated structs), erase_protocol for a concrete source, and classifyXX (all 5 kinds incl. protocol-to-pointer vs coerce and pointer-materialization -> coerce). zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir churn.
117 lines
5.7 KiB
Zig
117 lines
5.7 KiB
Zig
// Tests for conversions.zig — the coercion-planning classifier
|
|
// (`CoercionResolver`). Reached via `ir.CoercionResolver{ .l = &lowering }`,
|
|
// mirroring the other facade tests. These pin the `classify` / `classifyXX`
|
|
// DECISIONS; `coerceToType` / `lowerXX` emit them (emission stays in Lowering).
|
|
|
|
const std = @import("std");
|
|
const ast = @import("../ast.zig");
|
|
const Node = ast.Node;
|
|
|
|
const ir_mod = @import("ir.zig");
|
|
const TypeId = ir_mod.TypeId;
|
|
const Lowering = ir_mod.Lowering;
|
|
const CoercionResolver = ir_mod.CoercionResolver;
|
|
const Plan = CoercionResolver.CoercionPlan;
|
|
const XXPlan = CoercionResolver.XXPlan;
|
|
|
|
fn protoMethodReq(name: []const u8) ast.ProtocolMethodDecl {
|
|
return .{ .name = name, .params = &.{}, .param_names = &.{}, .return_type = null, .default_body = null };
|
|
}
|
|
|
|
test "conversions: classify covers the built-in coercion ladder" {
|
|
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer arena.deinit();
|
|
const alloc = arena.allocator();
|
|
var module = ir_mod.Module.init(alloc);
|
|
defer module.deinit();
|
|
var l = Lowering.init(&module);
|
|
const cr = CoercionResolver{ .l = &l };
|
|
const tt = &module.types;
|
|
|
|
// no-op + Any box/unbox.
|
|
try std.testing.expectEqual(Plan.no_op, cr.classify(.s64, .s64));
|
|
try std.testing.expectEqual(Plan.unbox_any, cr.classify(.any, .s64));
|
|
try std.testing.expectEqual(Plan.box_any, cr.classify(.s64, .any));
|
|
|
|
// Numeric / pointer ladder.
|
|
try std.testing.expectEqual(Plan.widen, cr.classify(.s32, .s64));
|
|
try std.testing.expectEqual(Plan.narrow, cr.classify(.s64, .s32));
|
|
try std.testing.expectEqual(Plan.int_to_float, cr.classify(.s32, .f64));
|
|
try std.testing.expectEqual(Plan.float_to_int, cr.classify(.f64, .s32));
|
|
const ptr_s64 = tt.ptrTo(.s64);
|
|
try std.testing.expectEqual(Plan.ptr_int_bitcast, cr.classify(ptr_s64, .s64));
|
|
try std.testing.expectEqual(Plan.ptr_int_bitcast, cr.classify(.s64, ptr_s64));
|
|
|
|
// Optional wrap / unwrap, and void → optional.
|
|
const opt_s64 = tt.optionalOf(.s64);
|
|
try std.testing.expectEqual(Plan.optional_wrap, cr.classify(.s64, opt_s64));
|
|
try std.testing.expectEqual(Plan.optional_unwrap, cr.classify(opt_s64, .s64));
|
|
try std.testing.expectEqual(Plan.void_to_optional, cr.classify(.void, opt_s64));
|
|
|
|
// Tuple → tuple, same arity.
|
|
const t_ss = tt.intern(.{ .tuple = .{ .fields = &[_]TypeId{ .s64, .s64 }, .names = null } });
|
|
const t_ii = tt.intern(.{ .tuple = .{ .fields = &[_]TypeId{ .s32, .s32 }, .names = null } });
|
|
try std.testing.expectEqual(Plan.tuple_elementwise, cr.classify(t_ss, t_ii));
|
|
|
|
// Closure value → bare fn-ptr: rejected.
|
|
const clo = tt.closureType(&.{}, .void);
|
|
const fnp = tt.functionType(&.{}, .void);
|
|
try std.testing.expectEqual(Plan.closure_to_fn_reject, cr.classify(clo, fnp));
|
|
|
|
// Two unrelated structs: no built-in applies → `.none` (lowerXX then tries
|
|
// a user `Into`). No silent numeric default.
|
|
const a = tt.intern(.{ .@"struct" = .{ .name = tt.internString("A"), .fields = &.{} } });
|
|
const b = tt.intern(.{ .@"struct" = .{ .name = tt.internString("B"), .fields = &.{} } });
|
|
try std.testing.expectEqual(Plan.none, cr.classify(a, b));
|
|
}
|
|
|
|
test "conversions: classify selects protocol erasure for a concrete source" {
|
|
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer arena.deinit();
|
|
const alloc = arena.allocator();
|
|
var module = ir_mod.Module.init(alloc);
|
|
defer module.deinit();
|
|
var l = Lowering.init(&module);
|
|
const cr = CoercionResolver{ .l = &l };
|
|
|
|
const methods = [_]ast.ProtocolMethodDecl{protoMethodReq("draw")};
|
|
const pd = ast.ProtocolDecl{ .name = "Drawable", .methods = &methods };
|
|
l.registerProtocolDecl(&pd);
|
|
const drawable = module.types.findByName(module.types.internString("Drawable")).?;
|
|
const circle = module.types.intern(.{ .@"struct" = .{ .name = module.types.internString("Circle"), .fields = &.{} } });
|
|
|
|
// Concrete struct → protocol value: erasure.
|
|
try std.testing.expectEqual(Plan.erase_protocol, cr.classify(circle, drawable));
|
|
}
|
|
|
|
test "conversions: classifyXX picks the xx-operator head decision" {
|
|
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer arena.deinit();
|
|
const alloc = arena.allocator();
|
|
var module = ir_mod.Module.init(alloc);
|
|
defer module.deinit();
|
|
var l = Lowering.init(&module);
|
|
const cr = CoercionResolver{ .l = &l };
|
|
const tt = &module.types;
|
|
|
|
const methods = [_]ast.ProtocolMethodDecl{protoMethodReq("draw")};
|
|
const pd = ast.ProtocolDecl{ .name = "Drawable", .methods = &methods };
|
|
l.registerProtocolDecl(&pd);
|
|
const drawable = tt.findByName(tt.internString("Drawable")).?;
|
|
|
|
// Any source unboxes regardless of dst.
|
|
try std.testing.expectEqual(XXPlan.unbox_any, cr.classifyXX(.any, .s64));
|
|
// Same type → no-op.
|
|
try std.testing.expectEqual(XXPlan.no_op, cr.classifyXX(.s64, .s64));
|
|
// dst is a protocol → erasure (checked before the src-protocol case).
|
|
try std.testing.expectEqual(XXPlan.erase_protocol, cr.classifyXX(.s64, drawable));
|
|
// src is a protocol, dst is a pointer → recover the ctx pointer.
|
|
try std.testing.expectEqual(XXPlan.protocol_to_pointer, cr.classifyXX(drawable, tt.ptrTo(.s64)));
|
|
// src is a protocol but dst is NOT a pointer → fall to the ladder.
|
|
try std.testing.expectEqual(XXPlan.coerce, cr.classifyXX(drawable, .s64));
|
|
// Pointer materialization (`xx value` into a `*T` slot, no built-in) defers
|
|
// to the ladder + the user-`Into` pointer fallback in lowerXX.
|
|
const a = tt.intern(.{ .@"struct" = .{ .name = tt.internString("A"), .fields = &.{} } });
|
|
try std.testing.expectEqual(XXPlan.coerce, cr.classifyXX(a, tt.ptrTo(.s32)));
|
|
}
|