[clang] Reapply "[Clang] Improve diagnostics for expansion length mismatch" (PR #121044)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 25 07:17:33 PDT 2025
================
@@ -888,31 +896,47 @@ bool Sema::CheckParameterPacksForExpansion(
// Pack comes from another template parameter. 'S<int>' is first
// instantiated, expanding the outer pack 'Outer' to <int>. The alias
// declaration is accordingly substituted, leaving the template arguments
- // as unexpanded
- // '<Pack...>'.
+ // as unexpanded '<Pack...>'.
//
// Since we have no idea of the size of '<Pack...>' until its expansion,
// we shouldn't assume its pack size for validation. However if we are
// certain that there are extra arguments beyond unexpanded packs, in
// which case the pack size is already larger than the previous expansion,
// we can complain that before instantiation.
- unsigned LeastNewPackSize = NewPackSize - PendingPackExpansionSize;
- if (PendingPackExpansionSize && LeastNewPackSize <= *NumExpansions) {
+ if (PendingPackExpansionSize && LeastNewPackSize <= *CurNumExpansions) {
ShouldExpand = false;
continue;
}
// C++0x [temp.variadic]p5:
// All of the parameter packs expanded by a pack expansion shall have
// the same number of arguments specified.
- if (HaveFirstPack)
- Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
- << FirstPack.first << Name << *NumExpansions
- << (LeastNewPackSize != NewPackSize) << LeastNewPackSize
- << SourceRange(FirstPack.second) << SourceRange(ParmPack.second);
- else
- Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
- << Name << *NumExpansions << (LeastNewPackSize != NewPackSize)
- << LeastNewPackSize << SourceRange(ParmPack.second);
+ Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
+ << FirstPack.first << Name << *CurNumExpansions
+ << (LeastNewPackSize != NewPackSize) << LeastNewPackSize
+ << SourceRange(FirstPack.second) << SourceRange(Loc);
+ return true;
+ }
+ }
+
+ // We have tried our best in the for loop to find out which outer pack
+ // expansion has a different length than the current one (by checking those
+ // Subst* nodes). However, if we fail to determine that, we'll have to
+ // complain the difference in a vague manner.
+ if (NumExpansions && CurNumExpansions &&
+ *NumExpansions != *CurNumExpansions) {
+ // If the current pack expansion contains any unresolved packs, and since
+ // they might eventually expand to match the outer expansion, we don't
+ // complain it too early.
----------------
cor3ntin wrote:
```suggestion
// diagnose it now.
```
https://github.com/llvm/llvm-project/pull/121044
More information about the cfe-commits
mailing list