https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117431

            Bug ID: 117431
           Summary: [contracts] contracts on lambdas are sometime ignored
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: iains at gcc dot gnu.org
          Reporter: iains at gcc dot gnu.org
  Target Milestone: ---

consider:
int foo (int x)
{
  auto f1 = [] (int y)
    [[pre: y > 0]] { return y * y; };
  return f1 (x);
}

int main ()
{
  return foo (-2);
}

---
At present, this will no build.

The reason is that the contract parsing is deferred, but never complete for the
lambda.  Then, when building the contract check function, contracts which are
deferred are ignored.  Net result; we fail to build a pre-check function (and
if we try to build the checks inline, that also fails because the machinery
does no understand deferred_parse trees).


-----

testing the following as a short-term fix:

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index d4c633dd6e78..39be950c97ac 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -12259,6 +12259,14 @@ cp_parser_lambda_body (cp_parser* parser, tree
lambda_expr)
        removed the need for that.  */
     cp_parser_function_body (parser, false);

+    /* We need to parse deferred contract conditions before we try to call
+       finish_function (which will try to emit the contracts).  */
+    for (tree a = DECL_ATTRIBUTES (fco); a; a = TREE_CHAIN (a))
+      {
+       if (cxx_contract_attribute_p (a))
+         cp_parser_late_contract_condition (parser, fco, a);
+      }
+
     finish_lambda_function (body);
   }

Reply via email to