Bug 19537 – Invariants from base classes not called

Status
NEW
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-01-01T21:56:46Z
Last change time
2024-12-13T19:01:50Z
Assigned to
No Owner
Creator
Neia Neutuladh
Moved to GitHub: dmd#17891 →

Comments

Comment #0 by dhasenan — 2019-01-01T21:56:46Z
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
According to run.dlang.io, this is a regression introduced in version 2.077.1. https://run.dlang.io/is/OwGz74
Comment #2 by b2.temp — 2019-01-02T09:16:27Z
Reducing further shows that this has never worked correctly actually, see https://run.dlang.io/is/D3nV8K.
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17891 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB