[clang] [Clang] Fix crash instantiating functions defaulted outside class templates (PR #240376)

Muhammad Bassiouni via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 05:52:36 PST 2026


https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/240376

>From 55bbca8ac939a09ee3463047817d2fae431bbfe6 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Mon, 9 Nov 2026 15:41:30 +0200
Subject: [PATCH 1/2] [Clang] Fix crash instantiating functions defaulted
 outside class templates

A function that is defaulted outside its class template gets the
unqualified lookups performed at its definition from the pattern when
its definition is instantiated (#229560). The transfer happened even
when the pattern had no DefaultedOrDeletedFunctionInfo, which set the
info bit with a null pointer. If non-default floating-point features
were in effect at the point of instantiation, SetDeclDefaulted then
tried to store them and asserted that the function already had the
info.

Only transfer the info when the pattern has one, and assert that
setDefaultedOrDeletedInfo is never given a null info.

Fixes #240351
---
 clang/lib/AST/Decl.cpp                        |  1 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +--
 clang/test/SemaCXX/GH240351.cpp               | 69 +++++++++++++++++++
 3 files changed, 74 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH240351.cpp

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 72e082e9c969ec..39fa98e023c5db 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3164,6 +3164,7 @@ FunctionDecl::DefaultedOrDeletedFunctionInfo::Create(
 
 void FunctionDecl::setDefaultedOrDeletedInfo(
     DefaultedOrDeletedFunctionInfo *Info) {
+  assert(Info && "expected defaulted or deleted function info");
   assert(!FunctionDeclBits.HasDefaultedOrDeletedInfo && "already have this");
   assert(!Body && "can't replace function body with defaulted function info");
 
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 9e277c902365f6..0fef335157c97e 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6266,11 +6266,11 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
     RebuildTypeSourceInfoForDefaultSpecialMembers();
     // A function defaulted outside its class was not defaulted when its
     // declaration was instantiated, so transfer the unqualified lookups
-    // performed at its definition now. They were performed from namespace
-    // scope, so they don't need to be instantiated.
+    // performed at its definition now, if any. They were performed from
+    // namespace scope, so they don't need to be instantiated.
     if (!Function->getDefaultedOrDeletedInfo())
-      Function->setDefaultedOrDeletedInfo(
-          PatternDecl->getDefaultedOrDeletedInfo());
+      if (auto *DFI = PatternDecl->getDefaultedOrDeletedInfo())
+        Function->setDefaultedOrDeletedInfo(DFI);
     SetDeclDefaulted(Function, PatternDecl->getLocation());
   } else {
     DeclContext *DC = Function->getLexicalDeclContext();
diff --git a/clang/test/SemaCXX/GH240351.cpp b/clang/test/SemaCXX/GH240351.cpp
new file mode 100644
index 00000000000000..172e87297586cb
--- /dev/null
+++ b/clang/test/SemaCXX/GH240351.cpp
@@ -0,0 +1,69 @@
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-c++2d-extensions -verify %s
+// RUN: %clang_cc1 -std=c++2d -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-c++2d-extensions -verify -DFP_PRAGMA %s
+// RUN: %clang_cc1 -std=c++2d -fsyntax-only -verify -DFP_PRAGMA %s
+// expected-no-diagnostics
+
+// A function defaulted outside its class template is not defaulted when its
+// declaration is instantiated. Instantiating its definition while non-default
+// floating-point features are in effect used to crash.
+
+namespace optnone {
+// Instantiating an optnone function leaves its floating-point features in
+// effect for the next pending instantiation.
+template <typename = int> struct a {
+  a();
+};
+template <typename b> a<b>::a() = default;
+template <typename f, typename g> __attribute__((optnone)) void h(f, g) {}
+void i(int j) {
+  h(j, j);
+  a k;
+}
+} // namespace optnone
+
+namespace special_members {
+template <typename T> struct S {
+  constexpr S();
+  constexpr S(const S &);
+  S &operator=(const S &);
+};
+template <typename T> constexpr S<T>::S() = default;
+template <typename T> constexpr S<T>::S(const S &) = default;
+template <typename T> S<T> &S<T>::operator=(const S &) = default;
+
+template <typename T> struct D {
+  D();
+  ~D();
+};
+template <typename T> D<T>::D() = default;
+template <typename T> D<T>::~D() = default;
+
+void f() {
+#ifdef FP_PRAGMA
+#pragma clang fp contract(fast)
+#endif
+  // The constexpr constructors are instantiated here, under the pragma.
+  constexpr S<int> s;
+  constexpr S<int> t = s;
+  S<int> u;
+  u = t;
+  D<int> d;
+}
+} // namespace special_members
+
+namespace postfix {
+// The pattern of a postfix operator defaulted outside its class holds the
+// result of the prefix operator lookup, which the instantiation reuses.
+template <typename T> struct P {
+  P &operator++();
+  P operator++(int);
+};
+template <typename T> P<T> P<T>::operator++(int) = default;
+void g(P<int> p) { p++; }
+} // namespace postfix
+
+#ifdef FP_PRAGMA
+// Pending instantiations are performed with the pragma in effect.
+#pragma clang fp contract(fast)
+#endif

>From f15a5e6ae2be609a89524263ea0ff9eb6d4c2d56 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 11 Nov 2026 15:52:17 +0200
Subject: [PATCH 2/2] Record the info of functions defaulted outside class
 templates

A function defaulted outside its class template returned from
SetDeclDefaulted before it recorded the floating-point features in
effect at its definition. Its instantiation then had no info to reuse
and took the features in effect at its point of instantiation instead,
which also crashed codegen when those were strict but the function was
not.

Always record the info for such a pattern, so that its instantiation
reuses it. Also don't record it twice for a postfix operator defaulted
under non-default floating-point features: its info, including those
features, is recorded along with the lookup of its prefix operator.
---
 clang/lib/Sema/SemaDeclCXX.cpp                | 33 +++++++++++------
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  7 ++--
 .../defaulted-function-fp-features.cpp        | 37 ++++++++++++++++++-
 clang/test/SemaCXX/GH240351.cpp               | 31 +++++++++++++---
 4 files changed, 87 insertions(+), 21 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 27b90e1a85ec00..5a30442d5eb7c1 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -19575,15 +19575,24 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) {
 
   // Defer checking functions that are defaulted in a dependent context.
   if (FD->isDependentContext()) {
-    // The unqualified lookup of the prefix operator for a postfix operator
-    // defaulted outside its class can only be performed now, while we have the
-    // scope of the function-body. Instantiation reuses its result. (When it is
-    // defaulted in its class, the lookup is performed once the class is
-    // complete.)
-    if (DefKind.isPostfixOperator() &&
-        !isa<CXXRecordDecl>(FD->getLexicalDeclContext()))
-      CheckExplicitlyDefaultedPostfixOperator(S, FD,
-                                              DefKind.asPostfixOperator());
+    // A function defaulted outside its class is not defaulted when its
+    // declaration is instantiated, so record what the instantiation of its
+    // definition needs from here: the floating-point features in effect and,
+    // for a postfix operator, the unqualified lookup of the prefix operator,
+    // which can only be performed now, while we have the scope of the
+    // function-body. The info is recorded even for default floating-point
+    // features, so that the instantiation does not take those in effect at its
+    // point of instantiation. (When the function is defaulted in its class,
+    // the lookup is performed once the class is complete.)
+    if (!isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
+      if (DefKind.isPostfixOperator())
+        CheckExplicitlyDefaultedPostfixOperator(S, FD,
+                                                DefKind.asPostfixOperator());
+      else if (!FD->getDefaultedOrDeletedInfo())
+        FD->setDefaultedOrDeletedInfo(
+            FunctionDecl::DefaultedOrDeletedFunctionInfo::Create(
+                Context, /*Lookups=*/{}, CurFPFeatureOverrides()));
+    }
     return;
   }
 
@@ -19615,8 +19624,10 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) {
 
   // Only allocate DefaultedOrDeletedFunctionInfo if we actually have
   // non-default FP features to stash. This avoids memory overhead for
-  // the vast majority of defaulted functions.
-  if (!FD->getDefaultedOrDeletedInfo() &&
+  // the vast majority of defaulted functions. A postfix operator defaulted
+  // with a scope gets its info, which includes these features, when the
+  // lookup of its prefix operator is performed below.
+  if (!FD->getDefaultedOrDeletedInfo() && !(DefKind.isPostfixOperator() && S) &&
       CurFPFeatureOverrides().requiresTrailingStorage()) {
     FD->setDefaultedOrDeletedInfo(
         FunctionDecl::DefaultedOrDeletedFunctionInfo::Create(
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 0fef335157c97e..b72864aec8d51c 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6265,9 +6265,10 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
   if (PatternDecl->isDefaulted()) {
     RebuildTypeSourceInfoForDefaultSpecialMembers();
     // A function defaulted outside its class was not defaulted when its
-    // declaration was instantiated, so transfer the unqualified lookups
-    // performed at its definition now, if any. They were performed from
-    // namespace scope, so they don't need to be instantiated.
+    // declaration was instantiated, so transfer the info recorded at its
+    // definition now: the floating-point features in effect there, and any
+    // unqualified lookups, which were performed from namespace scope and so
+    // don't need to be instantiated.
     if (!Function->getDefaultedOrDeletedInfo())
       if (auto *DFI = PatternDecl->getDefaultedOrDeletedInfo())
         Function->setDefaultedOrDeletedInfo(DFI);
diff --git a/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp b/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp
index 3da4aa6976677b..563448d1959a2a 100644
--- a/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp
+++ b/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OGCG
 // RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s %}
 
 // CHECK-DAG: define {{.*}} @_ZeqRK6TargetS1_({{.*}}) [[NORMAL_ATTRS:#[0-9]+]]
@@ -11,6 +11,12 @@
 // CHECK-DAG: define {{.*}} @_ZeqRK14TemplateTargetIfES2_({{.*}}) [[NORMAL_ATTRS]]
 // CHECK-DAG: define {{.*}} @_ZeqRK20StrictTemplateTargetIdES2_({{.*}}) [[STRICT_ATTRS]]
 
+// Templates defaulted outside the class
+// CHECK-DAG: define {{.*}} @_ZNK15OutOfLineTargetIeEeqERKS0_({{.*}}) [[NORMAL_ATTRS]]
+// CHECK-DAG: fcmp oeq x86_fp80
+// CHECK-DAG: define {{.*}} @_ZNK21StrictOutOfLineTargetIeEeqERKS0_({{.*}}) [[STRICT_ATTRS]]
+// OGCG-DAG: call i1 @llvm.experimental.constrained.fcmp.f80({{.*}}, metadata !"fpexcept.strict")
+
 // --- NON-STRICT DECLARATIONS (at top of file, default FP is non-strict) ---
 
 struct Target {
@@ -36,6 +42,15 @@ struct TemplateTarget {
   friend bool operator==(const TemplateTarget&, const TemplateTarget&) = default;
 };
 
+template <typename T>
+struct OutOfLineTarget {
+  T d;
+  bool operator==(const OutOfLineTarget&) const;
+};
+
+template <typename T>
+bool OutOfLineTarget<T>::operator==(const OutOfLineTarget&) const = default;
+
 // Trigger instantiation of TemplateTarget<double>::operator== in non-strict context.
 bool test_template_non_strict(TemplateTarget<double> a, TemplateTarget<double> b) {
   return a == b;
@@ -61,6 +76,10 @@ bool test_template_strict(TemplateTarget<float> a, TemplateTarget<float> b) {
   return a == b;
 }
 
+// Instantiate OutOfLineTarget<long double>::operator== in strict context.
+// It should still be non-strict because it was defaulted in non-strict context.
+template bool OutOfLineTarget<long double>::operator==(const OutOfLineTarget<long double>&) const;
+
 // Strict declarations (must get strictfp)
 
 struct StrictTarget {
@@ -87,6 +106,16 @@ struct StrictTemplateTarget {
 };
 
 
+template <typename T>
+struct StrictOutOfLineTarget {
+  T d;
+  bool operator==(const StrictOutOfLineTarget&) const;
+};
+
+template <typename T>
+bool StrictOutOfLineTarget<T>::operator==(const StrictOutOfLineTarget&) const = default;
+
+
 // --- NON-STRICT CONTEXT AGAIN ---
 #pragma STDC FENV_ACCESS OFF
 
@@ -96,6 +125,12 @@ bool test_strict_template_non_strict(StrictTemplateTarget<double> a, StrictTempl
   return a == b;
 }
 
+// Trigger instantiation of StrictOutOfLineTarget<long double>::operator== in non-strict context.
+// It should be strict because it was defaulted in strict context.
+bool test_strict_out_of_line_non_strict(StrictOutOfLineTarget<long double> a, StrictOutOfLineTarget<long double> b) {
+  return a == b;
+}
+
 
 // CHECK-DAG: attributes [[STRICT_ATTRS]] = { {{.*}}strictfp{{.*}} }
 // CHECK-DAG: attributes [[NORMAL_ATTRS]] = { {{.*}} }
diff --git a/clang/test/SemaCXX/GH240351.cpp b/clang/test/SemaCXX/GH240351.cpp
index 172e87297586cb..9daee4136515f0 100644
--- a/clang/test/SemaCXX/GH240351.cpp
+++ b/clang/test/SemaCXX/GH240351.cpp
@@ -52,6 +52,12 @@ void f() {
 }
 } // namespace special_members
 
+#ifdef FP_PRAGMA
+// This stays in effect until the end of the file, where pending instantiations
+// are performed.
+#pragma clang fp contract(fast)
+#endif
+
 namespace postfix {
 // The pattern of a postfix operator defaulted outside its class holds the
 // result of the prefix operator lookup, which the instantiation reuses.
@@ -60,10 +66,23 @@ template <typename T> struct P {
   P operator++(int);
 };
 template <typename T> P<T> P<T>::operator++(int) = default;
-void g(P<int> p) { p++; }
-} // namespace postfix
 
-#ifdef FP_PRAGMA
-// Pending instantiations are performed with the pragma in effect.
-#pragma clang fp contract(fast)
-#endif
+// The floating-point features of a postfix operator defaulted outside its
+// class are recorded along with the result of the prefix operator lookup.
+struct M {
+  M &operator++();
+  M operator++(int);
+};
+M M::operator++(int) = default;
+
+struct N {
+  N &operator--();
+};
+N operator--(N &, int) = default;
+
+void g(P<int> p, M m, N n) {
+  p++;
+  m++;
+  n--;
+}
+} // namespace postfix



More information about the cfe-commits mailing list