comp.lang.ada
 help / color / mirror / Atom feed
From: AdaMagica <christ-usch.grein@t-online.de>
Subject: Re: A hole in Ada type safety
Date: Wed, 11 May 2011 22:51:19 -0700 (PDT)
Date: 2011-05-11T22:51:19-07:00	[thread overview]
Message-ID: <6fc0d396-40ca-4f32-8840-8091b94d9ddf@s14g2000vbi.googlegroups.com> (raw)
In-Reply-To: iqes6i$18g$1@speranza.aioe.org

Randy has already given the answer.

Let me add a further remark (but note, I'm no language lawyer):

You argue about the return statement. But there is no return statement
involved in the RM with Unchecked_Conversion, it's just compiler magic
that is being performed. (Being intrinsic, Unchecked_Conversion need
not be implemented in Ada. In fact, I think there is no code at all,
it just takes the bit pattern as is and reinterpretes it.)

Run the following test program:

package UC is

  type LP1 is limited private;

  procedure Set (X: out LP1; to: Integer);

  type LP2 is limited private;

private

  type LP1 is limited record
    I: Integer := 1234;
  end record;

  type LP2 is limited record
    I: Integer := Integer'First;
  end record;

end UC;
package body UC is

  procedure Set (X: out LP1; to: Integer) is
  begin
    X.I := to;
  end Set;

end UC;
with Ada.Text_IO;
with Ada.Unchecked_Conversion;
with UC;

procedure Test_UC is

  function LP1_LP2 is new Ada.Unchecked_Conversion (UC.LP1, UC.LP2);
  function LP2_I   is new Ada.Unchecked_Conversion (UC.LP2, Integer);

  X: UC.LP1;
  Y: constant UC.LP2    :=   LP1_LP2 (X);  -- new object built in
place
  Z:          UC.LP2 renames LP1_LP2 (X);  -- read-only view

begin
 
-- expected result
  Ada.Text_IO.Put_Line (Integer'Image (LP2_I (Y)) & Integer'Image
(LP2_I (Z)));  -- 1234 1234
  UC.Set (X, -100);
  Ada.Text_IO.Put_Line (Integer'Image (LP2_I (Y)) & Integer'Image
(LP2_I (Z)));  -- 1234-100

end Test_UC;



  parent reply	other threads:[~2011-05-12  5:51 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-30  8:41 A hole in Ada type safety Florian Weimer
2011-04-30 11:56 ` Robert A Duff
2011-04-30 15:27   ` Gavino
2011-04-30 16:16   ` Florian Weimer
2011-04-30 23:39     ` Randy Brukardt
2011-05-01 10:26       ` Florian Weimer
2011-05-03  1:40         ` Randy Brukardt
2011-05-03 16:57           ` Robert A Duff
2011-05-07  9:09           ` Florian Weimer
2011-05-07  9:28             ` Dmitry A. Kazakov
2011-05-07  9:57               ` Florian Weimer
2011-05-08  8:08                 ` Dmitry A. Kazakov
2011-05-08  8:46                   ` Florian Weimer
2011-05-08  9:32                     ` Dmitry A. Kazakov
2011-05-08 10:30                       ` Florian Weimer
2011-05-08 20:24                         ` anon
2011-05-08 21:11                           ` Simon Wright
2011-05-10  6:27                             ` anon
2011-05-10 14:39                               ` Adam Beneschan
2011-05-11 20:39                                 ` anon
2011-05-12  0:51                                   ` Randy Brukardt
2011-05-13  0:47                                     ` anon
2011-05-13  0:58                                       ` Adam Beneschan
2011-05-13  5:31                                       ` AdaMagica
2011-05-12  5:51                                   ` AdaMagica [this message]
2011-05-12 12:09                                     ` Robert A Duff
2011-05-12 14:40                                     ` Adam Beneschan
2011-05-14  0:30                                       ` Randy Brukardt
2011-05-09  7:48                         ` Dmitry A. Kazakov
2011-05-09 20:41             ` Randy Brukardt
2011-05-14 23:47     ` anon
replies disabled

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