diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index cf1ba02cbc4b2..3d46f1ac3ff5f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -199,6 +199,12 @@ C++20 Feature Support to update the ``__cpp_concepts`` macro to `202002L`. This enables ```` from libstdc++ to work correctly with Clang. +- User defined constructors are allowed for copy-list-initialization with CTAD. + The example code for deduction guides for std::map in + (`cppreference `_) + will now work. + (#GH62925). + C++23 Feature Support ^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index f443e327eaf32..4b876db436b48 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -603,7 +603,7 @@ class InitializationKind { /// Normal context IC_Normal, - /// Normal context, but allows explicit conversion functionss + /// Normal context, but allows explicit conversion functions IC_ExplicitConvs, /// Implicit context (value initialization) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index ed8b226a6b39f..211b6887befa3 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -10892,8 +10892,6 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( // FIXME: The "second phase of [over.match.list] case can also // theoretically happen here, but it's not clear whether we can // ever have a parameter of the right type. - bool SuppressUserConversions = Kind.isCopyInit(); - if (TD) { SmallVector TmpInits; for (Expr *E : Inits) @@ -10903,12 +10901,12 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( TmpInits.push_back(E); AddTemplateOverloadCandidate( TD, FoundDecl, /*ExplicitArgs=*/nullptr, TmpInits, Candidates, - SuppressUserConversions, + /*SuppressUserConversions=*/false, /*PartialOverloading=*/false, AllowExplicit, ADLCallKind::NotADL, /*PO=*/{}, AllowAggregateDeductionCandidate); } else { AddOverloadCandidate(GD, FoundDecl, Inits, Candidates, - SuppressUserConversions, + /*SuppressUserConversions=*/false, /*PartialOverloading=*/false, AllowExplicit); } }; diff --git a/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp b/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp new file mode 100644 index 0000000000000..368857c83a88e --- /dev/null +++ b/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp @@ -0,0 +1,46 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value -std=c++20 %s + +namespace std { +typedef decltype(sizeof(int)) size_t; + +template +struct initializer_list { + const E *p; + size_t n; + initializer_list(const E *p, size_t n) : p(p), n(n) {} +}; + +// Classes to use to reproduce the exact scenario present in #62925. +template +class pair { + public: + pair(T f, Y s) {} +}; + +template +class map { + public: + map(std::initializer_list>) {} + map(std::initializer_list>, int a) {} +}; + +} // namespace std + +// This is the almost the exact code that was in issue #62925. +void testOneLevelNesting() { + std::map mOk = {std::pair{5, 'a'}, {6, 'b'}, {7, 'c'}}; + + // Verify that narrowing conversion is disabled in the first level of nesting. + 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}} +} + +void testMultipleLevelNesting() { + std::map aOk = {{std::pair{5, 'c'}, {5, 'c'}}, 5}; + + // Verify that narrowing conversion is disabled when it is not in a nested + // in another std::initializer_list, but it happens in the most outer one. + 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}} + + // Verify that narrowing conversion is disabled in the first level of nesting. + 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}} +}