Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

RazvanN7
Copy link
Contributor

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

@RazvanN7 RazvanN7 requested a review from ibuclaw as a code owner June 12, 2019 11:01
@dlang-bot
Copy link
Contributor

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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

Auto-close Bugzilla Severity Description
519 major Invariant not called from autogenerated class/struct constructor/destructor

Testing this PR locally

If 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"

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented Jun 12, 2019

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 fun must be destroyed, but since S.init is passed, then the invariant that is called before destruction is going to fail. I'm not sure how to fix this. It seems the problem stems from the fact that format passes around .init states when it shouldn't.

@@ -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
Copy link
Contributor

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.

@RazvanN7
Copy link
Contributor Author

I will not be pursuing this. Anyone is welcomed to adopt it.

@RazvanN7 RazvanN7 closed this Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants