mirror of
https://github.com/zeek/zeek.git
synced 2025-10-17 14:08:20 +00:00
BIT-1367: improve coercion of anonymous records in set constructor.
Error messages for set constructors that fail the type check may also be more verbose than before and point out specifically the suspect types.
This commit is contained in:
parent
f607d6aa30
commit
57501c6069
3 changed files with 51 additions and 0 deletions
33
src/Expr.cc
33
src/Expr.cc
|
@ -2599,6 +2599,39 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
|
|||
|
||||
if ( ! same_type(op1->Type(), op2->Type()) )
|
||||
{
|
||||
if ( bt1 == TYPE_TABLE && bt2 == TYPE_TABLE )
|
||||
{
|
||||
if ( op2->Tag() == EXPR_SET_CONSTRUCTOR )
|
||||
{
|
||||
// Some elements in constructor list must not match, see if
|
||||
// we can create a new constructor now that the expected type
|
||||
// of LHS is known and let it do coercions where possible.
|
||||
SetConstructorExpr* sce = dynamic_cast<SetConstructorExpr*>(op2);
|
||||
ListExpr* ctor_list = dynamic_cast<ListExpr*>(sce->Op());
|
||||
attr_list* attr_copy = 0;
|
||||
|
||||
if ( sce->Attrs() )
|
||||
{
|
||||
attr_list* a = sce->Attrs()->Attrs();
|
||||
attrs = new attr_list;
|
||||
loop_over_list(*a, i)
|
||||
attrs->append((*a)[i]);
|
||||
}
|
||||
|
||||
int errors_before = reporter->Errors();
|
||||
op2 = new SetConstructorExpr(ctor_list, attr_copy, op1->Type());
|
||||
int errors_after = reporter->Errors();
|
||||
|
||||
if ( errors_after > errors_before )
|
||||
{
|
||||
ExprError("type clash in assignment");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ExprError("type clash in assignment");
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -18,4 +18,7 @@ error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-che
|
|||
error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38: arithmetic mixed with non-arithmetic (port and 1002)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38 and port: type mismatch (1002 and port)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38: inconsistent type in set constructor (set(1002))
|
||||
error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 44: arithmetic mixed with non-arithmetic (port and 1003)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 44 and port: type mismatch (1003 and port)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 44: inconsistent type in set constructor (set(1003))
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 44: type clash in assignment (lea = set(1003))
|
||||
|
|
|
@ -43,3 +43,18 @@ event bro_init()
|
|||
{
|
||||
local lea: MySet = set(1003); # type clash
|
||||
}
|
||||
|
||||
type MyRecord: record {
|
||||
user: string;
|
||||
host: string;
|
||||
host_port: count &default=22;
|
||||
path: string;
|
||||
};
|
||||
|
||||
global set_of_records: set[MyRecord];
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
# Set ctor w/ anonymous record ctor should coerce.
|
||||
set_of_records = set([$user="testuser", $host="testhost", $path="testpath"]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue