Bug 20811 – Regression as of 2.066.0 - CTFE static variable retained across calls
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2020-05-08T18:08:55Z
Last change time
2023-01-01T06:25:03Z
Keywords
CTFE, pull, wrong-code
Assigned to
No Owner
Creator
Adam D. Ruppe
Comments
Comment #0 by destructionator — 2020-05-08T18:08:55Z
Here's a fun one:
string bug() {
char[1] counter = "0";
counter[$-1]++;
return counter.dup;
}
pragma(msg, bug());
pragma(msg, bug());
pragma(msg, bug());
pragma(msg, bug());
Each call returns something different; the counter variable is apparently retained between calls.
And if you call bug() at runtime after that, it retains the last counter. Add this in:
void main() {
assert(bug == "5");
assert(bug == "5");
assert(bug == "5");
assert(bug == "5");
}
and it passes! Unless you change the msgs lolol.
Same behavior on Windows and Linux.
I almost didn't want to report this because having a compile-time counter like this can actually be pretty useful! But it is also obviously wrong.
History output:
Random error prior to 63. Worked correctly between 63 and 65, then
2.066.0: Status -6 with output: dmd: interpret.c:4025: void Interpreter::interpretAssignCommon(BinExp*, Expression* (*)(Type*, Expression*, Expression*), int): Assertion `newval->op == TOKarrayliteral' failed.
2.067.1 to 2.071.2: Failure with output:
-----
1
2
3
4
Comment #1 by b2.temp — 2020-05-08T19:23:35Z
> having a compile-time counter like this can actually be pretty useful
There was a plan to add a new primary exp for counting, see the defunct PR https://github.com/dlang/dmd/pull/10131/files.
The problem with a CTFE counter is that it must allow static CTFE variables, this is not correct. global and mutable states are forbidden in CTFE, they can only change in a function body
Comment #2 by dlang-bot — 2022-12-20T23:57:00Z
@ibuclaw created dlang/dmd pull request #14729 "fix Issue 20811 - Regression as of 2.066.0 - CTFE static variable retained across calls" fixing this issue:
- fix Issue 20811 - Regression as of 2.066.0 - CTFE static variable retained across calls
https://github.com/dlang/dmd/pull/14729
Comment #3 by dlang-bot — 2022-12-21T10:48:05Z
dlang/dmd pull request #14729 "fix Issue 20811 - Regression as of 2.066.0 - CTFE static variable retained across calls" was merged into stable:
- 6205ad3ebcc936545ae623ec1527dc1fe95cf804 by Iain Buclaw:
fix Issue 20811 - Regression as of 2.066.0 - CTFE static variable retained across calls
https://github.com/dlang/dmd/pull/14729
Comment #4 by dlang-bot — 2023-01-01T06:25:03Z
dlang/dmd pull request #14765 "merge stable" was merged into master:
- 1f286b9cec6c83bf24028113407aee057f4d357a by Iain Buclaw:
fix Issue 20811 - Regression as of 2.066.0 - CTFE static variable retained across calls
https://github.com/dlang/dmd/pull/14765