dmpolukhin wrote:

> The missing bit was `-O1`, as soon as I've added that, I am now getting the 
> error with this commit and not getting any error without this commit. Here is 
> a proper reproducer: 
> [friends.tgz](https://github.com/user-attachments/files/17950471/friends.tgz)

Thank you for the reproducer. I was able to reproduce it with this PR. It seems 
that the root cause that deserialization may create additional forward 
declaration for friend functions (they don't have body because only primary 
declaration gets deserialized with body) but these extra declarations might be 
used as a pattern for template instantiation that results in the compilation 
error. This may happen with and without my change. I'm looking for the best way 
to fix it. I found that using canonical decl in 
[TemplateDeclInstantiator::VisitFunctionDecl](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp#L2308)
 fixes issue for your reproducer but I'm looking for general solution:
```
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp 
b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 332004055b58..30f2f57269ce 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2198,9 +2198,11 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(
 
     FunctionTemplate->setLexicalDeclContext(LexicalDC);
 
-    if (isFriend && D->isThisDeclarationADefinition()) {
+    // TODO: Find better place to resolve decl to canonical decl!
+    FunctionDecl* CanonicalDecl = D->getCanonicalDecl();
+    if (isFriend && CanonicalDecl->isThisDeclarationADefinition()) {
       FunctionTemplate->setInstantiatedFromMemberTemplate(
-                                           D->getDescribedFunctionTemplate());
+                                           
CanonicalDecl->getDescribedFunctionTemplate());
     }
   } else if (FunctionTemplate) {
     // Record this function template specialization.
```

https://github.com/llvm/llvm-project/pull/111992
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to