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.
This commit is contained in:
Jon Siwek 2019-10-25 12:48:52 -07:00
parent 691fd5c9a4
commit 34bf78984b
3 changed files with 23 additions and 2 deletions

View file

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

View file

@ -20,3 +20,6 @@
0
[a=13, c=13, v=[test]]
1
[a={
[one] = 1
}]

View file

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