# HG changeset patch # User Westerbly Snaydley # Date 1571592813 18000 # Sun Oct 20 12:33:33 2019 -0500 # Branch error_on_duplicate_tag # Node ID 3b596fa4547363c8149351ba558567b89318726c # Parent 380ae5fbda0cdc36cac03df21f0c9e3b5f7b8bbc enum: refactored duplicated field checking to a single function and added type name to error message to make it easier to find the mistake. diff --git a/lib/scopes/enum.sc b/lib/scopes/enum.sc --- a/lib/scopes/enum.sc +++ b/lib/scopes/enum.sc @@ -179,6 +179,19 @@ elseif (index <= 0xffffffff) 32 else 64 _ (max width iwidth) signed + + inline check-field-redefinition (name finalized-fieldT) + # if field has already been seen, its "last type" is finalized-fieldT + # ie. this checks if we defined the same tag twice. + let previously-definedT = + try + 'typeof ('@ T (name as Symbol)) + except (ex) + return; + if (previously-definedT == finalized-fieldT) + hide-traceback; + error (.. "Duplicate enum fields aren't allowed: " (repr (name as Symbol)) ":" (repr T)) + let classic? = if (T < CEnum) true elseif (T < Enum) false @@ -210,15 +223,7 @@ if (field-type != Nothing) error "plain enums can't have tagged fields" let value = (sc_const_int_new T index) - # if the value bound to this symbol has already been seen, its type is the enum type. - let previously-definedT = - try - ('typeof ('@ T (name as Symbol))) - except (ex) - Nothing - if (previously-definedT == T) - hide-traceback; - error ("Duplicate enum fields aren't allowed: " .. (name as Symbol as string)) + check-field-redefinition name T 'set-symbol T name value 'bind using-scope name value # build repr function @@ -277,24 +282,12 @@ sc_template_set_name constructor name constructor - # if field has already been seen, its "last type" is `Unknown` - # ie. this checks if we defined the same tag twice. - let previously-definedT = - try - 'typeof ('@ T (name as Symbol)) - except (ex) - # if it's a compile time tag, this will do. - Nothing - if (previously-definedT == Unknown) - hide-traceback; - error ("Duplicate enum fields aren't allowed: " .. (name as Symbol as string)) - + check-field-redefinition name Unknown 'set-symbol T name value 'bind using-scope name value spice finalize-enum (T storage) - if ('constant? T) finalize-enum-runtime (T as type) storage `()