Skip to content

Commit 01c5ec4

Browse files
tsobczynskiclaude
andcommitted
Fix #15278 TemplateSimplifier: class templates nested in a namespace need one pass per level
expandTemplate adjusts the scope of a template usage in a copied body with token_scope.find_last_of(" :: "), which finds the last space or colon rather than the substring " :: ". "NS :: A1" becomes "NS ::", so the new instantiation is named "NS :: :: A0", matches no declaration, and waits for the next pass. Each level of nesting inside a namespace costs one pass, and from 11 levels the 10-pass limit leaves the innermost template uninstantiated. Use rfind(" :: "), as getTemplateInstantiations does for the same purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 35e76b5 commit 01c5ec4

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,7 @@ void TemplateSimplifier::expandTemplate(
23012301
if (prev->strAt(-1) != "::") {
23022302
// adjust for current scope
23032303
std::string token_scope = tok3->scopeInfo()->name;
2304-
const std::string::size_type end = token_scope.find_last_of(" :: ");
2304+
const std::string::size_type end = token_scope.rfind(" :: ");
23052305
if (end != std::string::npos) {
23062306
token_scope.resize(end);
23072307
if (scope.empty())

‎test/testsimplifytemplate.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class TestSimplifyTemplate : public TestFixture {
251251
TEST_CASE(template_namespace_11); // #7145
252252
TEST_CASE(template_namespace_12);
253253
TEST_CASE(template_namespace_13);
254+
TEST_CASE(template_namespace_14); // #15278
254255
TEST_CASE(template_pointer_type);
255256
TEST_CASE(template_array_type);
256257

@@ -5518,6 +5519,51 @@ class TestSimplifyTemplate : public TestFixture {
55185519
tok(code));
55195520
}
55205521

5522+
void template_namespace_14() {
5523+
// #15278: eleven levels, one more than the simplifier's pass limit, so A0
5524+
// is instantiated only if each level does not cost an extra pass
5525+
const char code[] = "namespace NS {\n"
5526+
" template<class T> struct A0 { T t; };\n"
5527+
" template<class T> struct A1 { A0<T> m; };\n"
5528+
" template<class T> struct A2 { A1<T> m; };\n"
5529+
" template<class T> struct A3 { A2<T> m; };\n"
5530+
" template<class T> struct A4 { A3<T> m; };\n"
5531+
" template<class T> struct A5 { A4<T> m; };\n"
5532+
" template<class T> struct A6 { A5<T> m; };\n"
5533+
" template<class T> struct A7 { A6<T> m; };\n"
5534+
" template<class T> struct A8 { A7<T> m; };\n"
5535+
" template<class T> struct A9 { A8<T> m; };\n"
5536+
" template<class T> struct A10 { A9<T> m; };\n"
5537+
"}\n"
5538+
"NS::A10<int> x;\n";
5539+
ASSERT_EQUALS("namespace NS { "
5540+
"struct A0<int> ; "
5541+
"struct A1<int> ; "
5542+
"struct A2<int> ; "
5543+
"struct A3<int> ; "
5544+
"struct A4<int> ; "
5545+
"struct A5<int> ; "
5546+
"struct A6<int> ; "
5547+
"struct A7<int> ; "
5548+
"struct A8<int> ; "
5549+
"struct A9<int> ; "
5550+
"struct A10<int> ; "
5551+
"} "
5552+
"NS :: A10<int> x ; "
5553+
"struct NS :: A10<int> { A9<int> m ; } ; "
5554+
"struct NS :: A9<int> { A8<int> m ; } ; "
5555+
"struct NS :: A8<int> { A7<int> m ; } ; "
5556+
"struct NS :: A7<int> { A6<int> m ; } ; "
5557+
"struct NS :: A6<int> { A5<int> m ; } ; "
5558+
"struct NS :: A5<int> { A4<int> m ; } ; "
5559+
"struct NS :: A4<int> { A3<int> m ; } ; "
5560+
"struct NS :: A3<int> { A2<int> m ; } ; "
5561+
"struct NS :: A2<int> { A1<int> m ; } ; "
5562+
"struct NS :: A1<int> { A0<int> m ; } ; "
5563+
"struct NS :: A0<int> { int t ; } ;",
5564+
tok(code));
5565+
}
5566+
55215567
void template_pointer_type() {
55225568
const char code[] = "template<class T> void foo(const T x) {}\n"
55235569
"void bar() { foo<int*>(0); }\n";

0 commit comments

Comments
 (0)