Stmt: Error on deprecated when/local usage

This has been around since Zeek v4.1, so it was warned about in Zeek 5.0
LTS and we could've removed it with 5.1.

Also removed merge_top_scope() from the zeek::detail namespace, it's
unused now.

Updated the when-aggregates test somehow. I'm not quite sure what had
been tested there :-/
This commit is contained in:
Arne Welzel 2023-03-17 14:52:28 +01:00
parent 5ef62b2de8
commit bb80d80218
9 changed files with 18 additions and 36 deletions

View file

@ -190,20 +190,6 @@ ScopePtr pop_scope()
return old_top; return old_top;
} }
void merge_top_scope()
{
if ( scopes.size() < 2 )
reporter->InternalError("scope merge underflow");
auto t = pop_scope();
for ( const auto& v : t->OrderedVars() )
{
v->SetOffset(top_scope->Length());
top_scope->Insert(v->Name(), v);
}
}
ScopePtr current_scope() ScopePtr current_scope()
{ {
return top_scope; return top_scope;

View file

@ -93,10 +93,6 @@ extern void push_existing_scope(ScopePtr scope);
// Returns the one popped off. // Returns the one popped off.
extern ScopePtr pop_scope(); extern ScopePtr pop_scope();
// Merges the elements of the current scope with the one surrounding it,
// popping it in the process.
extern void merge_top_scope();
extern ScopePtr current_scope(); extern ScopePtr current_scope();
extern ScopePtr global_scope(); extern ScopePtr global_scope();

View file

@ -1933,11 +1933,10 @@ WhenInfo::WhenInfo(bool arg_is_return) : is_return(arg_is_return)
void WhenInfo::Build(StmtPtr ws) void WhenInfo::Build(StmtPtr ws)
{ {
// This will call ws->Error() if it's deprecated and we can
// short-circuit.
if ( IsDeprecatedSemantics(ws) ) if ( IsDeprecatedSemantics(ws) )
{
merge_top_scope();
return; return;
}
if ( ! cl ) if ( ! cl )
{ {
@ -2095,9 +2094,9 @@ bool WhenInfo::IsDeprecatedSemantics(StmtPtr ws)
} }
std::string msg = util::fmt("\"when\" statement referring to locals without an " std::string msg = util::fmt("\"when\" statement referring to locals without an "
"explicit [] capture is deprecated: %s", "explicit [] capture: %s",
vars.c_str()); vars.c_str());
ws->Warn(msg.c_str()); ws->Error(msg.c_str());
return true; return true;
} }

View file

@ -606,9 +606,8 @@ public:
private: private:
// True if the "when" statement corresponds to old-style deprecated // True if the "when" statement corresponds to old-style deprecated
// semantics (no captures, but needing captures). Also generates // semantics (no captures, but needing captures). Also triggers
// the corresponding deprecation warnings, which are associated // an error associated with "ws".
// with "ws".
bool IsDeprecatedSemantics(StmtPtr ws); bool IsDeprecatedSemantics(StmtPtr ws);
// Build those elements we'll need for invoking our lambda. // Build those elements we'll need for invoking our lambda.

View file

@ -1,5 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[x=1, y=2], [x=11, y=12] [x=1, y=2], [x=11, y=12]
[x=1, y=55], [x=11, y=12] [x=1, y=55], [x=11, y=12]
[x=111, y=222], [x=13, y=14] [x=99, y=222], [x=13, y=14]
[x=99, y=222], [x=13, y=14] [x=99, y=222], [x=13, y=14]

View file

@ -1,7 +1,7 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in <...>/when-capture-errors.zeek, lines 19-22: "when" statement referring to locals without an explicit [] capture is deprecated: orig1 (when (0 < g) { print orig1}) error in <...>/when-capture-errors.zeek, lines 19-22: "when" statement referring to locals without an explicit [] capture: orig1 (when (0 < g) { print orig1})
warning in <...>/when-capture-errors.zeek, lines 25-28: "when" statement referring to locals without an explicit [] capture is deprecated: orig3 (when (0 < g || orig3) { print g}) error in <...>/when-capture-errors.zeek, lines 25-28: orig3 is used inside "when" statement but not captured (when (0 < g || orig3) { print g})
warning in <...>/when-capture-errors.zeek, lines 34-38: "when" statement referring to locals without an explicit [] capture is deprecated: orig1 (when (0 < g) { print g} timeout 1.0 sec { print orig1}) error in <...>/when-capture-errors.zeek, lines 34-38: orig1 is used inside "when" statement but not captured (when (0 < g) { print g} timeout 1.0 sec { print orig1})
error in <...>/when-capture-errors.zeek, lines 66-70: orig2 is used inside "when" statement but not captured (when (0 < g) { print orig1} timeout 1.0 sec { print orig2}) error in <...>/when-capture-errors.zeek, lines 66-70: orig2 is used inside "when" statement but not captured (when (0 < g) { print orig1} timeout 1.0 sec { print orig2})
error in <...>/when-capture-errors.zeek, lines 76-80: orig3 is captured but not used inside "when" statement (when (0 < g) { print orig1} timeout 1.0 sec { print orig2}) error in <...>/when-capture-errors.zeek, lines 76-80: orig3 is captured but not used inside "when" statement (when (0 < g) { print orig1} timeout 1.0 sec { print orig2})
error in <...>/when-capture-errors.zeek, line 83: no such local identifier: l1 error in <...>/when-capture-errors.zeek, line 83: no such local identifier: l1

View file

@ -18,7 +18,7 @@ global h: opaque of Broker::Store;
function print_index(k: any) function print_index(k: any)
{ {
when ( local r = Broker::get(h, k) ) when [k] ( local r = Broker::get(h, k) )
{ {
print "master", k, r$status, r$result; print "master", k, r$status, r$result;
} }
@ -88,7 +88,7 @@ global inserted: event();
function print_index(k: any) function print_index(k: any)
{ {
when ( local r = Broker::get(h, k) ) when [k] ( local r = Broker::get(h, k) )
{ {
print "clone", k, r$status, r$result; print "clone", k, r$status, r$result;
} }

View file

@ -8,7 +8,7 @@ global g = 0;
function async_foo1(arg: r) : r function async_foo1(arg: r) : r
{ {
return when ( g > 0 ) return when [copy arg] ( g > 0 )
{ {
arg$x = 99; arg$x = 99;
return r($x = 11, $y = 12); return r($x = 11, $y = 12);
@ -29,7 +29,7 @@ event zeek_init()
local orig1 = r($x = 1, $y = 2); local orig1 = r($x = 1, $y = 2);
local orig2 = copy(orig1); local orig2 = copy(orig1);
when ( g == 1 && local resp1 = async_foo1(orig1) ) when [copy orig1] ( g == 1 && local resp1 = async_foo1(orig1) )
{ {
++g; ++g;
print orig1, resp1; print orig1, resp1;
@ -41,10 +41,12 @@ event zeek_init()
print orig2, resp2; print orig2, resp2;
} }
# The when within async_foo2 does not copy, so orig3 and orig4
# within the when body are modified with.
local orig3 = r($x = 111, $y = 222); local orig3 = r($x = 111, $y = 222);
local orig4 = copy(orig3); local orig4 = copy(orig3);
when ( g == 3 && local resp4 = async_foo2(orig3) ) when [copy orig3] ( g == 3 && local resp4 = async_foo2(orig3) )
{ {
++g; ++g;
print orig3, resp4; print orig3, resp4;

View file

@ -43,7 +43,7 @@ global ready_for_data: event();
event on_demand() event on_demand()
{ {
local host = 7.2.1.5; local host = 7.2.1.5;
when ( local result = SumStats::request_key("test sumstat", [$host=host]) ) when [host] ( local result = SumStats::request_key("test sumstat", [$host=host]) )
{ {
print "SumStat key request"; print "SumStat key request";
if ( "test" in result ) if ( "test" in result )