File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26902,8 +26902,10 @@ Evaluate the given new-expression.
2690226902 a_constant_ptr cp = init_cp->variant.aggregate.first_constant;
2690326903 for (; cp != NULL; cp = cp->next) ++n_init_elems;
2690426904 } else {
26905- /* ck_string case. */
26906- n_init_elems = init_cp->variant.string.length;
26905+ /* ck_string case (whose length is in bytes). */
26906+ a_type_ptr etp = skip_typerefs(
26907+ init_tp->variant.array.element_type);
26908+ n_init_elems = init_cp->variant.string.length / etp->size;
2690726909 } /* if */
2690826910 if (n_init_elems > orig_alloc_length) {
2690926911 info_with_pos_num(ec_constexpr_alloc_too_small,
Original file line number Diff line number Diff line change 1+ [GH #501]
2+ Array new with a wide string literal initializer in constant evaluation
3+
4+ During constant evaluation, the initializer of an array new-expression that is
5+ a string literal of a character type wider than one byte (e.g., wchar_t) was
6+ treated as having as many elements as the literal has bytes, so a large enough
7+ array could be diagnosed as too small for it. For example:
8+
9+ constexpr int f(int n) {
10+ auto p = new wchar_t[n]{L"ab"};
11+ int r = p[1];
12+ delete[] p;
13+ return r;
14+ }
15+ static_assert(f(3) == L'b'); // Previously an error; now okay.
Original file line number Diff line number Diff line change 1+ fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
2+ "Test_name.c", line 11: error: expression must have a constant value
3+ static_assert(g(2) == L'b'); // Error: too few elements for the initializer.
4+ ^
5+ "Test_name.c", line 6: note: number of dynamically-allocated elements (2) too small for initializer
6+ auto p = new wchar_t[n]{L"ab"};
7+ ^
8+
9+ 1 error detected in the compilation of "Test_name.c".
Original file line number Diff line number Diff line change 1+ fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
Original file line number Diff line number Diff line change 1+ // type:fn
2+ // options_all:--c++20
3+ // remark:Array new with a too-long wide string literal initializer
4+
5+ constexpr int g (int n) {
6+ auto p = new wchar_t [n]{L" ab" };
7+ int r = p[1 ];
8+ delete[] p;
9+ return r;
10+ }
11+ static_assert (g(2 ) == L' b' ); // Error: too few elements for the initializer.
Original file line number Diff line number Diff line change 1+ // type:fp
2+ // options_all:--c++20
3+ // remark:Constexpr array new with a wide string literal initializer
4+
5+ // The number of elements of a string literal initializer is compared with the
6+ // number of allocated elements, regardless of the character size.
7+ constexpr int g (int n) {
8+ auto p = new wchar_t [n]{L" ab" };
9+ int r = p[1 ] + p[n - 1 ];
10+ delete[] p;
11+ return r;
12+ }
13+ static_assert (g(3 ) == L' b' );
14+ static_assert (g(4 ) == L' b' );
15+
16+ constexpr int h (int n) {
17+ auto p16 = new char16_t [n]{u" ab" };
18+ auto p32 = new char32_t [n]{U" ab" };
19+ auto p8 = new char8_t [n]{u8" ab" };
20+ int r = (p16[1 ] == u' b' ) + (p32[1 ] == U' b' ) + (p8[1 ] == u8 ' b' );
21+ delete[] p16;
22+ delete[] p32;
23+ delete[] p8;
24+ return r;
25+ }
26+ static_assert (h(3 ) == 3 );
You can’t perform that action at this time.
0 commit comments