Skip to content

Commit 7a05e80

Browse files
autoantwortclaude
andcommitted
Keep requires-expressions after a member access on '(..)' and '[..]'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0b5c62b commit 7a05e80

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,17 @@ static bool isTrailingReturnTypeEnd(const Token *tok)
8989
// the type must follow '->' (already simplified to '.')
9090
if (!tok || tok->str() != "." || tok->originalName() != "->")
9191
return false;
92-
// a trailing return type follows the parameter list or a specifier, a member access 'p->x' follows an object
93-
return Token::Match(tok->previous(), ")|]|const|volatile|noexcept|mutable|&|&&");
92+
// a trailing return type follows a specifier or the parameter list, a member access follows an object
93+
const Token *prev = tok->previous();
94+
if (Token::Match(prev, "const|volatile|noexcept|mutable|&|&&"))
95+
return true;
96+
if (!Token::simpleMatch(prev, ")"))
97+
return false;
98+
// the '(' of a parameter list follows the function name, 'noexcept', template parameters or a lambda
99+
// introducer, the '(' of an object as in '(*p)->x' follows an operator or a keyword like 'return'.
100+
// A call result as in 'f()->x' is not told apart.
101+
const Token *beforeParen = prev->link()->previous();
102+
return Token::Match(beforeParen, "%name%|>|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await");
94103
}
95104

96105
namespace {

‎test/testsimplifytemplate.cpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6756,20 +6756,35 @@ class TestSimplifyTemplate : public TestFixture {
67566756
" auto g() -> S & requires (A<T> || B<T>) { return *this; }\n"
67576757
" auto h() -> std::vector<T> * requires A<T>;\n"
67586758
" auto i(T t) -> decltype(t) & requires A<T> || B<T>;\n"
6759+
" auto j() noexcept(true) -> int requires A<T> || B<T>;\n"
67596760
"};";
67606761
const char expected[] = "template < class T > "
67616762
"struct S { "
67626763
"auto f ( ) . int { return 0 ; } "
67636764
"auto g ( ) . S & { return * this ; } "
67646765
"auto h ( ) . std :: vector < T > * ; "
67656766
"auto i ( T t ) . decltype ( t ) & ; "
6767+
"auto j ( ) noexcept ( true ) . int ; "
67666768
"} ;";
67676769
ASSERT_EQUALS(expected, tok(code));
67686770

67696771
// 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+
const char code2[] = "template <class T> bool f(P* p, P** a) {\n"
6773+
" return p->x && requires (T t) { t.foo(); } &&\n"
6774+
" a[0]->x && requires (T t) { t.foo(); } &&\n"
6775+
" (*a)->x && requires (T t) { t.foo(); };\n"
6776+
"}";
6777+
const char expected2[] = "template < class T > bool f ( P * p , P * * a ) { "
6778+
"return p . x && requires ( T t ) { t . foo ( ) ; } && "
6779+
"a [ 0 ] . x && requires ( T t ) { t . foo ( ) ; } && "
6780+
"( * a ) . x && requires ( T t ) { t . foo ( ) ; } ; "
6781+
"}";
67726782
ASSERT_EQUALS(expected2, tok(code2));
6783+
6784+
// lambda
6785+
const char code3[] = "template <class T> void g() { auto l = [](int x) -> int requires A<T> || B<T> { return x; }; }";
6786+
const char expected3[] = "template < class T > void g ( ) { auto l ; l = [ ] ( int x ) . int { return x ; } ; }";
6787+
ASSERT_EQUALS(expected3, tok(code3));
67736788
}
67746789

67756790
void explicitBool1() {

0 commit comments

Comments
 (0)