Attr: Deprecate using &default and &optional together on record fields

If &default implies re-initialization of the field, using them together
doesn't make much sense.
This commit is contained in:
Arne Welzel 2025-07-27 15:56:00 +02:00
parent 23181e4811
commit 473723cc47
6 changed files with 43 additions and 1 deletions

4
NEWS
View file

@ -365,6 +365,10 @@ Deprecated Functionality
The replacement script doesn't populate the ``email_body_sections`` anymore either. The replacement script doesn't populate the ``email_body_sections`` anymore either.
- Using ``&default`` and ``&optional`` together on a record field has been deprecated
as it would only result in ``&default`` behavior. This will become an error starting
with Zeek 8.1.
- The ``zeek::Event()`` constructor was deprecated. Use ``event_mgr::Enqueue()`` - The ``zeek::Event()`` constructor was deprecated. Use ``event_mgr::Enqueue()``
or ``event_mgr::Dispatch()`` instead. or ``event_mgr::Dispatch()`` instead.

View file

@ -52,7 +52,7 @@ export {
## The peer that originated this weird. This is helpful in ## The peer that originated this weird. This is helpful in
## cluster deployments if a particular cluster node is having ## cluster deployments if a particular cluster node is having
## trouble to help identify which node is having trouble. ## trouble to help identify which node is having trouble.
peer: string &log &optional &default=peer_description; peer: string &log &default=peer_description;
## The source of the weird. When reported by an analyzer, this ## The source of the weird. When reported by an analyzer, this
## should be the name of the analyzer. ## should be the name of the analyzer.

View file

@ -5,6 +5,7 @@
#include "zeek/Desc.h" #include "zeek/Desc.h"
#include "zeek/Expr.h" #include "zeek/Expr.h"
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/Reporter.h"
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/input/Manager.h" #include "zeek/input/Manager.h"
#include "zeek/threading/SerialTypes.h" #include "zeek/threading/SerialTypes.h"
@ -298,6 +299,12 @@ bool Attributes::CheckAttr(Attr* a) {
case ATTR_OPTIONAL: case ATTR_OPTIONAL:
if ( global_var ) if ( global_var )
return AttrError("&optional is not valid for global variables"); return AttrError("&optional is not valid for global variables");
// Remove in v8.1: Call AttrError()
if ( in_record && Find(ATTR_DEFAULT) )
zeek::reporter->Deprecation(
"Remove in v8.1: Using &default and &optional together results in &default behavior");
break; break;
case ATTR_ADD_FUNC: case ATTR_ADD_FUNC:
@ -335,6 +342,11 @@ bool Attributes::CheckAttr(Attr* a) {
if ( Find(ATTR_DEFAULT_INSERT) ) if ( Find(ATTR_DEFAULT_INSERT) )
return AttrError("&default and &default_insert cannot be used together"); return AttrError("&default and &default_insert cannot be used together");
// Remove in v8.1: Call AttrError()
if ( in_record && Find(ATTR_OPTIONAL) )
zeek::reporter->Deprecation(
"Remove in v8.1: Using &default and &optional together results in &default behavior");
std::string err_msg; std::string err_msg;
if ( ! check_default_attr(a, type, global_var, in_record, err_msg) && ! err_msg.empty() ) if ( ! check_default_attr(a, type, global_var, in_record, err_msg) && ! err_msg.empty() )
return AttrError(err_msg.c_str()); return AttrError(err_msg.c_str());

View file

@ -0,0 +1,6 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in <...>/optional-and-default-field.zeek, line 10: Remove in v8.1: Using &default and &optional together results in &default behavior
warning in <...>/optional-and-default-field.zeek, line 11: Remove in v8.1: Using &default and &optional together results in &default behavior
warning in <...>/optional-and-default-field.zeek, line 12: Remove in v8.1: Using &default and &optional together results in &default behavior
warning in <...>/optional-and-default-field.zeek, line 13: Remove in v8.1: Using &default and &optional together results in &default behavior
warning in <...>/optional-and-default-field.zeek, line 14: Remove in v8.1: Using &default and &optional together results in &default behavior

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.
[c=5, i=-5, v=[], r0=[], r1=[]]

View file

@ -0,0 +1,18 @@
# @TEST-DOC: Warn on record fields that have both, &optional and &default
#
# @TEST-EXEC: zeek -b %INPUT
# @TEST-EXEC: btest-diff .stdout
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
type R: record { };
type X: record {
c: count &optional &default=5;
i: int &default=-5 &optional;
v: vector of string &optional &default=vector();
r0: R &optional &default=R();
r1: R &default=R() &optional;
};
global x = X();
print x;