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

commit r17-785-gf3ed871fa84b42d0da695020dbf437d9b49a9a81
Author: Gary Dismukes <[email protected]>
Date:   Thu Feb 5 23:32:52 2026 +0000

    ada: Error on legal No_Return subprogram with formals subject to 
Type_Invariant
    
    The compiler reports a spurious error indicating that a subprogram with
    aspect No_Return doesn't satisfy that aspect in cases where the subprogram
    has formals whose type specifies a Type_Invariant aspect.  The need for
    invariant or postcondition checks leads to the creation of a nested
    subprogram that wraps the enclosing subprogram's statements and exception
    handler, defeating the checking that's done in Sem_Ch6.Check_Returns.
    
    The fix is to suppress generation of the _wrapped_statements subprogram,
    which is legitimate because the invariants and postconditions will not be
    executed in any case for a No_Return subprogram since they can never be
    reached.
    
    gcc/ada/ChangeLog:
    
            * contracts.adb (Expand_Subprogram_Contract): Don't call
            Build_Subprogram_Contract_Wrapper for a No_Return subprogram,
            but include any prologue declarations (such as for preconditions).

Diff:
---
 gcc/ada/contracts.adb | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/ada/contracts.adb b/gcc/ada/contracts.adb
index 301115f3c27d..d7450d116ad5 100644
--- a/gcc/ada/contracts.adb
+++ b/gcc/ada/contracts.adb
@@ -3377,13 +3377,13 @@ package body Contracts is
       --  Step 6: Construct subprogram _wrapped_statements
 
       --  When no statements are present we still need to insert contract
-      --  related declarations.
+      --  related declarations. There's also no need to create the contracts
+      --  wrapper when the subprogram is marked as not returning, since
+      --  postconditions and invariant checks won't be reached in that case.
 
-      if No (Stmts) then
+      if No (Stmts) or else No_Return (Subp_Id) then
          Prepend_List_To (Declarations (Body_Decl), Decls);
 
-      --  Otherwise, we need a wrapper
-
       else
          Build_Subprogram_Contract_Wrapper (Body_Id, Stmts, Decls, Result);
       end if;

Reply via email to