Skip to content

Commit f80bd9b

Browse files
authored
[Sema][CTAD] Allow user defined conversion for copy-list-initialization (#94752)
Fixes #62925. The following code: ```cpp #include <map> int main() { std::map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::map m2(m1.begin(), m1.end()); // guide #1 } ``` Is rejected by clang, but accepted by both gcc and msvc: https://godbolt.org/z/6v4fvabb5 . So basically CTAD with copy-list-initialization is rejected. Note that this exact code is also used in a cppreference article: https://en.cppreference.com/w/cpp/container/map/deduction_guides I checked the C++11 and C++20 standard drafts to see whether suppressing user conversion is the correct thing to do for user conversions. Based on the standard I don't think that it is correct. ``` 13.3.1.4 Copy-initialization of class by user-defined conversion [over.match.copy] Under the conditions specified in 8.5, as part of a copy-initialization of an object of class type, a user-defined conversion can be invoked to convert an initializer expression to the type of the object being initialized. Overload resolution is used to select the user-defined conversion to be invoked ``` So we could use user defined conversions according to the standard. ``` If a narrowing conversion is required to initialize any of the elements, the program is ill-formed. ``` We should not do narrowing. ``` In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. ``` We should not use explicit constructors.
1 parent 04a75f5 commit f80bd9b

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

clang/docs/ReleaseNotes.rst

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ C++20 Feature Support
207207
to update the ``__cpp_concepts`` macro to `202002L`. This enables
208208
``<expected>`` from libstdc++ to work correctly with Clang.
209209

210+
- User defined constructors are allowed for copy-list-initialization with CTAD.
211+
The example code for deduction guides for std::map in
212+
(`cppreference <https://en.cppreference.com/w/cpp/container/map/deduction_guides>`_)
213+
will now work.
214+
(#GH62925).
215+
210216
C++23 Feature Support
211217
^^^^^^^^^^^^^^^^^^^^^
212218

clang/include/clang/Sema/Initialization.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class InitializationKind {
603603
/// Normal context
604604
IC_Normal,
605605

606-
/// Normal context, but allows explicit conversion functionss
606+
/// Normal context, but allows explicit conversion functions
607607
IC_ExplicitConvs,
608608

609609
/// Implicit context (value initialization)

clang/lib/Sema/SemaInit.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -10906,8 +10906,6 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
1090610906
// FIXME: The "second phase of [over.match.list] case can also
1090710907
// theoretically happen here, but it's not clear whether we can
1090810908
// ever have a parameter of the right type.
10909-
bool SuppressUserConversions = Kind.isCopyInit();
10910-
1091110909
if (TD) {
1091210910
SmallVector<Expr *, 8> TmpInits;
1091310911
for (Expr *E : Inits)
@@ -10917,12 +10915,12 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
1091710915
TmpInits.push_back(E);
1091810916
AddTemplateOverloadCandidate(
1091910917
TD, FoundDecl, /*ExplicitArgs=*/nullptr, TmpInits, Candidates,
10920-
SuppressUserConversions,
10918+
/*SuppressUserConversions=*/false,
1092110919
/*PartialOverloading=*/false, AllowExplicit, ADLCallKind::NotADL,
1092210920
/*PO=*/{}, AllowAggregateDeductionCandidate);
1092310921
} else {
1092410922
AddOverloadCandidate(GD, FoundDecl, Inits, Candidates,
10925-
SuppressUserConversions,
10923+
/*SuppressUserConversions=*/false,
1092610924
/*PartialOverloading=*/false, AllowExplicit);
1092710925
}
1092810926
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value -std=c++20 %s
2+
3+
namespace std {
4+
typedef decltype(sizeof(int)) size_t;
5+
6+
template <typename E>
7+
struct initializer_list {
8+
const E *p;
9+
size_t n;
10+
initializer_list(const E *p, size_t n) : p(p), n(n) {}
11+
};
12+
13+
// Classes to use to reproduce the exact scenario present in #62925.
14+
template<class T, class Y>
15+
class pair {
16+
public:
17+
pair(T f, Y s) {}
18+
};
19+
20+
template<class T, class Y>
21+
class map {
22+
public:
23+
map(std::initializer_list<pair<T, Y>>) {}
24+
map(std::initializer_list<pair<T, Y>>, int a) {}
25+
};
26+
27+
} // namespace std
28+
29+
// This is the almost the exact code that was in issue #62925.
30+
void testOneLevelNesting() {
31+
std::map mOk = {std::pair{5, 'a'}, {6, 'b'}, {7, 'c'}};
32+
33+
// Verify that narrowing conversion is disabled in the first level of nesting.
34+
std::map mNarrow = {std::pair{5, 'a'}, {6.0f, 'b'}, {7, 'c'}}; // expected-error {{type 'float' cannot be narrowed to 'int' in initializer list}} // expected-note {{insert an explicit cast to silence this issue}}
35+
}
36+
37+
void testMultipleLevelNesting() {
38+
std::map aOk = {{std::pair{5, 'c'}, {5, 'c'}}, 5};
39+
40+
// Verify that narrowing conversion is disabled when it is not in a nested
41+
// in another std::initializer_list, but it happens in the most outer one.
42+
std::map aNarrowNested = {{std::pair{5, 'c'}, {5.0f, 'c'}}, 5}; // expected-error {{type 'float' cannot be narrowed to 'int' in initializer list}} // expected-note {{insert an explicit cast to silence this issue}}
43+
44+
// Verify that narrowing conversion is disabled in the first level of nesting.
45+
std::map aNarrow = {{std::pair{5, 'c'}, {5, 'c'}}, 5.0f}; // expected-error {{type 'float' cannot be narrowed to 'int' in initializer list}} // expected-note {{insert an explicit cast to silence this issue}}
46+
}

0 commit comments

Comments
 (0)