GH-2920: Don't warn on uninitialized container options

This commit is contained in:
Tim Wojtulewicz 2023-04-03 15:52:49 -07:00
parent c5ce82143a
commit fcf7af259c
6 changed files with 46 additions and 2 deletions

View file

@ -1024,7 +1024,7 @@ inline bool IsString(TypeTag t)
return (t == TYPE_STRING);
}
// True if the given type is a container aggregate.
// True if the given type is an aggregate.
inline bool IsAggr(TypeTag tag)
{
return tag == TYPE_VECTOR || tag == TYPE_TABLE || tag == TYPE_RECORD;
@ -1038,6 +1038,12 @@ inline bool IsAggr(const TypePtr& t)
return IsAggr(t->Tag());
}
// True if the given type is a container.
inline bool IsContainer(TypeTag tag)
{
return tag == TYPE_VECTOR || tag == TYPE_TABLE;
}
// True if the given type tag corresponds to the error type.
inline bool IsErrorType(TypeTag t)
{

View file

@ -381,7 +381,7 @@ static void make_var(const IDPtr& id, TypePtr t, InitClass c, ExprPtr init,
if ( dt == VAR_OPTION )
{
if ( ! init )
if ( ! init && ! IsContainer(t->Tag()) )
id->Error("option variable must be initialized");
id->SetOption();

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
0
0
0

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,14 @@
# Ensures that an error doesn't print out for option variables
# that are containers. These get automatically initialized so
# there's no need to manually initialize them.
# @TEST-EXEC: zeek -b %INPUT > out
# @TEST-EXEC: btest-diff out
option foo: set[count] &redef;
option foo2: table[count] of count &redef;
option foo3: vector of count &redef;
print |foo|;
print |foo2|;
print |foo3|;

View file

@ -0,0 +1,19 @@
# Ensures that an error is printed out for option variables
# that are containers if they aren't initialized.
# @TEST-EXEC-FAIL: zeek -b %INPUT > out
# @TEST-EXEC: btest-diff out
@load misc/stats
type TestRecord: record {
a: count;
b: Stats::Info &optional;
};
option foo: TestRecord &redef;
event zeek_init()
{
print foo;
}