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.
Comment #1 by snarwin+bugzilla — 2019-01-02T08:22:34Z
Comment #3 by razvan.nitu1305 — 2021-02-24T15:07:57Z
Since this is not a regression, I'm changing the importance to normal.
Comment #4 by razvan.nitu1305 — 2022-09-07T13:26:37Z
Actually, this is a regression. The reduction is a different test case.
Comment #5 by razvan.nitu1305 — 2022-09-07T13:30:29Z
(In reply to Basile-z from comment #2)
> Reducing further shows that this has never worked correctly actually, see
> https://run.dlang.io/is/D3nV8K.
This case is Issue 7337 which is fixed by [1]. However, [1] does not fix the originally reported bug.
[1] https://github.com/dlang/dmd/pull/14414
Comment #6 by robert.schadek — 2024-12-13T19:01:50Z