bug fix for reporting poorly formed record constructors

This commit is contained in:
Vern Paxson 2021-12-22 14:26:03 -08:00
parent 1e60264548
commit 4f566f35ee
3 changed files with 25 additions and 1 deletions

View file

@ -3307,11 +3307,15 @@ RecordConstructorExpr::RecordConstructorExpr(ListExprPtr constructor_list)
const ExprPList& exprs = op->AsListExpr()->Exprs(); const ExprPList& exprs = op->AsListExpr()->Exprs();
type_decl_list* record_types = new type_decl_list(exprs.length()); type_decl_list* record_types = new type_decl_list(exprs.length());
const Expr* constructor_error_expr = nullptr;
for ( const auto& e : exprs ) for ( const auto& e : exprs )
{ {
if ( e->Tag() != EXPR_FIELD_ASSIGN ) if ( e->Tag() != EXPR_FIELD_ASSIGN )
{ {
Error("bad type in record constructor", e); // Don't generate the error yet, as reporting it
// requires that we have a well-formed type.
constructor_error_expr = e;
SetError(); SetError();
continue; continue;
} }
@ -3323,6 +3327,9 @@ RecordConstructorExpr::RecordConstructorExpr(ListExprPtr constructor_list)
} }
SetType(make_intrusive<RecordType>(record_types)); SetType(make_intrusive<RecordType>(record_types));
if ( constructor_error_expr )
Error("bad type in record constructor", constructor_error_expr);
} }
RecordConstructorExpr::RecordConstructorExpr(RecordTypePtr known_rt, ListExprPtr constructor_list) RecordConstructorExpr::RecordConstructorExpr(RecordTypePtr known_rt, ListExprPtr constructor_list)

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/record-bad-ctor3.zeek, line 13: bad type in record constructor ([$x=a + 5, a + 9] and a + 9)

View file

@ -0,0 +1,15 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# Every element in a record ctor's expression list should have an assignment
# form. Make sure we correctly report errors when that's not the case.
global a = 3;
type r: record { x: count; y: count; };
event zeek_init()
{
local b: r = record($x = a + 5, a + 9);
print b;
}