dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Fix Issue 22617 - CTFE rejects modification of copied static array

Open RazvanN7 opened this issue 3 years ago • 3 comments

int countWins(const uint[2] arr)
{
	uint[2] copy = arr;
        copy[0] = 0;
	return 0;
}

enum force = countWins([4, 8]);

When passing [4, 8] to countWins the interpreter thinks that if the parameter is constant, then there is no need to create a copy for CTFE, however, this is true only if the variable never gets modified. In this case, the arr is correctly assigned to a mutable static array, but since the interpreter thinks that the original array literal will never get modifier it simply initializes copy to a slice of it. Hence the error.

This patch fixes the issue by copying the initial array literal on the ctfe stack. The copy is elided only if the array element type contains indirections.

RazvanN7 avatar Jan 27 '22 15:01 RazvanN7

Hmm bot seems to be down, cc @CyberShadow .

RazvanN7 avatar Jan 27 '22 15:01 RazvanN7

Jan 27 15:26:55 [main(VpEu) WRN] GET https://api.github.com/repos/dlang/dmd/issues/13575/comments failed;  Not Found 404.

Race condition in GitHub API.

CyberShadow avatar Jan 27 '22 15:01 CyberShadow

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

Auto-close Bugzilla Severity Description
22617 regression CTFE rejects modification of copied static array

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 "stable + dmd#13575"

dlang-bot avatar Jan 27 '22 15:01 dlang-bot

@RazvanN7 coming back to this soon?

ibuclaw avatar Jan 15 '23 14:01 ibuclaw