[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:44:40 PDT 2024


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

Fixes #102025

>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] [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'}}
+}



More information about the cfe-commits mailing list