[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
Tue Nov 5 13:47:46 PST 2024


================
@@ -3430,15 +3430,17 @@ bool Sema::SubstDefaultArgument(
     ContextRAII SavedContext(*this, FD);
     std::unique_ptr<LocalInstantiationScope> LIS;
 
-    if (ForCallExpr) {
+    FunctionDecl *PatternFD =
+        ForCallExpr
+            ? FD->getTemplateInstantiationPattern(/*ForDefinition*/ false)
+            : nullptr;
+    if (PatternFD) {
       // When instantiating a default argument due to use in a call expression,
       // an instantiation scope that includes the parameters of the callee is
       // required to satisfy references from the default argument. For example:
       //   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);
----------------
a-tarasyuk wrote:

@mizvekov thanks for the detailed feedback.

> The tests added in this PR doesn't cover the non-trivial case where it does refer to them.

I’d appreciate it if you could share an example of the non-trivial cases. This would help move forward with a new approach to resolve the issue and ensure that as many scenarios as possible are covered to prevent future crashes.



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


More information about the cfe-commits mailing list