New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Issue 519 - Invariant not called from autogenerated class/struct constructor/destructor #10022
Conversation
…constructor/destructor
Thanks for your pull request and interest in making D better, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + dmd#10022" |
The phobos failure can be reduced to: int fun(Args...)(Args e)
{
return 1;
}
static const checkFormatException(alias fmt, Args...) =
{
fun(Args.init);
return null;
}();
void main()
{
import std.stdio;
struct S
{
int i;
@disable this();
invariant() { assert(this.i); }
this(int i) @safe in { assert(i); } do { this.i = i; }·
string toString() { return "S"; }
}
S s = S(1);
alias e = checkFormatException!("asd", s);
static assert(!e); The parameter to |
@@ -970,6 +970,12 @@ DtorDeclaration buildDtor(AggregateDeclaration ad, Scope* sc) | |||
switch (ad.dtors.dim) | |||
{ | |||
case 0: | |||
/* If running invariant, need a destructor to hang it on, | |||
* but only do for root modules, as ones in the library may not | |||
* have been compiled with useInvariants |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Contracts (and possibly invariants) should really be generated and called by the caller, just like a template.
I will not be pursuing this. Anyone is welcomed to adopt it. |
Rebase of #7536 . I added a few modifications:
in the original pull request, when the call to the invariant was introduced for StructLiteralExp the case where a default struct constructor was used had to be excluded; this was done by checking the length of the array of arguments that was passed to the struct literal (in e2ir.d), however by that point the default struct initializer was already lowered to a StructLiteral call with default values as arguments for the fields of the struct; the tests did not catch this bug because only structs with 0 members were used with default struct constructors.
I modified a test (runnable/test19731.d) because before this patch the invariant was not called when an object had to be destroyed, while after it is called before destruction. A deprecation cannot be issued; this is silent change of code behavior.
cc @ibuclaw @WalterBright