revised fix

This commit is contained in:
Vern Paxson 2025-09-27 22:20:10 -07:00
parent 2efa5cd075
commit 70065631a7
2 changed files with 19 additions and 4 deletions

View file

@ -351,21 +351,28 @@ void add_type(ID* id, TypePtr t, std::unique_ptr<std::vector<AttrPtr>> attr) {
if ( const auto& old_t = id->GetType() ) { if ( const auto& old_t = id->GetType() ) {
// The identifier already has a type associated with it. This can // The identifier already has a type associated with it. This can
// be okay if (1) it's already been marked as a Type identifier, // be okay if (1) it's already been marked as a Type identifier,
// (2) the previous type is a stub. // (2) (2) the previous type is a stub, or an equivalent enum.
if ( ! id->IsType() ) { if ( ! id->IsType() ) {
reporter->Error("Identifier %s has already been declared and is not a type", id->Name()); reporter->Error("Identifier %s has already been declared and is not a type", id->Name());
return; return;
} }
if ( old_t->Tag() == t->Tag() && if ( old_t->Tag() == t->Tag() && ((old_t->Tag() == TYPE_RECORD && old_t->AsRecordType()->NumFields() == 0) ||
((old_t->Tag() == TYPE_RECORD && old_t->AsRecordType()->NumFields() == 0) || t->Tag() == TYPE_ENUM) ) (t->Tag() == TYPE_ENUM && same_type(t, old_t))) )
// It has a consistent tag and is either redeclaring a stub // It has a consistent tag and is either redeclaring a stub
// record (used in init-bare.zeek) or an enum (which can // record (used in init-bare.zeek) or an enum (which can
// appear due to specifiers in BiFs, for example). // appear due to specifiers in BiFs, for example).
; ;
else { else {
reporter->Error("Type %s has already been declared", id->Name()); std::string loc;
auto li = id->GetLocationInfo();
auto fn = li->FileName();
int ln = li->FirstLine();
if ( fn && fn[0] != '\0' )
loc = " at " + std::string(fn) + ":" + std::to_string(ln);
reporter->Error("Type %s has already been declared%s", id->Name(), loc.c_str());
return; return;
} }
} }

View file

@ -59,3 +59,11 @@ global f: function();
global f = function() { }; global f = function() { };
global f: hook(); global f: hook();
global f: event(); global f: event();
# @TEST-START-NEXT
global f = function() { };
type f: bool;
# @TEST-START-NEXT
type f: record {};
type f: bool;