Skip to content

Commit 69f0a91

Browse files
authoredAug 13, 2020
Merge pull request #11545 from Geod24/fix-17351
Fix 17351 - Manifest constants can't sometimes be passed by `ref` in CTFE
2 parents 07e354a + ecf2ce2 commit 69f0a91

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎src/dmd/dinterpret.d

+9-1
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,15 @@ public:
21312131
return;
21322132
}
21332133

2134-
if (goal == ctfeNeedLvalue)
2134+
// Note: This is a workaround for
2135+
// https://issues.dlang.org/show_bug.cgi?id=17351
2136+
// The aforementioned bug triggers when passing manifest constant by `ref`.
2137+
// If there was not a previous reference to them, they are
2138+
// not cached and trigger a "cannot be read at compile time".
2139+
// This fix is a crude solution to get it to work. A more proper
2140+
// approach would be to resolve the forward reference, but that is
2141+
// much more involved.
2142+
if (goal == ctfeNeedLvalue && e.var.type.isMutable())
21352143
{
21362144
VarDeclaration v = e.var.isVarDeclaration();
21372145
if (v && !v.isDataseg() && !v.isCTFE() && !istate)

‎test/compilable/test17351.d

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bool fun(S)(ref S[3] a) { assert(a == [42, 84, 169]); return true; }
2+
bool fun2(S)(ref S a) { return true; }
3+
void main()
4+
{
5+
static const int[3] sa = [42, 84, 169];
6+
static const double sa2 = 42.42;
7+
static assert(fun(sa));
8+
static assert(fun2(sa2));
9+
}
10+
11+
int f1(ref const int p) { return p; }
12+
int f2(ref const int[2] p) { return p[0] + p[1]; }
13+
void test2()
14+
{
15+
static immutable int[2] P = [ 0, 1 ];
16+
static assert(f2(P) == 1);
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.