Merge remote-tracking branch 'origin/topic/jsiwek/gh-594-improve-table-init-type-checking'

* origin/topic/jsiwek/gh-594-improve-table-init-type-checking:
  GH-594: Improve table initialization type-check error messages
This commit is contained in:
Tim Wojtulewicz 2020-08-24 14:27:31 -07:00
commit 60443e3178
5 changed files with 54 additions and 1 deletions

View file

@ -1,3 +1,6 @@
3.3.0-dev.184 | 2020-08-24 14:27:31 -0700
* GH-594: Improve table initialization type-check error messages (Jon Siwek, Corelight)
3.3.0-dev.182 | 2020-08-21 13:46:16 -0700

View file

@ -1 +1 @@
3.3.0-dev.182
3.3.0-dev.184

View file

@ -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 )

View file

@ -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.])

View file

@ -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."]
};