mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
GH-594: Improve table initialization type-check error messages
This commit is contained in:
parent
5082b60c1f
commit
164b1f6514
3 changed files with 50 additions and 0 deletions
22
src/Expr.cc
22
src/Expr.cc
|
@ -2433,6 +2433,28 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
|
||||||
const auto& yt = tv->GetType()->Yield();
|
const auto& yt = tv->GetType()->Yield();
|
||||||
|
|
||||||
auto index = op1->InitVal(tt->GetIndices().get(), nullptr);
|
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);
|
auto v = op2->InitVal(yt.get(), nullptr);
|
||||||
|
|
||||||
if ( ! index || ! v )
|
if ( ! index || ! v )
|
||||||
|
|
|
@ -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.])
|
26
testing/btest/language/table-aggr-init-type-check.zeek
Normal file
26
testing/btest/language/table-aggr-init-type-check.zeek
Normal 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."]
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue