Ticket #395 (new Problem)

Opened 16 months ago

Last modified 11 months ago

Cannot add global to module if it already exists in global namespace

Reported by: gregor Owned by:
Priority: Normal Milestone: Bro2.1
Component: Bro Version: git/master
Keywords: Cc:

Description

The following policy snippet doesn't work:

global a = 1;
# the above can also be in a different file
module FOO;
export {
   global a = 2;
}

I get the error: (a): error, already defined

When I define the a's in a different order (module first, then global) it works as expected.

From a quick glance it seems that the problem is that when bro parses the global in the module it searches all its scopes to check whether the identifier already exists. I think this scanning of all scopes is necessary to make redefs of globals in modules work (global xyz and redef xyz are handled pretty much the same in parse.y).

The problem also exists when trying to "overload" types (i.e., have type xyz in the global scope and then try to redefine in in the module scope).

Change History

comment:1 Changed 12 months ago by seth

This works for some reason now, but I don't seem to have access to the 'a' in the global namespace. GLOBAL::a doesn't even work.

Gregor, could you close this ticket if it works now for what you needed it for?

comment:2 Changed 11 months ago by seth

  • Milestone changed from Bro1.6 to Bro1.7

Since this generally works now (for variables and types) I'm going to push the ticket back. I'm leaving it open though because we should still address the fact that you can't access the same-named global variable or type from inside of a module that uses the same name.

Here's my little test script for trying to access the global values and types:

global a = 1;
type Test: count;

module FOO;
export {
        type Test: string;
        global a = 2;
        global b: Test = "test";
        global c: GLOBAL::Test = 5;
}

print a;
print GLOBAL::a;
print b;
print c;

This should print out:

2
1
test
c

But instead it currently fails with this error:

./test33.bro, line 9: error: identifier not defined: GLOBAL::Test
error and ./test33.bro, line 9
   (error and 5): error, arithmetic mixed with non-arithmetic
./test33.bro, line 13: error: unknown identifier GLOBAL::a, at or near "GLOBAL::a"
Note: See TracTickets for help on using tickets.