From 2efa5cd07564a5d4c04474022a568a457eed0438 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 27 Sep 2025 16:47:11 -0700 Subject: [PATCH] importing fix for redeclaring variables --- src/Var.cc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Var.cc b/src/Var.cc index f719a4f2ec..14e108c31b 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -348,6 +348,28 @@ extern ExprPtr add_and_assign_local(IDPtr id, ExprPtr init, ValPtr val) { } void add_type(ID* id, TypePtr t, std::unique_ptr> attr) { + if ( const auto& old_t = id->GetType() ) { + // The identifier already has a type associated with it. This can + // be okay if (1) it's already been marked as a Type identifier, + // (2) the previous type is a stub. + if ( ! id->IsType() ) { + reporter->Error("Identifier %s has already been declared and is not a type", id->Name()); + return; + } + + if ( old_t->Tag() == t->Tag() && + ((old_t->Tag() == TYPE_RECORD && old_t->AsRecordType()->NumFields() == 0) || t->Tag() == TYPE_ENUM) ) + // It has a consistent tag and is either redeclaring a stub + // record (used in init-bare.zeek) or an enum (which can + // appear due to specifiers in BiFs, for example). + ; + + else { + reporter->Error("Type %s has already been declared", id->Name()); + return; + } + } + std::string new_type_name = id->Name(); std::string old_type_name = t->GetName();