D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6648 - Only call invariant once for a tree of public functions
Summary: Only call invariant once for a tree of public functions
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-11 11:22 UTC by timon.gehr
Modified: 2011-09-11 18:57 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 timon.gehr 2011-09-11 11:22:02 UTC
Consider:

struct S{
    int x=1;
    invariant(){assert(x==1);}
    void foo(){x=2;bar(1);}
    void bar(int i){x=i;}
}

void main(){
    S s;
    s.foo();
}

This should run fine, but currently it throws an AssertError.
Because of this, D's invariant is mostly useless outside toy examples.
Comment 1 yebblies 2011-09-11 18:52:29 UTC
I've changed the title to better reflect what (I think) you're asking for.  Please correct if I've missed the point.

Note that the compiler is working exactly to spec here (for once), so this is an enhancement.

An alternative approach is to make bar private/protected, or put the body of bar into a private/protected function that both foo and bar can call.
Comment 2 timon.gehr 2011-09-11 18:57:15 UTC
If it works for private functions that is fine. I could have sworn I tried it both with private and public bar, but private bar seems to work. I am closing this. sry for the noise.