D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7334 - autogenerated CTOR don't check invariant
Summary: autogenerated CTOR don't check invariant
Status: RESOLVED DUPLICATE of issue 519
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-21 09:07 UTC by devbai
Modified: 2012-01-21 17:21 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description devbai 2012-01-21 09:07:46 UTC
Hello

I miss the invariant-check during the execution of 
the auto-generated ctor.

Following programm ....

**************
import std.stdio;

class ClassA 
{
  private int mValue;
  
  // with the following default-CTOR would be the invariants be checked:
  // this()
  // {}
        
  @property
    {  
      public int value() const 
      { return mValue;
      }
        
      public void value(int newValue)
      { mValue = newValue;
      }
    }
        
  invariant()
  { writeln("checking INVARIANT ClassA (mValue: ", mValue, ")");
    assert ( mValue>0, "NOT mValue>0");              
  }
}
   
   
public static void main ()
{
  writeln("creating ClassA");
  ClassA aObject = new ClassA();   // may not pass;
                                   // because ClassA has no good CTOR
  writeln("reading value:");
  writeln(aObject.value);
}
**************

compiled with "DMD64 D Compiler v2.057" 
and run will shown this output:


**************
core.exception.AssertError@src/bug/bugNoInvariantCheckWithAutoCTOR/TestAppBugNoInvariantCheckWithAutoCTOR.d(24): NOT mValue>0
----------------
(_d_assert_msg+0x1f) [0x448323]
./(void TestAppBugNoInvariantCheckWithAutoCTOR.ClassA.__invariant()+0x5e) [0x443f46]
./(void invariant._d_invariant(Object)+0x20) [0x4489d0]
./(const(@property int function()) TestAppBugNoInvariantCheckWithAutoCTOR.ClassA.value+0x11) [0x443ea9]
./(_Dmain+0x50) [0x443f98]
./(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x17) [0x44892f]
./(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x4484d6]
./(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x42) [0x448982]
./(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x2a) [0x4484d6]
./(main+0xd3) [0x448467]
/lib/libc.so.6(__libc_start_main+0xfe) [0x7ffe8348ed8e]
----------------
creating ClassA
reading value:
checking INVARIANT ClassA (mValue: 0)
**************

I would expect that the assert should throw an Error during the ctor.
Comment 1 yebblies 2012-01-21 17:21:31 UTC

*** This issue has been marked as a duplicate of issue 519 ***