www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Weird (buggy) behaviour of "protected static" in classes

Hi all,

I have noticed that "protected static" doesn't work currently with 
classes. In my case, I wanted to use "static immutable", but I have 
tried regular static members and methods, and the same issue happens. 
However, the puzzling part is that protected enums (which are a valid 
workaround for me, perhaps not for others) work.

The spec [1] is a bit unclear about how that's supposed to work, 
although I tend to thing that it allows them:

---
protected only applies inside classes (and templates as they can be 
mixed in) and means that a symbol can only be seen by members of the 
same module, or by a derived class. If accessing a protected instance 
member through a derived class member function, that member can only be 
accessed for the object instance which can be implicitly cast to the 
same type as ‘this’. protected module members are illegal.
---

Is that a bug? I have to say, I think so, since it can also affect other 
symbols that are defined... for *grandchild* classes! You can see the 
multiple problems in the snippet [1]:

```dio.d
module dio;

public class A {
     public enum int a = 1;
     protected enum int b = 2;
     private enum int c = 3;
     public static immutable int d = 4;
     protected static immutable int e = 5;
     private static immutable int f = 6;

     protected static struct S { }
}

public class A2 {
     protected static struct S { }
}
```


```main.d
import dio;

class B : A {
     pragma(msg, "The value of A.a is: ", typeof(super).a);
     pragma(msg, "The value of A.b is: ", typeof(super).b);
     //pragma(msg, "The value of A.c is: ", typeof(super).c); // 
Expected failure
     pragma(msg, "The value of A.d is: ", typeof(super).d);
     pragma(msg, "The value of A.e is: ", typeof(super).e); // *BUG* 
Comment this line and *BOTH* errors will go away!!
     //pragma(msg, "The value of A.f is: ", typeof(super).f); // 
Expected failure

     S s;
}

class C : B {
     S s;
}

class B2 : A2 {
     S s;
}
class C2 : B2 {
     S s;
}

void main()
{
}
```

The most shocking thing is that it is C's access to A.S that gets 
affected, I think that must be a compiler bug. Still, it would be nice 
to confirm that "protected static" is supposed to work as intuitively 
expected.

Best,

A.

[1]: https://glot.io/snippets/f4a1b3x4sf
Aug 28 2018