Repository navigation
Fix #15296 internalAstError: requires-clause after trailing return type - #9099
autoantwort wants to merge 4 commits into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c91f0ab to
0b5c62b
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the case in the ticket and for several variations. However, the new check that tells a member access from a trailing return type ()|]|const|... before the ->) also matches member accesses on subscript and parenthesized objects. Two cases that the PR description does not list now get a requires-expression deleted: a[0]->x && requires (T t) { t.foo(); } and (*p)->x && requires (T t) { t.foo(); }. Only the requires (T t) part is removed, and && { t . foo ( ) ; } is left, which no longer parses as an expression. The PR's description mentions only the f()->x case. I found no other new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | a[0]->x && requires (...) {..} and (*p)->x && ... are mistaken for trailing return types |
AI review | lib/templatesimplifier.cpp:93 |
🆕 New |
@autoantwort, please fix, resolve or reply to the open and new comments: #1.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestSimplifyTemplate TestTokenizer: all passed.- Full
./testrunner -q: 5436 tests, 0 failed.
The fix works. I wrote the file below and compared base and PR with cppcheck --std=c++20 --debug:
template <class T>
struct S {
auto f() -> std::vector<std::vector<T>> & requires A<T> || B<T> { return v; }
auto g() -> int(&)[3] requires A<T> || B<T>;
auto h() const & -> T requires A<T> || B<T>;
auto k() -> typename T::type requires A<T> || B<T>;
};
template <class T> void g(T t) {
auto l = [](auto x) -> int requires A<T> || B<T> { return 0; };
auto l2 = [](auto x) mutable -> int * requires A<T> || B<T> { return 0; };
}- With the PR, the clause is removed for
f,h,k,landl2. Base gaveinternalAstErrorfor these. g(int(&)[3]) still givesinternalAstError. The PR documents this limitation.
Regression: a requires-expression is deleted after a member access.
struct P { bool x; };
template <class T> bool f1(P** a) { return a[0]->x && requires (T t) { t.foo(); }; }
template <class T> bool f2(P** p) { return (*p)->x && requires (T t) { t.foo(); }; }
template <class T> bool f3(P* p) { return p->x && requires (T t) { t.foo(); }; }
void use(P** a) { f1<int>(a); f2<int>(a); f3<int>(*a); }Command: cppcheck -q --std=c++20 --debug t2.cpp
Base output:
3: bool f2<int> ( P * * p@var9 ) { return ( * p@var9 ) . x@var10 && requires ( int t@var11 ) { t@var11 . foo ( ) ; } ; }
2: bool f1<int> ( P * * a@var12 ) { return a@var12 [ 0 ] . x@var14 && requires ( int t@var13 ) { t@var13 . foo ( ) ; } ; }
PR output:
3: bool f2<int> ( P * * p@var9 ) { return ( * p@var9 ) . x@var10 && { t . foo ( ) ; } ; }
2: bool f1<int> ( P * * a@var11 ) { return a@var11 [ 0 ] . x@var12 && { t . foo ( ) ; } ; }
f3 (p->x) is still correct.
Automated review of 0b5c62b by Claude. The pull request was built and tested locally.
| 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|&|&&"); |
There was a problem hiding this comment.
AI review
With ) and ] accepted before the ->, member accesses on subscript and parenthesized objects are also taken for trailing return types. The requires (T t) of a following requires-expression is then erased:
template <class T> bool f1(P** a) { return a[0]->x && requires (T t) { t.foo(); }; }
template <class T> bool f2(P** p) { return (*p)->x && requires (T t) { t.foo(); }; }With the PR, --debug shows return a [ 0 ] . x && { t . foo ( ) ; } ;, and ( * p ) . x && { t . foo ( ) ; } for f2. Base keeps the requires-expression. The PR description lists only f()->x as a known limitation; these two cases are new.
A stricter check would avoid them. For ), require that tok->previous()->link()->previous() is a name (or a >/] of a template or lambda introducer), not an operator, ( or return. For ], require that it closes a lambda introducer rather than a subscript. A test for these cases would also help.
There was a problem hiding this comment.
Thanks, confirmed and fixed. A ) before the -> now counts only if its ( follows a name (but not return, throw, co_return, co_yield, co_await), > or ], i.e. a function name, noexcept, template parameters or a lambda introducer. A ] before the -> no longer counts: a lambda without a parameter list cannot have a requires-clause. a[0]->x && requires (..) {..} and (*p)->x && requires (..) {..} are kept now and are part of the test. f()->x && requires (..) {..} is still not told apart, as documented.
(Written by Claude Code on behalf of @autoantwort.)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix for the ticket works, and the problem raised in C2 is fixed: a[0]->x && requires (..) {..} and (*p)->x && requires (..) {..} are kept now and are tested.
One new case is still mistaken for a trailing return type: a member access on the result of a cast, as in static_cast<P*>(q)->x && requires (T t) { t.foo(); }. The > that is accepted before the ( matches the closing > of the cast's template argument, so the PR deletes requires (T t) and leaves && { t . foo ( ) ; }. Base keeps the expression. This is similar to the documented f()->x limitation, but the description does not mention casts, and static_cast<..>(..)-> is a common pattern. I found no other new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | a[0]->x and (*p)->x followed by a requires-expression are mistaken for trailing return types |
AI review | lib/templatesimplifier.cpp:93 |
✅ Fixed |
| 2 | static_cast<P*>(q)->x && requires (..) {..} is still taken for a trailing return type |
AI review | lib/templatesimplifier.cpp:102 |
🆕 New |
@autoantwort, please fix, resolve or reply to the open and new comments: #2.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestSimplifyTemplate TestTokenizer: 0 failed.- Trailing return types now have their requires-clauses removed, and base gives
internalAstErrorfor them. These cases worked:operator(),operator<,operator[],&&-qualified functions,const int * const,int (*)(int),T::template X<int>, member templates, free functions,noexcept,requires(...)in parentheses, and template lambdas[]<class U>(U x) -> int requires .... - I checked the case from C2 again.
a[0]->x,(*p)->x,(q)->xand!(q)->xfollowed by&& requires (T t) {..}now keep the requires-expression, the same as base.
Regression: member access on the result of a cast
struct P { bool x; };
template <class T> bool f4(void* q) { return static_cast<P*>(q)->x && requires (T t) { t.foo(); }; }
void use(void* a) { f4<int>(a); }Command: cppcheck -q --std=c++20 --debug t4.cpp | grep "^2:"
Base:
2: bool f4<int> ( void * q@var4 ) { return static_cast < P * > (@exprUNIQUE q@var4 ) .@exprUNIQUE x@var5 &&@exprUNIQUE requires (@exprUNIQUE int t@var6 ) {@exprUNIQUE t@var6 .@exprUNIQUE foo (@exprUNIQUE ) ; } ; }
PR:
2: bool f4<int> ( void * q@var4 ) { return static_cast < P * > (@exprUNIQUE q@var4 ) .@exprUNIQUE x@var5 && { t .@exprUNIQUE foo (@exprUNIQUE ) ; } ; }
Automated review of 7a05e80 by Claude. The pull request was built and tested locally.
| // 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"); |
There was a problem hiding this comment.
AI review
Because > is accepted before the (, a member access on the result of a cast is still taken for a trailing return type. The requires (T t) of a following requires-expression is then erased:
template <class T> bool f4(void* q) { return static_cast<P*>(q)->x && requires (T t) { t.foo(); }; }With the PR, --debug shows return static_cast < P * > ( q ) . x && { t . foo ( ) ; } ;. Base keeps the requires-expression. The description only names the f()->x case, and static_cast/dynamic_cast/reinterpret_cast/const_cast<..>(..)-> is a common pattern. One fix: when beforeParen is >, find its opening < and reject the case if the name before it is a cast keyword. A test case for this would also help.
There was a problem hiding this comment.
Thanks, confirmed and fixed. When the ( follows a >, the token before the matching < must not be static_cast, dynamic_cast, reinterpret_cast or const_cast. static_cast<P*>(q)->x && requires (..) {..} is now part of the test. A call of a function template, as in f<P>(q)->x or std::bit_cast<P*>(q)->x, is the same case as the documented f()->x and is still not told apart. I added it to the limitations in the description.
(Written by Claude Code on behalf of @autoantwort.)
|
Test results for commit 7a05e80 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9099/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Trac: https://trac.cppcheck.net/ticket/15296
Cause:
TemplateSimplifier::simplifyTemplates()removes requires-clauses only when the token beforerequiresis),>,>>,constornoexcept. After a trailing return type it is the last token of the type (int,&,*, ...), so the clause stays and its||breaks the AST. (&&happened to work, and so did a type ending with>.)Fix: also remove the clause when the token before
requiresends a trailing return type: walking back over the type (%name%,::,&,&&,*,<..>,decltype(..)) reaches the.that was->, and that->follows a specifier (const,volatile,noexcept,mutable,&,&&) or a parameter list, i.e. a)whose(follows a name (notreturnetc.),]or a>that does not close a cast. The last check keeps a requires-expression after a member access (p->x,a[0]->x,(*p)->x,static_cast<P*>(q)->xfollowed by&& requires (T t) { .. }) untouched.Limitation: a trailing return type like
-> int(&)[3] requires ..is still not handled, andf()->x && requires (T t) { .. }(member access on a call result, alsof<P>(q)->xorstd::bit_cast<P*>(q)->x) is still mistaken for a trailing return type.Verification: the new test fails without the fix,
testrunnerpasses. The test/cfg and samples files and 18 libstdc++ C++20 headers give identical results, as does a large internal code base (17431 issues, no change). The real-code header from the ticket no longer gives the internalAstError.This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.
🤖 Generated with Claude Code