From 20418f4b8e77ad80a003a22d3fab0af6a3de3123 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Tue, 1 Dec 2026 15:37:13 +0100 Subject: [PATCH 1/4] Fix #15296 internalAstError: requires-clause after trailing return type Co-Authored-By: Claude Opus 5.5 --- lib/templatesimplifier.cpp | 24 +++++++++++++++++++++++- test/testsimplifytemplate.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d28b350ea8b..12d253b95c9 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -63,6 +63,27 @@ static Token *skipRequires(Token *tok) return tok; } +// is tok the last token of a trailing return type, as in 'auto f() -> T & requires C'? +static bool isTrailingReturnTypeEnd(const Token *tok) +{ + while (Token::Match(tok, "%name%|::|&|&&|*|>|)")) { + if (tok->str() == ">") { + tok = tok->findOpeningBracket(); + if (!tok) + return false; + } else if (tok->str() == ")") { + if (!Token::simpleMatch(tok->link()->previous(), "decltype")) + return false; + tok = tok->link()->previous(); + } + tok = tok->previous(); + } + if (!tok || tok->str() != "." || tok->originalName() != "->") + return false; + // a trailing return type follows the parameter list or a specifier, a member access 'p->x' follows an object + return Token::Match(tok->previous(), ")|]|const|volatile|noexcept|mutable|&|&&"); +} + namespace { class FindToken { public: @@ -3840,7 +3861,8 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime) // Remove concepts/requires // TODO concepts are not removed yet for (Token *tok = mTokenList.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|(")) + if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|(") && + !(Token::Match(tok, "%name%|&|&&|* requires %name%|(") && isTrailingReturnTypeEnd(tok))) continue; const Token* end = skipRequires(tok->next()); if (end) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 407649e0c7f..b66230cbb62 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -315,6 +315,7 @@ class TestSimplifyTemplate : public TestFixture { TEST_CASE(requires4); TEST_CASE(requires5); TEST_CASE(requires6); + TEST_CASE(requires7); TEST_CASE(explicitBool1); TEST_CASE(explicitBool2); @@ -6748,6 +6749,29 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } + void requires7() { // #15296 + const char code[] = "template \n" + "struct S {\n" + " auto f() -> int requires A || B { return 0; }\n" + " auto g() -> S & requires (A || B) { return *this; }\n" + " auto h() -> std::vector * requires A;\n" + " auto i(T t) -> decltype(t) & requires A || B;\n" + "};"; + const char expected[] = "template < class T > " + "struct S { " + "auto f ( ) . int { return 0 ; } " + "auto g ( ) . S & { return * this ; } " + "auto h ( ) . std :: vector < T > * ; " + "auto i ( T t ) . decltype ( t ) & ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + + // requires-expression after a member access + const char code2[] = "template bool f(P* p) { return p->x && requires (T t) { t.foo(); }; }"; + const char expected2[] = "template < class T > bool f ( P * p ) { return p . x && requires ( T t ) { t . foo ( ) ; } ; }"; + ASSERT_EQUALS(expected2, tok(code2)); + } + void explicitBool1() { const char code[] = "class Fred { explicit(true) Fred(int); };\n"; ASSERT_EQUALS("class Fred { explicit Fred ( int ) ; } ;", tok(code)); From 0b5c62b4b877d3c5f6d65252fcb49358e5a1ae5f Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Tue, 1 Dec 2026 15:49:44 +0100 Subject: [PATCH 2/4] Explain isTrailingReturnTypeEnd Co-Authored-By: Claude Opus 5.5 --- lib/templatesimplifier.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 12d253b95c9..dcd7a4b7b22 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -66,18 +66,27 @@ static Token *skipRequires(Token *tok) // is tok the last token of a trailing return type, as in 'auto f() -> T & requires C'? static bool isTrailingReturnTypeEnd(const Token *tok) { + // It is if the tokens from tok backwards form a type that starts right after a '->', and that + // '->' comes after the parameter list (or a specifier following it). A '->' after an object, + // as in 'p->x', is a member access instead. + + // walk back over the type: names, '::', '&', '&&', '*', template arguments '<..>' and 'decltype(..)'. + // The loop stops at the first token that cannot be part of the type, the token before the type. while (Token::Match(tok, "%name%|::|&|&&|*|>|)")) { if (tok->str() == ">") { + // skip the template arguments ('<' and '>' are not linked yet) tok = tok->findOpeningBracket(); if (!tok) return false; } else if (tok->str() == ")") { + // only 'decltype(..)' ends a type with ')', skip it if (!Token::simpleMatch(tok->link()->previous(), "decltype")) return false; tok = tok->link()->previous(); } tok = tok->previous(); } + // the type must follow '->' (already simplified to '.') if (!tok || tok->str() != "." || tok->originalName() != "->") return false; // a trailing return type follows the parameter list or a specifier, a member access 'p->x' follows an object From 7a05e80950e0aa9009426f748f9f014cde1bfccb Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Tue, 1 Dec 2026 20:13:31 +0100 Subject: [PATCH 3/4] Keep requires-expressions after a member access on '(..)' and '[..]' Co-Authored-By: Claude Opus 5.5 --- lib/templatesimplifier.cpp | 13 +++++++++++-- test/testsimplifytemplate.cpp | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index dcd7a4b7b22..d2cd1e1dfe9 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -89,8 +89,17 @@ static bool isTrailingReturnTypeEnd(const Token *tok) // the type must follow '->' (already simplified to '.') if (!tok || tok->str() != "." || tok->originalName() != "->") return false; - // a trailing return type follows the parameter list or a specifier, a member access 'p->x' follows an object - return Token::Match(tok->previous(), ")|]|const|volatile|noexcept|mutable|&|&&"); + // a trailing return type follows a specifier or the parameter list, a member access follows an object + const Token *prev = tok->previous(); + if (Token::Match(prev, "const|volatile|noexcept|mutable|&|&&")) + return true; + if (!Token::simpleMatch(prev, ")")) + return false; + // the '(' of a parameter list follows the function name, 'noexcept', template parameters or a lambda + // introducer, the '(' of an object as in '(*p)->x' follows an operator or a keyword like 'return'. + // A call result as in 'f()->x' is not told apart. + const Token *beforeParen = prev->link()->previous(); + return Token::Match(beforeParen, "%name%|>|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await"); } namespace { diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index b66230cbb62..d27d078fe72 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -6756,6 +6756,7 @@ class TestSimplifyTemplate : public TestFixture { " auto g() -> S & requires (A || B) { return *this; }\n" " auto h() -> std::vector * requires A;\n" " auto i(T t) -> decltype(t) & requires A || B;\n" + " auto j() noexcept(true) -> int requires A || B;\n" "};"; const char expected[] = "template < class T > " "struct S { " @@ -6763,13 +6764,27 @@ class TestSimplifyTemplate : public TestFixture { "auto g ( ) . S & { return * this ; } " "auto h ( ) . std :: vector < T > * ; " "auto i ( T t ) . decltype ( t ) & ; " + "auto j ( ) noexcept ( true ) . int ; " "} ;"; ASSERT_EQUALS(expected, tok(code)); // requires-expression after a member access - const char code2[] = "template bool f(P* p) { return p->x && requires (T t) { t.foo(); }; }"; - const char expected2[] = "template < class T > bool f ( P * p ) { return p . x && requires ( T t ) { t . foo ( ) ; } ; }"; + const char code2[] = "template bool f(P* p, P** a) {\n" + " return p->x && requires (T t) { t.foo(); } &&\n" + " a[0]->x && requires (T t) { t.foo(); } &&\n" + " (*a)->x && requires (T t) { t.foo(); };\n" + "}"; + const char expected2[] = "template < class T > bool f ( P * p , P * * a ) { " + "return p . x && requires ( T t ) { t . foo ( ) ; } && " + "a [ 0 ] . x && requires ( T t ) { t . foo ( ) ; } && " + "( * a ) . x && requires ( T t ) { t . foo ( ) ; } ; " + "}"; ASSERT_EQUALS(expected2, tok(code2)); + + // lambda + const char code3[] = "template void g() { auto l = [](int x) -> int requires A || B { return x; }; }"; + const char expected3[] = "template < class T > void g ( ) { auto l ; l = [ ] ( int x ) . int { return x ; } ; }"; + ASSERT_EQUALS(expected3, tok(code3)); } void explicitBool1() { From 5e8ff8cf01c1d390c73ba980636c75df68b90525 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Wed, 2 Dec 2026 16:11:45 +0100 Subject: [PATCH 4/4] Keep requires-expressions after a member access on a cast Co-Authored-By: Claude Opus 5.5 --- lib/templatesimplifier.cpp | 7 ++++++- test/testsimplifytemplate.cpp | 10 ++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d2cd1e1dfe9..ecb02b2d427 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -99,7 +99,12 @@ static bool isTrailingReturnTypeEnd(const Token *tok) // introducer, the '(' of an object as in '(*p)->x' follows an operator or a keyword like 'return'. // A call result as in 'f()->x' is not told apart. const Token *beforeParen = prev->link()->previous(); - return Token::Match(beforeParen, "%name%|>|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await"); + if (Token::simpleMatch(beforeParen, ">")) { + // the '>' closes template parameters as in '[](U u)', or a cast as in 'static_cast(q)->x' + const Token *opening = beforeParen->findOpeningBracket(); + return opening && !Token::Match(opening->previous(), "static_cast|dynamic_cast|reinterpret_cast|const_cast"); + } + return Token::Match(beforeParen, "%name%|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await"); } namespace { diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index d27d078fe72..73c435d51c4 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -6769,15 +6769,17 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS(expected, tok(code)); // requires-expression after a member access - const char code2[] = "template bool f(P* p, P** a) {\n" + const char code2[] = "template bool f(P* p, P** a, void* q) {\n" " return p->x && requires (T t) { t.foo(); } &&\n" " a[0]->x && requires (T t) { t.foo(); } &&\n" - " (*a)->x && requires (T t) { t.foo(); };\n" + " (*a)->x && requires (T t) { t.foo(); } &&\n" + " static_cast(q)->x && requires (T t) { t.foo(); };\n" "}"; - const char expected2[] = "template < class T > bool f ( P * p , P * * a ) { " + const char expected2[] = "template < class T > bool f ( P * p , P * * a , void * q ) { " "return p . x && requires ( T t ) { t . foo ( ) ; } && " "a [ 0 ] . x && requires ( T t ) { t . foo ( ) ; } && " - "( * a ) . x && requires ( T t ) { t . foo ( ) ; } ; " + "( * a ) . x && requires ( T t ) { t . foo ( ) ; } && " + "static_cast < P * > ( q ) . x && requires ( T t ) { t . foo ( ) ; } ; " "}"; ASSERT_EQUALS(expected2, tok(code2));