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 22617 - CTFE rejects modification of copied static array #13575

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from

Conversation

RazvanN7
Copy link
Contributor

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 RazvanN7 added the Regression PRs that fix regressions label Jan 27, 2022
@RazvanN7
Copy link
Contributor Author

Hmm bot seems to be down, cc @CyberShadow .

@CyberShadow
Copy link
Member

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.

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

Comment on lines +2510 to +2511
// If it's immutable and contains indirections, we don't need to dup it
result = e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem right. The following example still fails with this patch:

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

enum force = countWins([null, null]);

@ibuclaw
Copy link
Member

ibuclaw commented Jan 15, 2023

@RazvanN7 coming back to this soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants