Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Make _d_arrayctor weakly pure - #3587

Merged
dlang-bot merged 4 commits into
dlang:masterfrom
teodutu:make-_d_arrayctor-weakly-pure
Dec 3, 2021
Merged

dlang-bot merged 4 commits into
dlang:masterfrom
teodutu:make-_d_arrayctor-weakly-pure

Conversation

@teodutu

@teodutu teodutu commented Oct 12, 2021 •

Copy link
Copy Markdown
Member

This PR changes _d_arrayctor from a pure function to a weakly pure function.
This change is required because the removal of the _d_arrayctor hook implemented in this PR: dlang/dmd#13116, lowers constructions such as the one below to calls to _d_arrayctor:

struct S {};
const S[2] b;
const S[2] a = b;

The above code is lowered to _d_arrayctor(a, b). For const and immutable arguments, the function is strongly pure, and since its return value is ignored, the compiler might choose to remove the call altogether.

In order to avoid this behaviour, _d_arrayctor has to be made weakly pure, which is what the added 3rd parameter does.

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @teodutu! 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 coverage diff by visiting the details link of the codecov check)
  • 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

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + druntime#3587"

Comment thread src/core/internal/array/construction.d Resolved
@teodutu
teodutu force-pushed the make-_d_arrayctor-weakly-pure branch from 1e9b037 to 2eaed68 Compare October 13, 2021 13:59
@PetarKirov

Copy link
Copy Markdown
Member

@teodutu @RazvanN7 have you considered casting away const/immutable of the argument to inside the frontend, before this function is called?

void _d_arrayctor(T)(return scope T[] to, scope const(T)[] from);

struct S {};
const S[2] b;

const S[2] a = b; // gets lowered to:

const S[2] a = void;
_d_arrayctor!S(cast(S[]) a, b);

Essentially, T should only be instantiated with mutable types.

Also, why does _d_arrayctor need to return anything but unit (void), given that the result written to to?

@teodutu

teodutu commented Oct 14, 2021

Copy link
Copy Markdown
Member Author

@teodutu @RazvanN7 have you considered casting away const/immutable of the argument to inside the frontend, before this function is called?

Not yet, but this might work. Thanks!

Also, why does _d_arrayctor need to return anything but unit (void), given that the result written to to?

This is necessary for constructors where you might do something like this:

struct S
{
    int[3] a, b;
    this (ref int[3] c)
    {
        a = b = c;
        // this would be lowered to: _d_arrayctor(a, _d_arrayctor(b, c))
    }
}

@RazvanN7

RazvanN7 commented Oct 14, 2021 •

Copy link
Copy Markdown
Contributor

@PetarKirov But isn't that going to make the call to _d_arrayCtor not possible in @safe code?

@RazvanN7

Copy link
Copy Markdown
Contributor

@PetarKirov see also: https://forum.dlang.org/post/[email protected]

@teodutu
teodutu force-pushed the make-_d_arrayctor-weakly-pure branch 2 times, most recently from 254c1e2 to 1a82c3e Compare December 2, 2021 20:20
teodutu added a commit to teodutu/dmd that referenced this pull request Dec 2, 2021
teodutu added a commit to teodutu/dmd that referenced this pull request Dec 2, 2021
This undoes the changes brought by dlang#3611

Signed-off-by: Teodor Dutu <[email protected]>
@teodutu
teodutu force-pushed the make-_d_arrayctor-weakly-pure branch from 1a82c3e to 18e0799 Compare December 2, 2021 20:40
teodutu added a commit to teodutu/dmd that referenced this pull request Dec 2, 2021
teodutu added a commit to teodutu/dmd that referenced this pull request Dec 2, 2021
@RazvanN7

RazvanN7 commented Dec 3, 2021

Copy link
Copy Markdown
Contributor

Please follow-up on deleting the @trusted on the function.

@teodutu
teodutu deleted the make-_d_arrayctor-weakly-pure branch December 3, 2021 19:01
teodutu added a commit to teodutu/dmd that referenced this pull request Dec 3, 2021
RazvanN7 pushed a commit to dlang/dmd that referenced this pull request Dec 4, 2021
* Lower array construction to _d_array{,set}ctor

Signed-off-by: Dan Printzell <[email protected]>

* Remove backend code for _d_array{,set}ctor

Signed-off-by: Dan Printzell <[email protected]>

* Intercept and handle _d_array{,set}ctor for CTFE

Signed-off-by: Dan Printzell <[email protected]>

* Merge similar branches

* Fix failing test errors

Signed-off-by: Teodor Dutu <[email protected]>

* Update frontend.h with symbols for new templates

Signed-off-by: Teodor Dutu <[email protected]>

* Fix _d_array functions position in frontend.h

Signed-off-by: Teodor Dutu <[email protected]>

* _d_arrayctor: Update CTFE for weakly pure hook

druntime PR: dlang/druntime#3587

Signed-off-by: Teodor Dutu <[email protected]>

Co-authored-by: Dan Printzell <[email protected]>
kinke pushed a commit to ldc-developers/dmd-testsuite that referenced this pull request Dec 19, 2021
* Lower array construction to _d_array{,set}ctor

Signed-off-by: Dan Printzell <[email protected]>

* Remove backend code for _d_array{,set}ctor

Signed-off-by: Dan Printzell <[email protected]>

* Intercept and handle _d_array{,set}ctor for CTFE

Signed-off-by: Dan Printzell <[email protected]>

* Merge similar branches

* Fix failing test errors

Signed-off-by: Teodor Dutu <[email protected]>

* Update frontend.h with symbols for new templates

Signed-off-by: Teodor Dutu <[email protected]>

* Fix _d_array functions position in frontend.h

Signed-off-by: Teodor Dutu <[email protected]>

* _d_arrayctor: Update CTFE for weakly pure hook

druntime PR: dlang/druntime#3587

Signed-off-by: Teodor Dutu <[email protected]>

Co-authored-by: Dan Printzell <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants