Bug 7337 – subclasses without invariants don't check basisclass invariant after method

Status
NEW
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-21T10:19:51Z
Last change time
2024-12-13T17:57:53Z
Keywords
contracts, pull, wrong-code
Assigned to
No Owner
Creator
devbai
Depends on
5039
Moved to GitHub: dmd#18402 →

Comments

Comment #0 by devbai — 2012-01-21T10:19:51Z
Hello, Subclasses without own invariants don't check basisclass invariant after method. example: the programm .. ************** import std.stdio; /** Basisclass with own invariants */ class ClassA { private int mValue=0; invariant() { writeln("checking INVARIANT ClassA"); assert ( mValue>=0 ); } public void doSomthing() { writeln("ClassA.doSomthing() - BODY"); } } /** Subclass with own invariants */ class ClassB : ClassA { invariant() { writeln("checking INVARIANT ClassB"); } override public void doSomthing() { writeln("ClassB.doSomthing() - BODY"); } } /** Subclass without own invariants */ class ClassC : ClassA { override public void doSomthing() { writeln("ClassC.doSomthing() - BODY"); } } public static void main () { writeln("*** START ***"); writeln("\nClassA:"); ClassA aObject = new ClassA(); aObject.doSomthing(); writeln("\nClassB:"); ClassB bObject = new ClassB(); bObject.doSomthing(); writeln("\nClassC:"); ClassC cObject = new ClassC(); cObject.doSomthing(); writeln("\n*** END ***"); } ************** compiled with DMD64 D Compiler v2.057 will output: ************** *** START *** ClassA: checking INVARIANT ClassA ClassA.doSomthing() - BODY checking INVARIANT ClassA ClassB: checking INVARIANT ClassB checking INVARIANT ClassA ClassB.doSomthing() - BODY checking INVARIANT ClassB checking INVARIANT ClassA ClassC: checking INVARIANT ClassA ClassC.doSomthing() - BODY *** END *** ************** as seen classC-method doSomething() dont check invariant after methode-call.
Comment #1 by yebblies — 2012-01-29T08:11:53Z
This is currently disabled in FuncDeclaration::addPostInvariant, and enabling it causes a lot of error bugs in druntime and phobos, presumably due to issue 5039. Once that bug is closed this will hopefully be a quick fix.
Comment #2 by crunchengine — 2014-04-13T18:57:17Z
(In reply to yebblies from comment #1) > This is currently disabled in FuncDeclaration::addPostInvariant, and > enabling it causes a lot of error bugs in druntime and phobos, presumably > due to issue 5039. Once that bug is closed this will hopefully be a quick > fix. I just tried to re-enable it, everything seems ok after the issue 5039 resolution, no error in druntime or phobos appeared in a simple build. Can I create a mini pull request ?
Comment #3 by yebblies — 2014-07-29T18:20:18Z
(In reply to Adrien Pensart from comment #2) > (In reply to yebblies from comment #1) > > This is currently disabled in FuncDeclaration::addPostInvariant, and > > enabling it causes a lot of error bugs in druntime and phobos, presumably > > due to issue 5039. Once that bug is closed this will hopefully be a quick > > fix. > > I just tried to re-enable it, everything seems ok after the issue 5039 > resolution, no error in druntime or phobos appeared in a simple build. > > Can I create a mini pull request ? Yes, please do.
Comment #4 by crunchengine — 2014-07-29T21:50:20Z
I retried to fix this some month ago in days, but i'm not comfortable at all with dmd internals and i didn't succeed... it could take weeks for me to resolve this... i started writing a testcase into runnable/testinvariant.d : /***************************************************/ // 7337 void test7337() { class A { static uint invariantStatus; public void foo() in { assert(invariantStatus == 0); } out { assert(invariantStatus == 2); } body { printf("in A.foo : %d\n", invariantStatus); assert(invariantStatus == 1); } invariant() { printf("in A invariant\n"); invariantStatus++; } } class B : A { override public void foo() { printf("in B.foo : %d\n", invariantStatus); } } A a = new A(); a.foo(); A.invariantStatus = 0; B b = new B(); b.foo(); }
Comment #5 by crunchengine — 2014-07-29T22:19:16Z
diff --git a/src/func.c b/src/func.c index 93ca8fd..395cb51 100644 --- a/src/func.c +++ b/src/func.c @@ -3933,7 +3933,7 @@ bool FuncDeclaration::addPostInvariant() AggregateDeclaration *ad = isThis(); ClassDeclaration *cd = ad ? ad->isClassDeclaration() : NULL; return (ad && !(cd && cd->isCPPclass()) && - ad->inv && + //ad->inv && global.params.useInvariants && (protection == PROTprotected || protection == PROTpublic || protection == PROTexport) && !naked && There is multiple problems, i'll try to give you what i tested but phobos is ok. A trivial error in druntime, it does not compile the first time, but it works if we replay the exact command, i don't know why (MODEL=64 make -j4 -f posix.mak install) : src/core/sync/semaphore.d(182): Error: function core.sync.semaphore.Semaphore.wait no return exp; or assert(0); at end of function src/core/sync/semaphore.d(298): Error: function core.sync.semaphore.Semaphore.tryWait no return exp; or assert(0); at end of function some test failed in dmd suite : * compilable/interpret3.d : ../src/dmd -m64 -Icompilable -odtest_results/compilable -oftest_results/compilable/interpret3_0.o -c compilable/interpret3.d compilable/interpret3.d(143): Error: assert(w.r.x == 4) failed compilable/interpret3.d(181): called from here: retRefTest2() compilable/interpret3.d(181): while evaluating: static assert(retRefTest2() == 2) dmd: interpret.c:180: void CtfeStack::setValue(VarDeclaration*, Expression*): Assertion `v->ctfeAdrOnStack >= 0 && v->ctfeAdrOnStack < stackPointer()' failed. Aborted ============================== Test failed: expected rc == 0, exited with rc == 134 Makefile:163: recipe for target 'test_results/compilable/interpret3.d.out' failed * runnable/sdtor.d : Test failed. The logged output: ../src/dmd -m64 -Irunnable -odtest_results/runnable -oftest_results/runnable/sdtor_0 runnable/sdtor.d S7353 test_results/runnable/sdtor_0 core.exception.AssertError@runnable/sdtor.d(859): Assertion failure ---------------- test_results/runnable/sdtor_0() [0x445679] test_results/runnable/sdtor_0(void sdtor.test33()+0x8e) [0x437c36] test_results/runnable/sdtor_0(_Dmain+0xa9) [0x443841] test_results/runnable/sdtor_0(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x44963b] test_results/runnable/sdtor_0(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x44958e] test_results/runnable/sdtor_0(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x30) [0x4495f4] test_results/runnable/sdtor_0(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x44958e] test_results/runnable/sdtor_0(_d_run_main+0x193) [0x44950f] test_results/runnable/sdtor_0(main+0x17) [0x4456c7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f475382fb45] [...output...] ============================== Test failed: expected rc == 0, exited with rc == 1
Comment #6 by yebblies — 2014-07-30T11:47:49Z
I guess it wasn't so quick after all.
Comment #7 by dlang-bot — 2022-07-14T12:40:21Z
@RazvanN7 created dlang/dmd pull request #14304 "Fix Issue 7337 - subclasses without invariants don't check baseclass …" fixing this issue: - Fix Issue 7337 - subclasses without invariants don't check baseclass invariant after method https://github.com/dlang/dmd/pull/14304
Comment #8 by robert.schadek — 2024-12-13T17:57:53Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18402 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB