Skip to content

Commit 20418f4

Browse files
autoantwortclaude
andcommitted
Fix #15296 internalAstError: requires-clause after trailing return type
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a4fedd commit 20418f4

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ static Token *skipRequires(Token *tok)
6363
return tok;
6464
}
6565

66+
// is tok the last token of a trailing return type, as in 'auto f() -> T & requires C<T>'?
67+
static bool isTrailingReturnTypeEnd(const Token *tok)
68+
{
69+
while (Token::Match(tok, "%name%|::|&|&&|*|>|)")) {
70+
if (tok->str() == ">") {
71+
tok = tok->findOpeningBracket();
72+
if (!tok)
73+
return false;
74+
} else if (tok->str() == ")") {
75+
if (!Token::simpleMatch(tok->link()->previous(), "decltype"))
76+
return false;
77+
tok = tok->link()->previous();
78+
}
79+
tok = tok->previous();
80+
}
81+
if (!tok || tok->str() != "." || tok->originalName() != "->")
82+
return false;
83+
// a trailing return type follows the parameter list or a specifier, a member access 'p->x' follows an object
84+
return Token::Match(tok->previous(), ")|]|const|volatile|noexcept|mutable|&|&&");
85+
}
86+
6687
namespace {
6788
class FindToken {
6889
public:
@@ -3840,7 +3861,8 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
38403861
// Remove concepts/requires
38413862
// TODO concepts are not removed yet
38423863
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
3843-
if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|("))
3864+
if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|(") &&
3865+
!(Token::Match(tok, "%name%|&|&&|* requires %name%|(") && isTrailingReturnTypeEnd(tok)))
38443866
continue;
38453867
const Token* end = skipRequires(tok->next());
38463868
if (end)

‎test/testsimplifytemplate.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class TestSimplifyTemplate : public TestFixture {
315315
TEST_CASE(requires4);
316316
TEST_CASE(requires5);
317317
TEST_CASE(requires6);
318+
TEST_CASE(requires7);
318319

319320
TEST_CASE(explicitBool1);
320321
TEST_CASE(explicitBool2);
@@ -6748,6 +6749,29 @@ class TestSimplifyTemplate : public TestFixture {
67486749
ASSERT_EQUALS(expected, tok(code));
67496750
}
67506751

6752+
void requires7() { // #15296
6753+
const char code[] = "template <class T>\n"
6754+
"struct S {\n"
6755+
" auto f() -> int requires A<T> || B<T> { return 0; }\n"
6756+
" auto g() -> S & requires (A<T> || B<T>) { return *this; }\n"
6757+
" auto h() -> std::vector<T> * requires A<T>;\n"
6758+
" auto i(T t) -> decltype(t) & requires A<T> || B<T>;\n"
6759+
"};";
6760+
const char expected[] = "template < class T > "
6761+
"struct S { "
6762+
"auto f ( ) . int { return 0 ; } "
6763+
"auto g ( ) . S & { return * this ; } "
6764+
"auto h ( ) . std :: vector < T > * ; "
6765+
"auto i ( T t ) . decltype ( t ) & ; "
6766+
"} ;";
6767+
ASSERT_EQUALS(expected, tok(code));
6768+
6769+
// requires-expression after a member access
6770+
const char code2[] = "template <class T> bool f(P* p) { return p->x && requires (T t) { t.foo(); }; }";
6771+
const char expected2[] = "template < class T > bool f ( P * p ) { return p . x && requires ( T t ) { t . foo ( ) ; } ; }";
6772+
ASSERT_EQUALS(expected2, tok(code2));
6773+
}
6774+
67516775
void explicitBool1() {
67526776
const char code[] = "class Fred { explicit(true) Fred(int); };\n";
67536777
ASSERT_EQUALS("class Fred { explicit Fred ( int ) ; } ;", tok(code));

0 commit comments

Comments
 (0)