https://gcc.gnu.org/g:b1665ceab362fafb0322e201261078eedb9421ec

commit r16-8990-gb1665ceab362fafb0322e201261078eedb9421ec
Author: Gary Dismukes <[email protected]>
Date:   Wed Jan 21 23:35:56 2026 +0000

    ada: Parsing error on declare expr in expression default for generic formal 
function
    
    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.

Diff:
---
 gcc/ada/par-ch12.adb | 2 +-
 gcc/ada/par-ch4.adb  | 6 +++---
 gcc/ada/par.adb      | 8 ++++----
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gcc/ada/par-ch12.adb b/gcc/ada/par-ch12.adb
index 3b9cb3c0355d..07495c47d4a7 100644
--- a/gcc/ada/par-ch12.adb
+++ b/gcc/ada/par-ch12.adb
@@ -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");
diff --git a/gcc/ada/par-ch4.adb b/gcc/ada/par-ch4.adb
index 1eb1fe6b9024..58bac211e781 100644
--- a/gcc/ada/par-ch4.adb
+++ b/gcc/ada/par-ch4.adb
@@ -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;
diff --git a/gcc/ada/par.adb b/gcc/ada/par.adb
index c0fada1f5d1b..4fe825b57af2 100644
--- a/gcc/ada/par.adb
+++ b/gcc/ada/par.adb
@@ -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

Reply via email to