comp.lang.ada
 help / color / mirror / Atom feed
* Generic formal ">"
@ 2022-01-26 16:54 Simon Wright
  2022-01-26 18:46 ` Jeffrey R.Carter
  2022-01-26 18:48 ` AdaMagica
  0 siblings, 2 replies; 5+ messages in thread
From: Simon Wright @ 2022-01-26 16:54 UTC (permalink / raw)


I have a generic with a formal private type Element_Type and a formal
function ">" (L, R : Element_Type) return Boolean. This gives me
puzzling and compiler-dependent behaviour when I instantiate with
">" => "<".

The output from the code below is

GCC 10.1.0:

   Data'Length: 2
   Data'Length > 1: TRUE
   Integer'(Data'Length) > 1: TRUE
   Standard.">" (Data'Length, 1): TRUE
   Standard.">" (Integer'(Data'Length), 1): TRUE

GCC 11.1.0:

   Data'Length: 2
   Data'Length > 1: FALSE
   Integer'(Data'Length) > 1: TRUE
   Standard.">" (Data'Length, 1): FALSE
   Standard.">" (Integer'(Data'Length), 1): TRUE

I don't know what version of ">" will be used when the unqualified
Data'Length is involved, but I would have expected that the overridden
("<") version would be used in the Integer'(Data'Length)
version. (Sorry, I don't think overridden is the right word).

GCC 10 uses the version from package standard even when given
integer-qualified data.

GCC 11 uses the version from package standard when given
integer-qualified data, and the overriding version when not.

Is it right that Standard.">" is getting overridden?

Have the rules changed in this area?

My "fix" was to compare Data'Length >= 2!

8X-------------------
with Ada.Text_IO; use Ada.Text_IO;
procedure Gen_Cmp is
   generic
      type Element_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type range <>) of Element_Type;
      with function ">" (Left, Right : Element_Type) return Boolean is <>;
   procedure Gen (Data: in out Array_Type);

   procedure Gen (Data: in out Array_Type) is
   begin
      Put_Line ("Data'Length:" & Data'Length'Image);
      Put_Line ("Data'Length > 1: "
                  & Boolean'Image (Data'Length > 1));
      Put_Line ("Integer'(Data'Length) > 1: "
                  & Boolean'Image (Integer'(Data'Length) > 1));
      Put_Line ("Standard."">"" (Data'Length, 1): "
                  & Boolean'Image (Standard.">" (Data'Length, 1)));
      Put_Line ("Standard."">"" (Integer'(Data'Length), 1): "
                  & Boolean'Image (Standard.">" (Integer'(Data'Length), 1)));
   end Gen;

   type Alpha is
     (A, B, C, D, E, F, G, H, I, J, K, L, M,
      N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

   type My_Array is array (Alpha range <>) of Integer;

   procedure Chk_Down is new Gen (Element_Type => Integer,
                                  Index_Type   => Alpha,
                                  Array_Type   => My_Array,
                                  ">"          => "<");

   Data : My_Array (A .. B);
begin
   Chk_Down (Data);
end Gen_Cmp;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Generic formal ">"
  2022-01-26 16:54 Generic formal ">" Simon Wright
@ 2022-01-26 18:46 ` Jeffrey R.Carter
  2022-01-26 18:48 ` AdaMagica
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey R.Carter @ 2022-01-26 18:46 UTC (permalink / raw)


On 2022-01-26 17:54, Simon Wright wrote:
> 
> GCC 10.1.0:
> 
>     Data'Length: 2
>     Data'Length > 1: TRUE
>     Integer'(Data'Length) > 1: TRUE
>     Standard.">" (Data'Length, 1): TRUE
>     Standard.">" (Integer'(Data'Length), 1): TRUE
> 
> GCC 11.1.0:
> 
>     Data'Length: 2
>     Data'Length > 1: FALSE
>     Integer'(Data'Length) > 1: TRUE
>     Standard.">" (Data'Length, 1): FALSE
>     Standard.">" (Integer'(Data'Length), 1): TRUE

This looks like an error introduced in V11. 'Length is universal_integer, which 
has no primitive subprograms. Within the generic, Element_Type and Integer are 
distinct types, and Element_Type is not a numeric type. So Data'Length should 
not be converted to Element_Type, and the ">" defined for Element_Type should 
not be invoked. This is also true for Integer'(Data'Length).

-- 
Jeff Carter
"Son of a window-dresser."
Monty Python & the Holy Grail
12

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Generic formal ">"
  2022-01-26 16:54 Generic formal ">" Simon Wright
  2022-01-26 18:46 ` Jeffrey R.Carter
@ 2022-01-26 18:48 ` AdaMagica
  2022-01-26 19:00   ` AdaMagica
  1 sibling, 1 reply; 5+ messages in thread
From: AdaMagica @ 2022-01-26 18:48 UTC (permalink / raw)


Within the generic, data'length is an integer value of type universal integer. This has nothing to do with the generic parameter Element_Type, even if this is instantiated with Integer.
The predefined attribute 'Image produces a string from parameter Integer. So Data'Length is implicitly converted from universal_integer to integer. This is independent from the generic parameter Element_Type.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Generic formal ">"
  2022-01-26 18:48 ` AdaMagica
@ 2022-01-26 19:00   ` AdaMagica
  2022-01-27 12:05     ` Simon Wright
  0 siblings, 1 reply; 5+ messages in thread
From: AdaMagica @ 2022-01-26 19:00 UTC (permalink / raw)


GNAT CE 2021 produces the wrong result. You should definitely report this.

Data'Length: 2
Data'Length > 1: FALSE
Integer'(Data'Length) > 1: TRUE
Standard.">" (Data'Length, 1): FALSE
Standard.">" (Integer'(Data'Length), 1): TRUE
[2022-01-26 19:53:10] process terminated successfully, elapsed time: 02.39s

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Generic formal ">"
  2022-01-26 19:00   ` AdaMagica
@ 2022-01-27 12:05     ` Simon Wright
  0 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2022-01-27 12:05 UTC (permalink / raw)


AdaMagica <christ-usch.grein@t-online.de> writes:

> GNAT CE 2021 produces the wrong result. You should definitely report this.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104258

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-27 12:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-26 16:54 Generic formal ">" Simon Wright
2022-01-26 18:46 ` Jeffrey R.Carter
2022-01-26 18:48 ` AdaMagica
2022-01-26 19:00   ` AdaMagica
2022-01-27 12:05     ` Simon Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox