[clang] [Clang] prevent null explicit object argument from being deduced (PR #104328)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 15:59:55 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/104328

>From 432686be091dae7ff532028fd5c41f73862e0bd5 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 15 Aug 2024 01:43:15 +0300
Subject: [PATCH 1/2] [Clang] prevent null explicit object argument from being
 deduced

---
 clang/docs/ReleaseNotes.rst                   |  1 +
 clang/lib/Sema/SemaTemplateDeduction.cpp      |  3 ++
 .../cxx2b-deduced-explicit-arg.cpp            | 30 +++++++++++++++++++
 3 files changed, 34 insertions(+)
 create mode 100644 clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1541b0cbf4875c..7acaf4ebdf1f7d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -245,6 +245,7 @@ Bug Fixes to C++ Support
   specialization of a conversion function template.
 - Correctly diagnose attempts to use a concept name in its own definition;
   A concept name is introduced to its scope sooner to match the C++ standard. (#GH55875)
+- Fixed an assertion failure by preventing null explicit object arguments from being deduced. (#GH102025).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index ec951d5ac06dbc..fc883e423f4710 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -4462,6 +4462,9 @@ TemplateDeductionResult Sema::DeduceTemplateArguments(
       ParamTypesForArgChecking.push_back(ParamType);
 
       if (ParamIdx == 0 && HasExplicitObject) {
+        if (ObjectType.isNull())
+          return TemplateDeductionResult::InvalidExplicitArguments;
+
         if (auto Result = DeduceCallArgument(ParamType, 0,
                                              /*ExplicitObjectArgument=*/true);
             Result != TemplateDeductionResult::Success)
diff --git a/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp b/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp
new file mode 100644
index 00000000000000..4880b148c53c14
--- /dev/null
+++ b/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++2b -verify %s
+
+namespace std {
+struct rv {};
+
+template <bool B, typename T> struct enable_if;
+template <typename T> struct enable_if<true, T> { typedef T type; };
+
+template <typename U, typename T>
+typename enable_if<__is_convertible(T, rv), U>::type forward(T &);
+template <typename U, typename T>
+typename enable_if<!__is_convertible(T, rv), U &>::type forward(T &);
+}
+
+struct Foo {
+  template <class T>
+  constexpr auto operator[](this T &&self, auto... i)        // expected-note {{candidate template ignored: substitution failure [with T = Foo &, i:auto = <>]: member '_evaluate' used before its declaration}}
+      -> decltype(_evaluate(std::forward<T>(self), i...)) {
+    return self._evaluate(i...);
+  }
+
+private:
+  template <class T>
+  constexpr auto _evaluate(this T &&self, auto... i) -> decltype((i + ...));
+};
+
+int main() {
+  Foo foo;
+  return foo[]; // expected-error {{no viable overloaded operator[] for type 'Foo'}}
+}

>From 3017d5303fe1ae522cdbdf8662519ba3f5250262 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 15 Aug 2024 01:59:43 +0300
Subject: [PATCH 2/2] move test to cxx2b-deducing-this

---
 clang/test/SemaCXX/cxx2b-deducing-this.cpp    | 19 ++++++++++++
 .../cxx2b-deduced-explicit-arg.cpp            | 30 -------------------
 2 files changed, 19 insertions(+), 30 deletions(-)
 delete mode 100644 clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp

diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 45fee6514c12bc..985c0d81cc320f 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -972,3 +972,22 @@ struct R {
 	f(r_value_ref); // expected-error {{no matching member function for call to 'f'}}
   }
 };
+
+namespace GH102025 {
+struct Foo {
+  template <class T>
+  constexpr auto operator[](this T &&self, auto... i) // expected-note {{candidate template ignored: substitution failure [with T = Foo &, i:auto = <>]: member '_evaluate' used before its declaration}}
+      -> decltype(_evaluate(self, i...)) {
+    return self._evaluate(i...);
+  }
+
+private:
+  template <class T>
+  constexpr auto _evaluate(this T &&self, auto... i) -> decltype((i + ...));
+};
+
+int main() {
+  Foo foo;
+  return foo[]; // expected-error {{no viable overloaded operator[] for type 'Foo'}}
+}
+}
diff --git a/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp b/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp
deleted file mode 100644
index 4880b148c53c14..00000000000000
--- a/clang/test/SemaTemplate/cxx2b-deduced-explicit-arg.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -std=c++2b -verify %s
-
-namespace std {
-struct rv {};
-
-template <bool B, typename T> struct enable_if;
-template <typename T> struct enable_if<true, T> { typedef T type; };
-
-template <typename U, typename T>
-typename enable_if<__is_convertible(T, rv), U>::type forward(T &);
-template <typename U, typename T>
-typename enable_if<!__is_convertible(T, rv), U &>::type forward(T &);
-}
-
-struct Foo {
-  template <class T>
-  constexpr auto operator[](this T &&self, auto... i)        // expected-note {{candidate template ignored: substitution failure [with T = Foo &, i:auto = <>]: member '_evaluate' used before its declaration}}
-      -> decltype(_evaluate(std::forward<T>(self), i...)) {
-    return self._evaluate(i...);
-  }
-
-private:
-  template <class T>
-  constexpr auto _evaluate(this T &&self, auto... i) -> decltype((i + ...));
-};
-
-int main() {
-  Foo foo;
-  return foo[]; // expected-error {{no viable overloaded operator[] for type 'Foo'}}
-}



More information about the cfe-commits mailing list