https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124648
--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Jason Merrill <[email protected]>: https://gcc.gnu.org/g:13e1fc21c9ac0b3916141ade288089adc88bd910 commit r16-8692-g13e1fc21c9ac0b3916141ade288089adc88bd910 Author: Iain Sandoe <[email protected]> Date: Wed Apr 8 15:17:16 2026 +0530 c++, contracts: implicit capture in lambda contract [PR124648] We were currently accepting invalid code by allowing contract assertions to trigger implicit lambda captures contrary to: [expr.prim.lambda.closure] / p10. The solution here is to mark captures that occur within contract assertion scopes and then clear that mark if we then see a normal capture for the same entity - we must defer the error handling since the following is valid: auto f5 = [=] { contract_assert (i > 0); // OK, i is referenced elsewhere. return i; }; PR c++/124648 gcc/cp/ChangeLog: * cp-tree.h (DECL_CONTRACT_CAPTURE_P): New. * parser.cc (cp_parser_lambda_body): Scan the captures for ones were only added in contract assertion scopes. Issue errors for those found. * semantics.cc (process_outer_var_ref): Mark implicit captures that occur in contract assertion scopes. Clear the mark if the entity is subsequently captured 'normally'. (set_contract_capture_flag): New. gcc/testsuite/ChangeLog: * g++.dg/contracts/cpp26/expr.prim.lambda.closure.p10.C: New test. Signed-off-by: Iain Sandoe <[email protected]> Co-authored-by: Jason Merrill <[email protected]>
