Bug 10326 – Disallow 'invariant' for immutable, allow class/struct invariants without (), and later disallow usage of ()

Status
REOPENED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-10T14:38:25Z
Last change time
2024-12-13T18:08:04Z
Keywords
pull
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=5038
Moved to GitHub: dmd#18607 →

Comments

Comment #0 by bearophile_hugs — 2013-06-10T14:38:25Z
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 }
Comment #1 by k.hara.pg — 2013-06-11T07:39:36Z
For changing current deprecation messages to errors: https://github.com/D-Programming-Language/dmd/pull/2160
Comment #2 by github-bugzilla — 2013-06-15T02:55:27Z
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
Comment #3 by k.hara.pg — 2014-05-09T21:58:56Z
*** Issue 12718 has been marked as a duplicate of this issue. ***
Comment #4 by k.hara.pg — 2014-05-10T14:15:08Z
Remove errors and accept the syntax `invariant { ... }` for declaring invariant block. https://github.com/D-Programming-Language/dmd/pull/3540
Comment #5 by github-bugzilla — 2014-05-12T02:18:17Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7cbb9a48ac7b9ea6c14ea859bc40dbc35e932e0c fix Issue 10326 - Disallow 'invariant' for immutable, allow class/struct invariants without (), and later disallow usage of () https://github.com/D-Programming-Language/dmd/commit/6f08c339d574bb3d808f40ea965f29caa433c000 Merge pull request #3540 from 9rnsr/fix10326 Issue 10326 - Disallow 'invariant' for immutable, allow class/struct invariants without (), and later disallow usage of ()
Comment #6 by bearophile_hugs — 2014-05-12T07:38:24Z
I'd like to reopen this issue because it's not done yet. The last missing step is to (in future) give an error in line 2 of program, disallowing the () after invariant: struct Foo { invariant() { assert(false); } void bar() {} } unittest() {} // Not allowed. void main() { Foo f; f.bar; }
Comment #7 by bearophile_hugs — 2014-05-12T09:48:19Z
Reopened. Waiting for opinions.
Comment #8 by temtaime — 2014-05-12T09:49:42Z
I agree () should give deprecation for now and error in future.
Comment #9 by k.hara.pg — 2014-05-17T10:23:11Z
(In reply to bearophile_hugs from comment #6) > I'd like to reopen this issue because it's not done yet. The last missing > step is to (in future) give an error in line 2 of program, disallowing the > () after invariant: > > > struct Foo { > invariant() { > assert(false); > } > void bar() {} > } > unittest() {} // Not allowed. > void main() { > Foo f; > f.bar; > } Instead of that, how about to accept both invariant(){...} and unittest(){...} ?
Comment #10 by temtaime — 2014-05-17T10:26:21Z
@Kenji it's useless imo.
Comment #11 by k.hara.pg — 2014-05-17T10:39:51Z
(In reply to Temtaime from comment #10) > @Kenji it's useless imo. My main concern is, that disallowing current invariant() syntax will break many existing D2 code. I'm afraid to decrease language stability. On the other hand, accepting the syntax `unittest(){...}` would not have any design benefit, but it will increase syntax consistency.
Comment #12 by temtaime — 2014-05-17T10:41:49Z
I think we should simply show deprecate message for invariant() to help updating user's code and remove it in future. It's best way imo.
Comment #13 by bearophile_hugs — 2014-05-17T12:17:26Z
(In reply to Kenji Hara from comment #9) > Instead of that, how about to accept both invariant(){...} and > unittest(){...} ? I prefer to disallow () in both.
Comment #14 by yebblies — 2014-05-17T16:24:28Z
(In reply to Kenji Hara from comment #9) > > Instead of that, how about to accept both invariant(){...} and > unittest(){...} ? I think that's a good idea. I see very little benefit to enforcing one syntax over the other. There doesn't seem much potential for confusion in having two syntaxes if both unittest and invariant work the same way.
Comment #15 by briancschott — 2014-05-18T00:09:06Z
(In reply to bearophile_hugs from comment #13) > (In reply to Kenji Hara from comment #9) > > > Instead of that, how about to accept both invariant(){...} and > > unittest(){...} ? > > I prefer to disallow () in both. I vote for this also. The parenthesis serve no purpose. The compiler should accept () with a deprecation message and then we should get rid of them. Quoting from bug 5038: "Well, this is a very strange situation. invariant without parentheses is LEGAL IN D1! When converting D1 code to D2, you get this nonsense deprecation message asking you to use immutable instead. We have existing production code which uses invariant without parentheses! We should make it legal again. This would be an undeprecation, which AFAIK has never happened before in D."
Comment #16 by robert.schadek — 2024-12-13T18:08:04Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18607 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB