Skip to content

Commit 8ea702c

Browse files
Repeated constexpr initialization of a local array from a string literal [GH #521]
1 parent f75ed4b commit 8ea702c

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎src/interpret.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29510,8 +29510,20 @@ a reused value, if any, in the iwp_1st_resume visit.
2951029510
switch (dip->kind) {
2951129511
case dik_constant:
2951229512
case dik_nonconstant_aggregate:
29513-
result = extract_value_from_constant(ips, dip->variant.constant.ptr,
29514-
dst_addr, implied_src);
29513+
{ a_constant_ptr cp = dip->variant.constant.ptr;
29514+
result = extract_value_from_constant(ips, cp, dst_addr, implied_src);
29515+
if (constant_is(cp, ck_string)) {
29516+
/* The ck_string contents were copied to the array being initialized.
29517+
Undo the mapping of the array storage to the ck_string entry
29518+
since it is not a persistent association (the storage may be
29519+
initialized again, e.g., in the next iteration of a loop). */
29520+
a_byte *mapped;
29521+
get_stack_bytes(ips, dst_addr.address, mapped);
29522+
if (mapped == (a_byte*)cp) {
29523+
unmap_stack_bytes(ips, dst_addr.address);
29524+
} /* if */
29525+
} /* if */
29526+
}
2951529527
break;
2951629528
case dik_lambda:
2951729529
if (constexpr_lambdas_enabled) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[GH #521]
2+
Constant evaluation of repeated initializations of an array from a string
3+
4+
When constant evaluation initialized a local array from a string literal, it
5+
left the storage of the array associated with the string literal. If the same
6+
storage was initialized again (e.g., in the next iteration of a loop), the
7+
front end could fail with the internal error "duplicate map key in
8+
interpreter". For example:
9+
10+
constexpr int f() {
11+
int r = 0;
12+
for (int i = 0; i < 2; ++i) {
13+
char a[2] = "a";
14+
r += a[0];
15+
}
16+
return r;
17+
}
18+
static_assert(f() == 2*'a'); // Previously an internal error.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++14 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=2 --c++20 Test_name.c
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//type:fp
2+
//options:--c++14:--c++20
3+
//remark:Repeated constexpr initialization of a local array from a string
4+
5+
// Initializing a local array from a string literal must not leave its storage
6+
// associated with the string literal: The same storage is initialized again
7+
// in the next iteration of a loop.
8+
9+
constexpr int f() {
10+
int r = 0;
11+
for (int i = 0; i < 2; ++i) {
12+
char a[2] = "a";
13+
r += a[0];
14+
}
15+
return r;
16+
}
17+
static_assert(f() == 2*'a', "");
18+
19+
constexpr int g() {
20+
int r = 0;
21+
for (int i = 0; i < 3; ++i) {
22+
char a[4] = "abc";
23+
a[i] = 'x';
24+
r = r*256 + a[0];
25+
}
26+
return r;
27+
}
28+
static_assert(g() == ('x'*256 + 'a')*256 + 'a', "");
29+
30+
constexpr long h() {
31+
long r = 0;
32+
for (int i = 0; i < 2; ++i) {
33+
const char a[] = "ab";
34+
char16_t b[3] = u"cd";
35+
wchar_t c[4] = L"ef";
36+
r += a[1] + b[1] + c[1];
37+
}
38+
return r;
39+
}
40+
static_assert(h() == 2*('b' + u'd' + L'f'), "");

0 commit comments

Comments
 (0)