[clang] [Sema][CTAD] Allow user defined conversion for copy-list-initialization (PR #94752)

Gábor Spaits via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 9 05:25:09 PDT 2024


https://github.com/spaits updated https://github.com/llvm/llvm-project/pull/94752

>From 8e1f3aa676b891c9566344ad2be046898df34a3a Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Fri, 7 Jun 2024 14:38:08 +0200
Subject: [PATCH 1/2] [CTAD] Allow user defined conversion

---
 clang/include/clang/Sema/Initialization.h     |  2 +-
 clang/lib/Sema/SemaInit.cpp                   |  6 +-
 .../SemaCXX/ctad-copy-init-list-narrowing.cpp | 90 +++++++++++++++++++
 3 files changed, 93 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp

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<Expr *, 8> 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..21b1137158d5a
--- /dev/null
+++ b/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp
@@ -0,0 +1,90 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value -std=c++20 %s
+namespace std
+{
+  typedef long unsigned int size_t;
+}
+
+namespace std
+{
+  template<class _E>
+    class initializer_list
+    {
+    public:
+      typedef _E value_type;
+      typedef const _E& reference;
+      typedef const _E& const_reference;
+      typedef size_t size_type;
+      typedef const _E* iterator;
+      typedef const _E* const_iterator;
+
+    private:
+      iterator _M_array;
+      size_type _M_len;
+
+
+      constexpr initializer_list(const_iterator __a, size_type __l)
+      : _M_array(__a), _M_len(__l) { }
+
+    public:
+      constexpr initializer_list() noexcept
+      : _M_array(0), _M_len(0) { }
+
+
+      constexpr size_type
+      size() const noexcept { return _M_len; }
+
+
+      constexpr const_iterator
+      begin() const noexcept { return _M_array; }
+
+
+      constexpr const_iterator
+      end() const noexcept { return begin() + size(); }
+    };
+
+  template<class _Tp>
+    constexpr const _Tp*
+    begin(initializer_list<_Tp> __ils) noexcept
+    { return __ils.begin(); }
+
+  template<class _Tp>
+    constexpr const _Tp*
+    end(initializer_list<_Tp> __ils) noexcept
+    { return __ils.end(); }
+}
+
+template<class T, class Y>
+class pair{
+    private:
+    T fst;
+    Y snd;
+    public:
+    pair(T f, Y s) : fst(f), snd(s) {}
+};
+
+template<class T, class Y>
+class map {
+    public:
+    map(std::initializer_list<pair<T, Y>>, int a = 4, int b = 5) {}
+};
+
+template<class T, class Y>
+class Contained {
+  public:
+  Contained(T, Y) {}
+};
+
+template<class T, class Y>
+class A {
+  public:
+  A(std::initializer_list<Contained<T, Y> >, int) {}
+};
+
+int main() {
+    map mOk ={pair{5, 'a'}, {6, 'b'}, {7, 'c'}};
+    map mNarrow ={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}}
+
+    A aOk = {{Contained{5, 'c'}, {5, 'c'}}, 5};
+    A aNarrowNested = {{Contained{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}}
+    A aNarrow = {{Contained{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}}
+}

>From 8e33ac051a04e5d5c6d73efdd4cf47868d5dc644 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Sun, 9 Jun 2024 14:22:42 +0200
Subject: [PATCH 2/2] Simplify boiler plate and re-organize tests

---
 .../SemaCXX/ctad-copy-init-list-narrowing.cpp | 95 +++++++------------
 1 file changed, 35 insertions(+), 60 deletions(-)

diff --git a/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp b/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp
index 21b1137158d5a..a2de4cf8a2f90 100644
--- a/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp
+++ b/clang/test/SemaCXX/ctad-copy-init-list-narrowing.cpp
@@ -1,65 +1,25 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value -std=c++20 %s
-namespace std
-{
-  typedef long unsigned int size_t;
-}
-
-namespace std
-{
-  template<class _E>
-    class initializer_list
-    {
-    public:
-      typedef _E value_type;
-      typedef const _E& reference;
-      typedef const _E& const_reference;
-      typedef size_t size_type;
-      typedef const _E* iterator;
-      typedef const _E* const_iterator;
-
-    private:
-      iterator _M_array;
-      size_type _M_len;
-
-
-      constexpr initializer_list(const_iterator __a, size_type __l)
-      : _M_array(__a), _M_len(__l) { }
-
-    public:
-      constexpr initializer_list() noexcept
-      : _M_array(0), _M_len(0) { }
-
-
-      constexpr size_type
-      size() const noexcept { return _M_len; }
-
 
-      constexpr const_iterator
-      begin() const noexcept { return _M_array; }
+namespace std {
+  typedef decltype(sizeof(int)) size_t;
 
+  template <typename E>
+  struct initializer_list
+  {
+    const E *p;
+    size_t n;
+    initializer_list(const E *p, size_t n) : p(p), n(n) {}
+  };
 
-      constexpr const_iterator
-      end() const noexcept { return begin() + size(); }
-    };
-
-  template<class _Tp>
-    constexpr const _Tp*
-    begin(initializer_list<_Tp> __ils) noexcept
-    { return __ils.begin(); }
-
-  template<class _Tp>
-    constexpr const _Tp*
-    end(initializer_list<_Tp> __ils) noexcept
-    { return __ils.end(); }
-}
+  struct string {
+    string(const char *);
+  };
 
+  // Classes to use to reproduce the exact scenario present in 62925.
 template<class T, class Y>
 class pair{
-    private:
-    T fst;
-    Y snd;
     public:
-    pair(T f, Y s) : fst(f), snd(s) {}
+    pair(T f, Y s) {}
 };
 
 template<class T, class Y>
@@ -68,6 +28,10 @@ class map {
     map(std::initializer_list<pair<T, Y>>, int a = 4, int b = 5) {}
 };
 
+} // namespace std
+
+
+// Classes to test different levels of nestings and conversions.
 template<class T, class Y>
 class Contained {
   public:
@@ -80,11 +44,22 @@ class A {
   A(std::initializer_list<Contained<T, Y> >, int) {}
 };
 
-int main() {
-    map mOk ={pair{5, 'a'}, {6, 'b'}, {7, 'c'}};
-    map mNarrow ={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}}
 
-    A aOk = {{Contained{5, 'c'}, {5, 'c'}}, 5};
-    A aNarrowNested = {{Contained{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}}
-    A aNarrow = {{Contained{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}}
+// 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() {
+  A aOk = {{Contained{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.
+  A aNarrowNested = {{Contained{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.
+  A aNarrow = {{Contained{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}}
 }



More information about the cfe-commits mailing list