diff --git a/src/Expr.cc b/src/Expr.cc index 772fb731fb..43bd42f336 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2433,6 +2433,28 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const const auto& yt = tv->GetType()->Yield(); auto index = op1->InitVal(tt->GetIndices().get(), nullptr); + + if ( ! same_type(*yt, *op2->GetType(), true) ) + { + if ( yt->Tag() == TYPE_RECORD && op2->GetType()->Tag() == TYPE_RECORD ) + { + if ( ! record_promotion_compatible(yt->AsRecordType(), + op2->GetType()->AsRecordType()) ) + { + Error("type mismatch in table value initialization: " + "incompatible record types"); + return nullptr; + } + } + else + { + Error(fmt("type mismatch in table value initialization: " + "assigning '%s' to table with values of type '%s'", + type_name(op2->GetType()->Tag()), type_name(yt->Tag()))); + return nullptr; + } + } + auto v = op2->InitVal(yt.get(), nullptr); if ( ! index || ! v ) diff --git a/testing/btest/Baseline/language.table-aggr-init-type-check/output b/testing/btest/Baseline/language.table-aggr-init-type-check/output new file mode 100644 index 0000000000..f431041b10 --- /dev/null +++ b/testing/btest/Baseline/language.table-aggr-init-type-check/output @@ -0,0 +1,2 @@ +error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.table-aggr-init-type-check/table-aggr-init-type-check.zeek, line 21: type mismatch in table value initialization: assigning 'types' to table with values of type 'record' (three = 1, 2) +error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.table-aggr-init-type-check/table-aggr-init-type-check.zeek, line 25: type mismatch in table value initialization: incompatible record types (four = [$b=No.]) diff --git a/testing/btest/language/table-aggr-init-type-check.zeek b/testing/btest/language/table-aggr-init-type-check.zeek new file mode 100644 index 0000000000..cb87bc2071 --- /dev/null +++ b/testing/btest/language/table-aggr-init-type-check.zeek @@ -0,0 +1,26 @@ +# @TEST-EXEC-FAIL: zeek -b %INPUT >output 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output + +type MyRec: record { + b: count; + c: count; + d: count &optional; +}; + +const testtable: table[string] of MyRec = table() &redef; + +redef testtable += { + ["one"] = [$b=1, $c=2] +}; + +redef testtable += { + ["two"] = [$b=1, $c=2, $d=3] +}; + +redef testtable += { + ["three"] = [1, 2] +}; + +redef testtable += { + ["four"] = [$b="No."] +};