From c1514eb060c72193a17b22467428d5e0d0b76ffb Mon Sep 17 00:00:00 2001 From: Ed Schonberg Date: Wed, 14 Nov 2018 11:40:52 +0000 Subject: [PATCH] [Ada] Improper extension of bounds of fixed-point type 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; 2018-11-14 Ed Schonberg 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. From-SVN: r266116 --- gcc/ada/ChangeLog | 8 ++++++++ gcc/ada/freeze.adb | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index dda456ea45f..ac96e954c8d 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,11 @@ +2018-11-14 Ed Schonberg + + * 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. + 2018-11-14 Hristian Kirtchev * checks.adb (Install_Primitive_Elaboration_Check): Do not diff --git a/gcc/ada/freeze.adb b/gcc/ada/freeze.adb index 4ff0d385ae4..19b9ca9ecff 100644 --- a/gcc/ada/freeze.adb +++ b/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; -- 2.30.2