From 34bf78984b9dc3567e4974705945ef065cf07763 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Fri, 25 Oct 2019 12:48:52 -0700 Subject: [PATCH] GH-654: allow table() in record &default expressions Table fields of records previously did not coerce unspecified tables used in their &default attribute to the correct type. --- src/Attr.cc | 14 ++++++++++++-- .../Baseline/language.record-default-coercion/out | 3 +++ .../btest/language/record-default-coercion.zeek | 8 ++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Attr.cc b/src/Attr.cc index c6cf036642..d63f72a3fd 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -372,11 +372,21 @@ void Attributes::CheckAttr(Attr* a) { // &default applies to record field. - if ( same_type(atype, type) || - (atype->Tag() == TYPE_TABLE && atype->AsTableType()->IsUnspecifiedTable()) ) + if ( same_type(atype, type) ) // Ok. break; + if ( (atype->Tag() == TYPE_TABLE && atype->AsTableType()->IsUnspecifiedTable()) ) + { + Expr* e = a->AttrExpr(); + + if ( check_and_promote_expr(e, type) ) + { + a->SetAttrExpr(e); + break; + } + } + // Table defaults may be promotable. if ( ytype && ytype->Tag() == TYPE_RECORD && atype->Tag() == TYPE_RECORD && diff --git a/testing/btest/Baseline/language.record-default-coercion/out b/testing/btest/Baseline/language.record-default-coercion/out index bf76ba5033..0e9619bbd4 100644 --- a/testing/btest/Baseline/language.record-default-coercion/out +++ b/testing/btest/Baseline/language.record-default-coercion/out @@ -20,3 +20,6 @@ 0 [a=13, c=13, v=[test]] 1 +[a={ +[one] = 1 +}] diff --git a/testing/btest/language/record-default-coercion.zeek b/testing/btest/language/record-default-coercion.zeek index 83e48044a3..231edc7b60 100644 --- a/testing/btest/language/record-default-coercion.zeek +++ b/testing/btest/language/record-default-coercion.zeek @@ -17,6 +17,10 @@ type Bar: record { foo: Foo &default=[$foo=1234]; }; +type Qux: record { + a: table[string] of string &default=table(); +}; + function print_bar(b: Bar) { print b; @@ -46,3 +50,7 @@ print |r$v|; r$v += "test"; print r; print |r$v|; + +local q = Qux(); +q$a["one"] = "1"; +print q;