Thread overview
[Issue 10326] New: Disallow 'invariant' for immutable, allow class/struct invariants without (), and later disallow usage of ()
Jun 11, 2013
Kenji Hara
June 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10326

           Summary: Disallow 'invariant' for immutable, allow class/struct
                    invariants without (), and later disallow usage of ()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2013-06-10 14:38:25 PDT ---
struct Foo {
    int x = 0;
    invariant() {
        assert(x != 0);
    }
    void bar() {}
}
void main() {
    Foo f;
    f.bar;
    invariant int y; // deprecation warning
}



With dmd 2.064alpha this code gives the deprecation warning: temp.d(11): Deprecation: use of 'invariant' rather than 'immutable' is deprecated

And then correctly asserts at run-time:
core.exception.AssertError@temp(4): Assertion failure

- - - - - - - - - - - - - - - - - - -

The first step (I suggest in dmd 2.064) is to disallow 'invariant' as a replacement for the 'immutable' turning the deprecation into an error, and at the same time allow the definition of class/struct invariants without the ( ), and and at the same time a give a deprecation message where a ( ) is used (the alternative is to give just a warning, but this change has a low probability of introducing bugs in D code, so a deprecation is enough):


struct Foo {
    int x = 0;
    invariant() { // deprecation warning here, no () needed.
        assert(x != 0);
    }
    invariant { // OK
        assert(x != 0);
    }
    void bar() {}
}
void main() {
    Foo f;
    f.bar;
    invariant int y; // error
}

- - - - - - - - - - - - - - - - - - -

In a successive compiler release the usage of ( ) at the struct/class invariant becomes an error to tidy up the language:

struct Foo {
    int x = 0;
    invariant() { // error
        assert(x != 0);
    }
    invariant { // OK
        assert(x != 0);
    }
    void bar() {}
}
void main() {
    Foo f;
    f.bar;
    invariant int y; // error
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 11, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10326


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2013-06-11 07:39:36 PDT ---
For changing current deprecation messages to errors: https://github.com/D-Programming-Language/dmd/pull/2160

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10326



--- Comment #2 from github-bugzilla@puremagic.com 2013-06-15 02:55:27 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/5479162eed97bd7107485437f15d426658e25a76 fix Issue 10326 - Change old `invariant` usage to error

deprecation -> error

https://github.com/D-Programming-Language/dmd/commit/a9fa72432981719fa88a042d3f4649fca7ddf0fe Merge pull request #2160 from 9rnsr/fix10326

Issue 10326 - Change old `invariant` usage to error

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