[clang] [Clang] prevent assertion failure from an invalid template instantiation pattern when adding instantiated params to the scope in friend functions with defaulted params (PR #113777)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 1 09:41:50 PDT 2024


================
@@ -3437,10 +3437,10 @@ bool Sema::SubstDefaultArgument(
       //   template<typename T> void f(T a, int = decltype(a)());
       //   void g() { f(0); }
       LIS = std::make_unique<LocalInstantiationScope>(*this);
-      FunctionDecl *PatternFD = FD->getTemplateInstantiationPattern(
-          /*ForDefinition*/ false);
-      if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
-        return true;
+      if (FunctionDecl *PatternFD =
+              FD->getTemplateInstantiationPattern(/*ForDefinition*/ false))
+        if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
+          return true;
----------------
a-tarasyuk wrote:

> I'm wondering if we could avoid substituting into default arguments if we already know the friend declaration is not a definition.

@zyn0217 I’ve thought about omitting the substitution for default arguments in 

https://github.com/llvm/llvm-project/blob/e577f14b670ee2ae6bb717133310b215be4331b3/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp#L4688

or 

https://github.com/llvm/llvm-project/blob/10a1ea9b53647c1511d3cce8fa7637f8ffdb39b5/clang/lib/Sema/SemaExpr.cpp#L5515-L5518

However, I’m not certain that it’s safe to skip

https://github.com/llvm/llvm-project/blob/10a1ea9b53647c1511d3cce8fa7637f8ffdb39b5/clang/lib/Sema/SemaTemplateInstantiate.cpp#L3446-L3449

While I was investigating it, I tried to skip it early., however, for example, the following test encountered an assertion failure

https://github.com/llvm/llvm-project/blob/10a1ea9b53647c1511d3cce8fa7637f8ffdb39b5/clang/test/CodeGenCXX/mangle-lambdas.cpp#L243-L256

https://github.com/llvm/llvm-project/pull/113777


More information about the cfe-commits mailing list