From 0be1a54c2d7500a222ffae5f6ab38842a30cf1c4 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 29 May 2018 06:47:55 -0700 Subject: structs: slot access on non-struct: improve diagnosis. * struct.c (struct_handle_for_slot): New static function. (slot, maybe_slot, slotset): Use struct_handle_for_slot rather than struct_handle, for improved error message that reveals what slot name was requested. --- struct.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/struct.c b/struct.c index 7ee55a35..f55c28f9 100644 --- a/struct.c +++ b/struct.c @@ -660,6 +660,14 @@ static struct struct_inst *struct_handle(val obj, val ctx) ctx, obj, nao); } +static struct struct_inst *struct_handle_for_slot(val obj, val ctx, val slot) +{ + if (cobjp(obj) && obj->co.ops == &struct_inst_ops) + return coerce(struct struct_inst *, obj->co.handle); + uw_throwf(error_s, lit("~a: attempt to access slot ~s of non-structure ~s"), + ctx, slot, obj, nao); +} + val copy_struct(val strct) { const val self = lit("copy-struct"); @@ -949,7 +957,7 @@ static noreturn void no_such_static_slot(val ctx, val type, val slot) val slot(val strct, val sym) { const val self = lit("slot"); - struct struct_inst *si = struct_handle(strct, self); + struct struct_inst *si = struct_handle_for_slot(strct, self, sym); if (sym && symbolp(sym)) { loc ptr = lookup_slot_load(strct, si, sym); @@ -963,7 +971,7 @@ val slot(val strct, val sym) val maybe_slot(val strct, val sym) { const val self = lit("slot"); - struct struct_inst *si = struct_handle(strct, self); + struct struct_inst *si = struct_handle_for_slot(strct, self, sym); if (sym && symbolp(sym)) { loc ptr = lookup_slot_load(strct, si, sym); @@ -977,7 +985,7 @@ val maybe_slot(val strct, val sym) val slotset(val strct, val sym, val newval) { const val self = lit("slotset"); - struct struct_inst *si = struct_handle(strct, self); + struct struct_inst *si = struct_handle_for_slot(strct, self, sym); if (sym && symbolp(sym)) { loc ptr = lookup_slot(strct, si, sym); -- cgit v1.2.3