Too optimistic static binding in switch
Imported from http://bugzilla.roxen.com/bugzilla/show_bug.cgi?id=1311
Reported by Martin Stjernholm mast@roxen.com
The following program could reasonably be expected to either produce a compilation error or write "1":
class A
{
constant foo = 17;
int x (int i)
{
switch (i) {
// On the following line, foo is bound as if being local
// (i.e. statically).
case foo: return 1;
default: return 0;
}
}
}
class B
{
inherit A;
constant foo = 19;
}
int main()
{
werror ("%O\n", B()->x (19));
}
The problem is that switch merrily resolves the constant expression "foo" to 17, without considering the situation where the constant is overridden in an inheriting class. In other words, in all switch labels, any constant identifier is bound as if it were implicitly preceded by "local::".
I think that's nonintuitive; it ought to be necessary to either declare the constant as local or final, or use "local::" in front of it when used as a switch label.
This problem exists at least in 7.0 and later, but it obviously cannot be changed without introducing compatibility problems.