]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Error on legal No_Return subprogram with formals subject to Type_Invariant
authorGary Dismukes <dismukes@adacore.com>
Thu, 5 Feb 2026 23:32:52 +0000 (23:32 +0000)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 26 May 2026 08:38:21 +0000 (10:38 +0200)
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).

gcc/ada/contracts.adb

index 301115f3c27dfb2114510f8aac748297134589bb..d7450d116ad58f1d449fb96276240eabb3d34ea3 100644 (file)
@@ -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;