Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.os.vms    |    DEC's VAX* line of computers & VMS.    |    264,096 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 263,779 of 264,096    |
|    =?UTF-8?Q?Arne_Vajh=C3=B8j?= to All    |
|    Re: Unsafe code blocks    |
|    17 Nov 25 14:55:42    |
   
   From: arne@vajhoej.dk   
      
   Ada is generally pretty good.   
      
   But I don't know if you noticed that the example gave a   
   warning.   
      
   The warning did not have an impact.   
      
   But the same warning in another context can indicate   
   huge problem.   
      
   And I was surprised.   
      
   $ type Simon1.adb   
   with Unchecked_Conversion;   
   with Ada.Text_IO, Ada.Integer_Text_IO;   
      
   use Ada.Text_IO, Ada.Integer_Text_IO;   
      
   procedure Simon1 is   
      
   type Rt is new Integer range 1..6;   
      
   function I2R is new Unchecked_Conversion(Source => Integer, Target => Rt);   
      
   V : Rt;   
   I : Integer;   
      
   begin   
    i := 9;   
    V := I2R(I);   
    if V'Valid then   
    Put(Integer(V));   
    else   
    Put("Not valid");   
    end if;   
    New_Line;   
   end Simon1;   
   $ gnat make Simon1   
   gcc -c simon1.adb   
   simon1.adb:10:01: warning: types for unchecked conversion have different   
   sizes   
   gnatbind -x simon1.ali   
   gnatlink simon1.ali   
   $ run Simon1   
    1   
   $ type Simon2.adb   
   with Unchecked_Conversion;   
   with Ada.Text_IO, Ada.Integer_Text_IO;   
      
   use Ada.Text_IO, Ada.Integer_Text_IO;   
      
   procedure Simon2 is   
      
   type Rt is new Integer range 1..6;   
   for Rt'Size use Integer'Size;   
      
   function I2R is new Unchecked_Conversion(Source => Integer, Target => Rt);   
      
   V : Rt;   
   I : Integer;   
      
   begin   
    i := 9;   
    V := I2R(I);   
    if V'Valid then   
    Put(Integer(V));   
    else   
    Put("Not valid");   
    end if;   
    New_Line;   
   end Simon2;   
   $ gnat make Simon2   
   gcc -c simon2.adb   
   gnatbind -x simon2.ali   
   gnatlink simon2.ali   
   $ run Simon2   
   Not valid   
      
   That was not what I expected from Ada.   
      
   And it is not due to VMS Alpha GNAT being an old 3.12p.   
      
   C:\Work\Ada>gnat -v   
   GNAT Community 2021 (20210519-103)   
   Copyright 1996-2021, Free Software Foundation, Inc.   
   ...   
      
   C:\Work\Ada>type Simon1.adb   
   with Unchecked_Conversion;   
   with Ada.Text_IO, Ada.Integer_Text_IO;   
      
   use Ada.Text_IO, Ada.Integer_Text_IO;   
      
   procedure Simon1 is   
      
   type Rt is new Integer range 1..6;   
      
   function I2R is new Unchecked_Conversion(Source => Integer, Target => Rt);   
      
   V : Rt;   
   I : Integer;   
      
   begin   
    i := 9;   
    V := I2R(I);   
    if V'Valid then   
    Put(Integer(V));   
    else   
    Put("Not valid");   
    end if;   
    New_Line;   
   end Simon1;   
      
   C:\Work\Ada>gnat make Simon1   
   gcc -c simon1.adb   
   simon1.adb:10:01: warning: types for unchecked conversion have different   
   sizes [-gnatwz]   
   gnatbind -x simon1.ali   
   gnatlink simon1.ali   
      
   C:\Work\Ada>Simon1   
    1   
      
   C:\Work\Ada>type Simon2.adb   
   with Unchecked_Conversion;   
   with Ada.Text_IO, Ada.Integer_Text_IO;   
      
   use Ada.Text_IO, Ada.Integer_Text_IO;   
      
   procedure Simon2 is   
      
   type Rt is new Integer range 1..6;   
   for Rt'Size use Integer'Size;   
      
   function I2R is new Unchecked_Conversion(Source => Integer, Target => Rt);   
      
   V : Rt;   
   I : Integer;   
      
   begin   
    i := 9;   
    V := I2R(I);   
    if V'Valid then   
    Put(Integer(V));   
    else   
    Put("Not valid");   
    end if;   
    New_Line;   
   end Simon2;   
      
   C:\Work\Ada>gnat make Simon2   
   gcc -c simon2.adb   
   gnatbind -x simon2.ali   
   gnatlink simon2.ali   
      
   C:\Work\Ada>Simon2   
   Not valid   
      
   Arne   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca