]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Parsing error on declare expr in expression default for generic formal function
authorGary Dismukes <dismukes@adacore.com>
Wed, 21 Jan 2026 23:35:56 +0000 (23:35 +0000)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 26 May 2026 08:38:17 +0000 (10:38 +0200)
The compiler incorrectly issues syntax errors for a generic formal function
with default given by an expression (a GNAT extension) when the expression
is a declare_expression.  This is fixed by calling P_Expression_If_OK rather
than P_Expression to parse the expression, which properly handles parsing
declare_expressions without parentheses (but still requires parentheses to
enclose the expression).

gcc/ada/ChangeLog:

* par.adb (P_Expression_If_OK): Add declare expression to spec comment.
* par-ch4.adb (P_Expression_If_OK): Add declare expression to comments.
* par-ch12.adb (P_Formal_Subprogram_Declaration): Call
P_Expression_If_OK rather than P_Expression.

gcc/ada/par-ch12.adb
gcc/ada/par-ch4.adb
gcc/ada/par.adb

index 3b9cb3c0355d4990f4f311d0369c10e9c91ae7d4..07495c47d4a7a1849cf8337cf200589ed6c94790 100644 (file)
@@ -1213,7 +1213,7 @@ package body Ch12 is
             if Nkind (Spec_Node) = N_Function_Specification then
                Scan;  --  past "("
 
-               Set_Expression (Def_Node, P_Expression);
+               Set_Expression (Def_Node, P_Expression_If_OK);
 
                if Token /= Tok_Right_Paren then
                   Error_Msg_SC ("missing "")"" at end of expression default");
index 1eb1fe6b9024e48863883f24d634781be00094dd..58bac211e781646aad72fcb63fef0e510f17d0e8 100644 (file)
@@ -2135,19 +2135,19 @@ package body Ch4 is
    end P_Expression;
 
    --  This function is identical to the normal P_Expression, except that it
-   --  also permits the appearance of a case, conditional, or quantified
+   --  also permits the appearance of a conditional, quantified, or declare
    --  expression if the call immediately follows a left paren, and followed
    --  by a right parenthesis. These forms are allowed if these conditions
    --  are not met, but an error message will be issued.
 
    function P_Expression_If_OK return Node_Id is
    begin
-      --  Case of conditional, case or quantified expression
+      --  Case of conditional, quantified, or declare expression
 
       if Token in Tok_Case | Tok_If | Tok_For | Tok_Declare then
          return P_Unparen_Cond_Expr_Etc;
 
-      --  Normal case, not case/conditional/quantified expression
+      --  Normal case: not a conditional, quantified, or declare expression
 
       else
          return P_Expression;
index c0fada1f5d1be1ab805bee7993e1dca2c7d95e84..4fe825b57af2d1df5cf0b172b16c102e7ac9ed52 100644 (file)
@@ -839,10 +839,10 @@ function Par (Configuration_Pragmas : Boolean) return List_Id is
 
       function P_Expression_If_OK return Node_Id;
       --  Scans out an expression allowing an unparenthesized case expression,
-      --  if expression, or quantified expression to appear without enclosing
-      --  parentheses. However, if such an expression is not preceded by a left
-      --  paren, and followed by a right paren, an error message will be output
-      --  noting that parenthesization is required.
+      --  if expression, quantified expression, or declare expression to appear
+      --  without enclosing parentheses. However, if such an expression is not
+      --  preceded by a left paren, and followed by a right paren, an error
+      --  message will be output noting that parenthesization is required.
 
       function P_Expression_No_Right_Paren return Node_Id;
       --  Scans out an expression in contexts where the expression cannot be