diff mbox series

[Ada] Improper extension of bounds of fixed-point type

Message ID 20181114114336.GA73945@adacore.com
State New
Headers show
Series [Ada] Improper extension of bounds of fixed-point type | expand

Commit Message

Pierre-Marie de Rodat Nov. 14, 2018, 11:43 a.m. UTC
If the given Delta of an ordinariy fixed-point type is not a machine
number and there is no specified 'Small for the type, the compiler
chooses the actual bounds of the type using the nearest model numbers
that include the given bounds, but it is free to exclude those bounds if
a size clause restricts the number of bits to use for the type. This
patch fixes an error in the case where the bounds of the type can be
chosen to be larger than the bounds specified in the type declaration:
prior to this patch the lower bounds could be chosen to be one delta
smaller that the given bound, when that given bound was smaller than the
nearest machine number,

Compiling rep2.adb must yield:

   rep2.adb:7:24:
       warning: value not in range of type "Test_Type" defined at line 4
   rep2.adb:7:24:
       warning: "Constraint_Error" will be raised at run time

----
with Ada.Text_IO; use Ada.Text_IO;
procedure Rep2 is

   type    Test_Type is delta 0.1 range 0.1 .. 100.0 with Size => 16;
   subtype Next_Type is Test_Type range 0.1 .. 100.0;

   Item : Test_Type := 0.0;                        -- Why is this allowed?
   Next : Next_Type with Address => Item'Address;

begin

   Put_Line (Item'Img & " - " & Item'Valid'Img);  -- Returns "0.0 - TRUE"
   Put_Line (Next'Img & " - " & Next'Valid'Img);  -- Returns "0.0 - FALSE"

end Rep2;

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

2018-11-14  Ed Schonberg  <schonberg@adacore.com>

gcc/ada/

	* freeze.adb (Freeze_Fixed_Point_Type): If the given low bound
	of the type is less than the nearest model number, do not expand
	the range of the type to include the model number below the
	bound. Similar adjustment if the upper bound is larger than the
	nearest model number.
diff mbox series

Patch

--- gcc/ada/freeze.adb
+++ gcc/ada/freeze.adb
@@ -8008,7 +8008,8 @@  package body Freeze is
                Set_Realval (Lo, Loval);
             end if;
 
-            --  Compute the fudged bounds. If the number is a model number,
+            --  Compute the fudged bounds. If the bound is a model number,
+            --  (or greater if given low bound, smaller if high bound)
             --  then we do nothing to include it, but we are allowed to backoff
             --  to the next adjacent model number when we exclude it. If it is
             --  not a model number then we straddle the two values with the
@@ -8016,7 +8017,7 @@  package body Freeze is
 
             Model_Num := UR_Trunc (Loval / Small) * Small;
 
-            if Loval = Model_Num then
+            if UR_Ge (Loval, Model_Num) then
                Loval_Incl_EP := Model_Num;
             else
                Loval_Incl_EP := Model_Num - Small;
@@ -8050,7 +8051,7 @@  package body Freeze is
 
             Model_Num := UR_Trunc (Hival / Small) * Small;
 
-            if Hival = Model_Num then
+            if UR_Le (Hival, Model_Num) then
                Hival_Incl_EP := Model_Num;
             else
                Hival_Incl_EP := Model_Num + Small;