www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3578] New: Impossible to run a struct invariant using assert(s)

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578

           Summary: Impossible to run a struct invariant using assert(s)
           Product: D
           Version: 1.041
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: llucax gmail.com



PST ---
This fails to compile (with -unittest):

---
struct S {
    invariant() { assert (false); }
    unittest { S s; assert (s); }
}
---

With this error message:
  inv.d(3): Error: expression s of type S does not have a boolean value

With both DMD 1 and 2 (tested with DMD 1.041 and DMD 2.037. Changing struct to
class compiles fine (and dumps a core if you run it, of course ;).

Thes specs are not clear on this in both D1 and D2. Invariant is only mentioned
in structs, but I guess it's more likely to think that the description of class
invariant apply to structs too, so I guess this should be supported in both D1
and D2.

If it's a change to big to make it in D1, I think the specs should be updated
to clarify the differences between struct and class invariants. In D2 I don't
see why the language should not be updated to support this, since there is no
other way to trigger an invariant check (and, for example, invariant is not
executed when a struct is constructed or when using alias this, I think).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 05 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578


Leandro Lucarella <llucax gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



PDT ---
*** Issue 4326 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578


Shin Fujishiro <rsinfu gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsinfu gmail.com



---
It's undocumented, but struct invariants are fired on pointers:
--------------------
struct S
{
    invariant() { assert(0); }  // (3)
}
void main()
{
    S s;
    assert(&s);
}
--------------------
% dmd -run test.d
core.exception.AssertError test(3): Assertion failure
--------------------

Though it's non-intuitive, I find it reasonable considering that structs can
define opCast to bool.  assert(s) would be ambiguous if s had both invariant
and opCast!bool.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578




PDT ---

 It's undocumented, but struct invariants are fired on pointers:
 --------------------
 struct S
 {
     invariant() { assert(0); }  // (3)
 }
 void main()
 {
     S s;
     assert(&s);
 }
 --------------------
 % dmd -run test.d
 core.exception.AssertError test(3): Assertion failure
 --------------------
 
 Though it's non-intuitive, I find it reasonable considering that structs can
 define opCast to bool.  assert(s) would be ambiguous if s had both invariant
 and opCast!bool.
I never expected that, but it makes sense as you mention. I guess this behavior should be documented in the specs. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578




Commit pushed to
https://github.com/D-Programming-Language/d-programming-language.org

https://github.com/D-Programming-Language/d-programming-language.org/commit/8ed7c3ded95c50a4dc297a272fa53c0deb18e68e
fix Issue 3578 - Impossible to run a struct invariant using assert(s)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3578


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



Commit pushed to
https://github.com/D-Programming-Language/d-programming-language.org

https://github.com/D-Programming-Language/d-programming-language.org/commit/8ed7c3ded95c50a4dc297a272fa53c0deb18e68e
fix Issue 3578 - Impossible to run a struct invariant using assert(s)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012