Skip to content

Commit f75ed4b

Browse files
Reject explicit conversion functions for list-initialized reference temporaries (CWG 2267) [GH #344]
1 parent 6a6c7f2 commit f75ed4b

18 files changed

Lines changed: 398 additions & 7 deletions

File tree

‎src/il.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,10 @@ typedef int a_conv_context_set;
28232823
#define CCO_FORCE_DEPENDENCE ((a_conv_context_set)0x20000000)
28242824
/* TRUE if overload resolution should treat the call
28252825
as dependent in any case. */
2826+
#define CCO_EXPLICIT_CONV_FUNCTIONS_RETURN_REF ((a_conv_context_set)0x40000000)
2827+
/* Used with CCO_ALLOW_EXPLICIT_CONV_FUNCTIONS to
2828+
restrict the explicit conversion functions allowed
2829+
to those that return a reference. */
28262830

28272831

28282832
/*

‎src/overload.c‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16401,7 +16401,10 @@ ultimate binding is to an rvalue reference .
1640116401
} /* if */
1640216402
} else if (conv_context & CCO_ALLOW_EXPLICIT_CONV_FUNCTIONS) {
1640316403
/* We've been told specifically to allow explicit conversion
16404-
functions. */
16404+
functions (possibly only those that return a reference). */
16405+
if (conv_context & CCO_EXPLICIT_CONV_FUNCTIONS_RETURN_REF) {
16406+
require_reference_return = TRUE;
16407+
} /* if */
1640516408
} else {
1640616409
goto reject_function;
1640716410
} /* if */
@@ -28193,6 +28196,32 @@ will be an lvalue instead of the usual prvalue.
2819328196
in reproducing the source when the cp_gen_be is used. */
2819428197
rmake_lvalue_temp = TRUE;
2819528198
} /* if */
28199+
if (is_direct_init &&
28200+
(clang_version_is(any_version) || gnu_version_is(<90000) ||
28201+
ms_version_is(<1929) ||
28202+
(ms_version_is(any_version) && ms_permissive))) {
28203+
/* Core issue 2267 clarified that the temporary is copy-list-
28204+
initialized even when the reference is direct-list-initialized,
28205+
and so explicit conversion functions cannot be used to initialize
28206+
it. For example:
28207+
struct A { explicit operator int(); } a;
28208+
int &&r{a}; // Error.
28209+
Clang, g++ before version 9, and MSVC in permissive mode (and
28210+
before version 19.29 also in non-permissive mode) accept such
28211+
cases, though. */
28212+
rconv_context |= CCO_ALLOW_EXPLICIT_CONV_FUNCTIONS;
28213+
} else if (is_direct_init && gnu_version_is(>= 140000)) {
28214+
/* g++ 14 and later bind the reference directly to the result of a
28215+
conversion function that returns a reference (a proposed resolution
28216+
of Core issue 1996), and that binding may use an explicit conversion
28217+
function:
28218+
struct B { explicit operator int&&(); };
28219+
int &&r{B{}}; // Okay with g++ 14.
28220+
Approximate that by allowing such conversion functions for the
28221+
initialization of the temporary. */
28222+
rconv_context |= CCO_ALLOW_EXPLICIT_CONV_FUNCTIONS |
28223+
CCO_EXPLICIT_CONV_FUNCTIONS_RETURN_REF;
28224+
} /* if */
2819628225
/* The cost of a reference initialization in overload resolution is the
2819728226
cost of the underlying initialization of the temporary, so pass
2819828227
arg_match down to the next level. See below for class cases. */
@@ -28201,7 +28230,7 @@ will be an lvalue instead of the usual prvalue.
2820128230
/*is_direct_init=*/FALSE,
2820228231
/*check_narrowing=*/TRUE,
2820328232
/*warning_on_narrowing=*/FALSE,
28204-
rconv_context | CCO_ALLOW_EXPLICIT_CONV_FUNCTIONS,
28233+
rconv_context,
2820528234
/*fill_in_dtor=*/TRUE,
2820628235
/*force_temp=*/TRUE,
2820728236
rmake_lvalue_temp,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[GH #344]
2+
Explicit conversion functions and list-initialization of references
3+
4+
When a reference is list-initialized from an element whose type is not
5+
reference-related to the referenced type, a temporary is created and the
6+
reference is bound to that temporary. The resolution of Core issue 2267
7+
clarified that this temporary is copy-list-initialized even when the reference
8+
itself is direct-list-initialized, so explicit conversion functions cannot be
9+
used to initialize it. The front end nevertheless accepted them, even for
10+
copy-list-initialization (including when passing an argument). That is now
11+
fixed. For example:
12+
13+
struct A { explicit operator int() const; } a;
14+
struct B { explicit operator int&&(); };
15+
void f(int&&);
16+
int &&r1{a}; // Now an error.
17+
int &&r2{B{}}; // Now an error.
18+
int &&r3 = {a}; // Now an error in all modes.
19+
void g() {
20+
f({a}); // Now an error in all modes.
21+
}
22+
23+
The change for EDGcpfe/23402 (in version 6.2) implemented the resolution of
24+
Core issue 2267 for initializations with parentheses (e.g., "int &&r(a);"),
25+
but not for list-initializations. Those were still governed by the change for
26+
EDGcpfe/21369 (in version 5.1), which allowed explicit conversion functions to
27+
initialize the temporary in every list-initialization of a reference (to
28+
accept cases like r2 above). That allowance is now limited to the emulation
29+
modes described below.
30+
31+
Because Clang, GCC before version 9, and Microsoft C++ in permissive mode
32+
(and before version 19.29 also in non-permissive mode) accept the
33+
direct-list-initialization cases, those are still accepted in the
34+
corresponding emulation modes. GCC 14 and later accept them only when the
35+
conversion function returns a reference (like r2 above), and so does the
36+
front end when emulating those versions.

‎tests/tests/edg/ccs/gnats21369.sft.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//type:cp
2-
//options_all:--c++11
2+
//options_all:--c++11 --gnu=80000
33

44
struct A {
55
explicit operator int() { return int{}; }

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2a-explicit-bool.rto/default.1.1.txt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,22 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
356356
int ai01 = {A<true>()}; // expected-error {{no viable conversion}}
357357
^
358358

359+
"Test_name.c", line 258: error: no suitable conversion function from "conversion::A<true>" to "const int" exists
360+
const int& ai11 = {A<true>()}; // expected-error {{no viable conversion}}
361+
^
362+
363+
"Test_name.c", line 259: error: no suitable conversion function from "conversion::A<true>" to "int" exists
364+
int&& ai31 = {A<true>()}; // expected-error {{no viable conversion}}
365+
^
366+
359367
"Test_name.c", line 260: error: no suitable conversion function from "conversion::A<true>" to "int" exists
360368
int ai41 = {A_true}; // expected-error {{no viable conversion}}
361369
^
362370

371+
"Test_name.c", line 261: error: no suitable conversion function from "conversion::A<true>" to "const int" exists
372+
const int& ai51 = {A_true}; // expected-error {{no viable conversion}}
373+
^
374+
363375
"Test_name.c", line 296: warning: 'explicit(bool)' is a C++20 feature
364376
explicit(enable_ifv<is_same<B, T2>::value, a>::value)
365377
^
@@ -815,4 +827,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
815827
explicit(1L) S(char, char, char);
816828
^
817829

818-
118 errors detected in the compilation of "Test_name.c".
830+
121 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx2a-explicit-bool.rto/default.2.1.txt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,22 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
296296
int ai01 = {A<true>()}; // expected-error {{no viable conversion}}
297297
^
298298

299+
"Test_name.c", line 258: error: no suitable conversion function from "conversion::A<true>" to "const int" exists
300+
const int& ai11 = {A<true>()}; // expected-error {{no viable conversion}}
301+
^
302+
303+
"Test_name.c", line 259: error: no suitable conversion function from "conversion::A<true>" to "int" exists
304+
int&& ai31 = {A<true>()}; // expected-error {{no viable conversion}}
305+
^
306+
299307
"Test_name.c", line 260: error: no suitable conversion function from "conversion::A<true>" to "int" exists
300308
int ai41 = {A_true}; // expected-error {{no viable conversion}}
301309
^
302310

311+
"Test_name.c", line 261: error: no suitable conversion function from "conversion::A<true>" to "const int" exists
312+
const int& ai51 = {A_true}; // expected-error {{no viable conversion}}
313+
^
314+
303315
"Test_name.c", line 305: error: no suitable conversion function from "conversion2::A<true>" to "int" exists
304316
int ai0 = A<true>(); // expected-error {{no viable conversion}}
305317
^
@@ -683,4 +695,4 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
683695
explicit(ptr) S(long); // expected-error {{conversion from 'const int *' to 'bool' is not allowed in a converted constant expression}}
684696
^
685697

686-
121 errors detected in the compilation of "Test_name.c".
698+
124 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.explicit.rto/default.1.1.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
123123
int copyList4 = {n}; // expected-error {{no viable conversion}}
124124
^
125125

126+
"Test_name.c", line 201: error: no suitable conversion function from "NotBool" to "const bool" exists
127+
const bool &copyList6 = {n}; // expected-error {{no viable conversion}}
128+
^
129+
126130
"Test_name.c", line 203: error: no suitable conversion function from "NotBool" to "const int" exists
127131
const int &copyList8 = {n}; // expected-error {{no viable conversion}}
128132
^
@@ -171,4 +175,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
171175
switch(EC){}
172176
^
173177

174-
30 errors detected in the compilation of "Test_name.c".
178+
31 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.explicit.rto/default.2.1.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
131131
int copyList4 = {n}; // expected-error {{no viable conversion}}
132132
^
133133

134+
"Test_name.c", line 201: error: no suitable conversion function from "NotBool" to "const bool" exists
135+
const bool &copyList6 = {n}; // expected-error {{no viable conversion}}
136+
^
137+
134138
"Test_name.c", line 203: error: no suitable conversion function from "NotBool" to "const int" exists
135139
const int &copyList8 = {n}; // expected-error {{no viable conversion}}
136140
^
@@ -171,4 +175,4 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
171175
switch(EC){}
172176
^
173177

174-
31 errors detected in the compilation of "Test_name.c".
178+
32 errors detected in the compilation of "Test_name.c".
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fe_only -DTEST_NUMBER=1 --c++11 Test_name.c
2+
"Test_name.c", line 21: error: no suitable conversion function from "E" to "int" exists
3+
int &&r1{e};
4+
^
5+
6+
"Test_name.c", line 22: error: no suitable conversion function from "E" to "const int" exists
7+
const int &r2{e};
8+
^
9+
10+
"Test_name.c", line 23: error: no suitable conversion function from "ER" to "int" exists
11+
int &&r3{ER{}};
12+
^
13+
14+
"Test_name.c", line 26: error: no suitable conversion function from "E" to "int" exists
15+
int &&r4 = {e};
16+
^
17+
18+
"Test_name.c", line 27: error: no suitable conversion function from "ER" to "int" exists
19+
int &&r5 = {ER{}};
20+
^
21+
22+
"Test_name.c", line 29: error: no suitable conversion function from "E" to "int" exists
23+
h({e});
24+
^
25+
26+
"Test_name.c", line 30: error: no suitable conversion function from "ER" to "int" exists
27+
h({ER{}});
28+
^
29+
30+
"Test_name.c", line 31: error: no suitable conversion function from "E" to "const int" exists
31+
hc({e});
32+
^
33+
34+
8 errors detected in the compilation of "Test_name.c".
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fe_only -DTEST_NUMBER=2 --c++20 Test_name.c
2+
"Test_name.c", line 21: error: no suitable conversion function from "E" to "int" exists
3+
int &&r1{e};
4+
^
5+
6+
"Test_name.c", line 22: error: no suitable conversion function from "E" to "const int" exists
7+
const int &r2{e};
8+
^
9+
10+
"Test_name.c", line 23: error: no suitable conversion function from "ER" to "int" exists
11+
int &&r3{ER{}};
12+
^
13+
14+
"Test_name.c", line 26: error: no suitable conversion function from "E" to "int" exists
15+
int &&r4 = {e};
16+
^
17+
18+
"Test_name.c", line 27: error: no suitable conversion function from "ER" to "int" exists
19+
int &&r5 = {ER{}};
20+
^
21+
22+
"Test_name.c", line 29: error: no suitable conversion function from "E" to "int" exists
23+
h({e});
24+
^
25+
26+
"Test_name.c", line 30: error: no suitable conversion function from "ER" to "int" exists
27+
h({ER{}});
28+
^
29+
30+
"Test_name.c", line 31: error: no suitable conversion function from "E" to "const int" exists
31+
hc({e});
32+
^
33+
34+
8 errors detected in the compilation of "Test_name.c".

0 commit comments

Comments
 (0)