[Issue 19537] New: Invariants from base classes not called

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 1 21:56:46 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19537

          Issue ID: 19537
           Summary: Invariants from base classes not called
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dhasenan at gmail.com

>From the spec ( https://dlang.org/spec/contracts.html ):

"Class invariants are inherited, that is, any class invariant is implicitly in
addition to the invariants of its base classes."

Consider the following code:

---
import core.exception;

class A
{
    int i = 3;
    invariant { assert(i >= 2); }
}
class B : A
{
    void setB(int v) { i = v; }
    invariant { assert(i <= 10); }
}
void main()
{
    B b = new B;
    b.setB(1);
}
---

B should have two assertions in its invariant:
1. assert(i >= 2)
2. assert(i <= 10)

The first assertion should fail; 1 is not greater than or equal to 2. However,
A's invariant is not called. This can be verified by adding `writeln("A
invariant");` to A's invariant.

Either this is a deliberate change and the spec needs to be updated, or it's a
bug and the behavior needs to be changed.

--


More information about the Digitalmars-d-bugs mailing list