GH-1296: fix type-checks related to list-type equality

List-types as used in composite table/set indices, for example,
previously had incorrect same_type() comparisons due to flattening
of the list-type into a single type without checking whether the
number and kind of types all match.

This patch simply removes the flatten_type() call from same_type() since
it was already contradicting/preventing a subsequent full-comparison
between elements of two TYPE_LISTs.

There was also a superfluous special-case of the `in` operator's
type-checking for testing whether a record is in a table/set.  It's
superfluous because the general case will already do the type-checking
from MatchesIndex() after first wrapping the record operand in a
ListExpr.  The previous logic was incorrectly relying on the
flatten_type() for testing equality of a record-type against a
list-type, whereas the general case correctly normalizes to testing
equality of two list-types.

The special-cased type-checking logic for assigning a record value to a
table index during its initialization similarly needed minor
re-organization in order to maintain the same error messages as before.
This commit is contained in:
Jon Siwek 2021-01-10 17:32:50 -08:00
parent 54b52eb197
commit 8c64ba6907
6 changed files with 88 additions and 38 deletions

View file

@ -2488,19 +2488,28 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
auto index = op1->InitVal(tt->GetIndices().get(), nullptr);
if ( ! same_type(*yt, *op2->GetType(), true) )
if ( yt->Tag() == TYPE_RECORD )
{
if ( yt->Tag() == TYPE_RECORD && op2->GetType()->Tag() == TYPE_RECORD )
if ( 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;
}
Error(util::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;
}
else
if ( ! same_type(*yt, *op2->GetType()) &&
! record_promotion_compatible(yt->AsRecordType(),
op2->GetType()->AsRecordType()) )
{
Error("type mismatch in table value initialization: "
"incompatible record types");
return nullptr;
}
}
else
{
if ( ! same_type(*yt, *op2->GetType(), true) )
{
Error(util::fmt("type mismatch in table value initialization: "
"assigning '%s' to table with values of type '%s'",
@ -4086,29 +4095,6 @@ InExpr::InExpr(ExprPtr arg_op1, ExprPtr arg_op2)
SetType(base_type(TYPE_BOOL));
}
else if ( op1->GetType()->Tag() == TYPE_RECORD )
{
if ( op2->GetType()->Tag() != TYPE_TABLE )
{
op2->GetType()->Error("table/set required");
SetError();
}
else
{
const auto& t1 = op1->GetType();
const auto& it = op2->GetType()->AsTableType()->GetIndices();
if ( ! same_type(t1, it) )
{
t1->Error("indexing mismatch", op2->GetType().get());
SetError();
}
else
SetType(base_type(TYPE_BOOL));
}
}
else if ( op1->GetType()->Tag() == TYPE_STRING &&
op2->GetType()->Tag() == TYPE_STRING )
SetType(base_type(TYPE_BOOL));