Skip to content

Commit e017bed

Browse files
committed
[Clang] Correctly construct template arguments for file-scope template template parameters
This fixes the bug introduced by 6db007a. We construct placeholder template arguments for template-template parameters to avoid mismatching argument substitution since they have different depths with their corresponding template arguments. In this case, ```cpp template <template <Concept C> class T> void foo(T<int>); ``` T lies at the depth 0, and C lies at 1. The corresponding argument, of which there is exactly one, int, is at depth 0. If we consider the argument as the outermost one, then we would end up substituting 'int' into the wrong parameter T. We used to perform such placeholder construction during the context walk-up. In the previous patch, we slipped through that inadvertently because we would walk up to the parent, which is precisely a FileContext for template-template parameters, after adding innermost arguments. Besides, this patch moves the sanity check up to the context switch. That way, we avoid dereferencing null pointers if ND is unspecified. Closes #57410.
1 parent 3fb0d8d commit e017bed

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,11 @@ Bug Fixes to C++ Support
857857
(`#64607 <https://github.com/llvm/llvm-project/issues/64607>`_)
858858
(`#64086 <https://github.com/llvm/llvm-project/issues/64086>`_)
859859

860+
- Fixed a regression where clang forgets how to substitute into constraints on template-template
861+
parameters. Fixes:
862+
(`#57410 <https://github.com/llvm/llvm-project/issues/57410>`_) and
863+
(`#76604 <https://github.com/llvm/llvm-project/issues/57410>`_)
864+
860865
Bug Fixes to AST Handling
861866
^^^^^^^^^^^^^^^^^^^^^^^^^
862867
- Fixed an import failure of recursive friend class template.

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,19 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs(
344344

345345
using namespace TemplateInstArgsHelpers;
346346
const Decl *CurDecl = ND;
347+
348+
if (!ND)
349+
CurDecl = Decl::castFromDeclContext(DC);
350+
347351
if (Innermost) {
348352
Result.addOuterTemplateArguments(const_cast<NamedDecl *>(ND),
349353
Innermost->asArray(), Final);
350-
CurDecl = Response::UseNextDecl(ND).NextDecl;
354+
if (CurDecl->getDeclContext()->isFileContext())
355+
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl))
356+
HandleDefaultTempArgIntoTempTempParam(TTP, Result);
357+
CurDecl = Response::UseNextDecl(CurDecl).NextDecl;
351358
}
352359

353-
if (!ND)
354-
CurDecl = Decl::castFromDeclContext(DC);
355-
356360
while (!CurDecl->isFileContextDecl()) {
357361
Response R;
358362
if (const auto *VarTemplSpec =

clang/test/CXX/temp/temp.arg/temp.arg.template/p3-2a.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,28 @@ struct Nothing {};
5959

6060
// FIXME: Wait the standard to clarify the intent.
6161
template<> template<> Z<Nothing> S5<Z>::V<Nothing>;
62+
63+
namespace GH57410 {
64+
65+
template<typename T>
66+
concept True = true;
67+
68+
template<typename T>
69+
concept False = false; // #False
70+
71+
template<template<True T> typename Wrapper>
72+
using Test = Wrapper<int>;
73+
74+
template<template<False T> typename Wrapper> // #TTP-Wrapper
75+
using Test = Wrapper<int>; // expected-error {{constraints not satisfied for template template parameter 'Wrapper' [with T = int]}}
76+
77+
// expected-note@#TTP-Wrapper {{'int' does not satisfy 'False'}}
78+
// expected-note@#False {{evaluated to false}}
79+
80+
template <template<False> typename T> // #TTP-foo
81+
void foo(T<int>); // expected-error {{constraints not satisfied for template template parameter 'T' [with $0 = int]}}
82+
83+
// expected-note@#TTP-foo {{'int' does not satisfy 'False'}}
84+
// expected-note@#False {{evaluated to false}}
85+
86+
}

0 commit comments

Comments
 (0)