diff mbox series

[Ada] Membership test of class-wide interface

Message ID 20180525090754.GA34810@adacore.com
State New
Headers show
Series [Ada] Membership test of class-wide interface | expand

Commit Message

Pierre-Marie de Rodat May 25, 2018, 9:07 a.m. UTC
The compiler rejects the use of a membership test when the left operand
is a class-wide interface type object and the right operand is not a
class-wide type.

Tested on x86_64-pc-linux-gnu, committed on trunk

2018-05-25  Javier Miranda  <miranda@adacore.com>

gcc/ada/

	* sem_res.adb (Resolve_Membership_Op): Allow the use of the membership
	test when the left operand is a class-wide interface and the right
	operand is not a class-wide type.
	* exp_ch4.adb (Tagged_Membership): Adding support for interface as the
	left operand.

gcc/testsuite/

	* gnat.dg/interface7.adb: New testcase.
diff mbox series

Patch

--- gcc/ada/exp_ch4.adb
+++ gcc/ada/exp_ch4.adb
@@ -13891,7 +13891,7 @@  package body Exp_Ch4 is
           Selector_Name =>
             New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
 
-      if Is_Class_Wide_Type (Right_Type) then
+      if Is_Class_Wide_Type (Right_Type) or else Is_Interface (Left_Type) then
 
          --  No need to issue a run-time check if we statically know that the
          --  result of this membership test is always true. For example,

--- gcc/ada/sem_res.adb
+++ gcc/ada/sem_res.adb
@@ -9032,7 +9032,6 @@  package body Sem_Res is
       elsif Ada_Version >= Ada_2005
         and then Is_Class_Wide_Type (Etype (L))
         and then Is_Interface (Etype (L))
-        and then Is_Class_Wide_Type (Etype (R))
         and then not Is_Interface (Etype (R))
       then
          return;

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/interface7.adb
@@ -0,0 +1,16 @@ 
+--  { dg-do compile }
+
+procedure Interface7 is
+   type I_Type is interface;
+
+   type A1_Type is tagged null record;
+   type A2_Type is new A1_Type and I_Type with null record;
+
+   procedure Test (X : I_Type'Class) is
+   begin
+      if X in A2_Type then   --  Test
+         null;
+      end if;
+   end Test;
+
+begin null; end;