Author: Akash Manna
Date: 2026-08-21T02:04:43+08:00
New Revision: f2951f7619cb46b93005f42da00e9a1c9e73f95b

URL: 
https://github.com/llvm/llvm-project/commit/f2951f7619cb46b93005f42da00e9a1c9e73f95b
DIFF: 
https://github.com/llvm/llvm-project/commit/f2951f7619cb46b93005f42da00e9a1c9e73f95b.diff

LOG: [Clang][Sema] Fix crash on default argument added after a parameter pack 
(#217386)

Fixes #216211

The DR1344 check that runs next locates "the
first defaulted parameter" as `getParamDecl(getMinRequiredArguments())`,
which is wrong when a parameter pack comes first: the pack is skipped by
the count but still occupies a slot. The lookup lands on the pack and
`assert(NewParam->hasDefaultArg())` fails. Without assertions, the
"makes this constructor a default constructor" error is emitted pointing
at the wrong parameter.

The DR1344 check now scans for the first parameter that actually has a
default argument, so the assertion holds and the diagnostic points at
the right parameter.

LLM tools were used for this contribution.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 3d28f1d4407a5..0ff4358574ea7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -516,6 +516,11 @@ features cannot lower the translation-unit ABI level;
 
 - Fixed an assertion during template argument deduction where a function 
parameter pack is referenced by other types in the function type. (#GH28877), 
(#GH213760)
 
+- Fixed an assertion when a redeclaration of a function template or an 
out-of-line
+  definition of a member of a class template added a default argument to a
+  parameter that follows a parameter pack (e.g.
+  `template <typename... T> S::S(T..., int = 10) {}`).  (#GH216211)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e84d35b2b5ecb..277e990f248c0 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -679,8 +679,11 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, 
FunctionDecl *Old,
                          OldSM =
                              cast<CXXMethodDecl>(Old)->getSpecialMemberKind();
     if (NewSM != OldSM) {
-      ParmVarDecl *NewParam = 
New->getParamDecl(New->getMinRequiredArguments());
-      assert(NewParam->hasDefaultArg());
+      auto It = llvm::find_if(New->parameters(), [](const ParmVarDecl *P) {
+        return P->hasDefaultArg();
+      });
+      assert(It != New->param_end());
+      ParmVarDecl *NewParam = *It;
       Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special)
           << NewParam->getDefaultArgRange() << NewSM;
       Diag(Old->getLocation(), diag::note_previous_declaration);

diff  --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp 
b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp
index 6014268a18601..091a864b28620 100644
--- a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p4.cpp
@@ -106,3 +106,25 @@ void main() {
 }
 
 } // namespace pr12724
+
+namespace GH216211 {
+
+struct S {
+  template <typename... T> S(T..., int); // expected-note{{previous template 
declaration is here}} expected-note{{previous declaration is here}}
+};
+template <typename... T>
+S::S(T..., int = 10) {} // expected-error{{cannot be added}} 
expected-error{{makes this constructor a default constructor}}
+
+struct S2 {
+  template <typename... T> S2(T..., int, int); // expected-note 2{{previous 
template declaration is here}} expected-note{{previous declaration is here}}
+};
+template <typename... T>
+S2::S2(T..., int = 1, int = 2) {} // expected-error 2{{cannot be added}} 
expected-error{{makes this constructor a default constructor}}
+
+struct S3 {
+  template <typename... T> S3(T..., int, int); // expected-note{{previous 
template declaration is here}}
+};
+template <typename... T>
+S3::S3(T..., int, int = 2) {} // expected-error{{cannot be added}}
+
+} // namespace GH216211


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to