[COMMITTED,2/2] ada: Fix missing detection of late equality operator returning subtype of Boolean

Message ID 20250109125736.718450-2-poulhies@adacore.com
State New
Headers
Series [COMMITTED,1/2] ada: Accept predefined multiply operator for fixed point in expression function |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Marc Poulhiès Jan. 9, 2025, 12:57 p.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

In Ada 2012, the compiler fails to check that a primitive equality operator
for an untagged record type must appear before the type is frozen, when the
operator returns a subtype of Boolean.  This plugs the legality loophole but
adds the debug switch -gnatd_q to go back to the previous state.

gcc/ada/ChangeLog:

	PR ada/18765
	* debug.adb (d_q): Document new usage.
	* sem_ch6.adb (New_Overloaded_Entity): Apply the special processing
	to all equality operators whose base result type is Boolean, but do
	not enforce the new Ada 2012 freezing rule if the result type is a
	proper subtype of it and the -gnatd_q switch is specified.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/debug.adb   |  6 +++++-
 gcc/ada/sem_ch6.adb | 12 ++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gcc/ada/debug.adb b/gcc/ada/debug.adb
index 7b95fa87f02..ac3ce41dcc5 100644
--- a/gcc/ada/debug.adb
+++ b/gcc/ada/debug.adb
@@ -154,7 +154,7 @@  package body Debug is
    --  d_n
    --  d_o
    --  d_p  Ignore assertion pragmas for elaboration
-   --  d_q
+   --  d_q  Do not enforce freezing for equality operator of boolean subtype
    --  d_r  Disable the use of the return slot in functions
    --  d_s  Stop elaboration checks on synchronous suspension
    --  d_t  In LLVM-based CCG, dump LLVM IR after transformations are done
@@ -999,6 +999,10 @@  package body Debug is
    --       semantics of invariants and postconditions in both the static and
    --       dynamic elaboration models.
 
+   --  d_q  The compiler does not enforce the new freezing rule introduced for
+   --       primitive equality operators in Ada 2012 when the operator returns
+   --       a subtype of Boolean.
+
    --  d_r  The compiler does not make use of the return slot in the expansion
    --       of functions returning a by-reference type. If this use is required
    --       for these functions to return on the primary stack, then they are
diff --git a/gcc/ada/sem_ch6.adb b/gcc/ada/sem_ch6.adb
index 49d83f8d5e0..80e0c9c634c 100644
--- a/gcc/ada/sem_ch6.adb
+++ b/gcc/ada/sem_ch6.adb
@@ -12880,12 +12880,20 @@  package body Sem_Ch6 is
 
       <<Check_Inequality>>
          if Chars (S) = Name_Op_Eq
-           and then Etype (S) = Standard_Boolean
+           and then Base_Type (Etype (S)) = Standard_Boolean
            and then Present (Parent (S))
            and then not Is_Dispatching_Operation (S)
          then
             Make_Inequality_Operator (S);
-            Check_Untagged_Equality (S);
+
+            --  The freezing rule introduced in Ada 2012 was historically
+            --  not enforced for operators returning a subtype of Boolean.
+
+            if Etype (S) = Standard_Boolean
+              or else not Debug_Flag_Underscore_Q
+            then
+               Check_Untagged_Equality (S);
+            end if;
          end if;
    end New_Overloaded_Entity;