public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Warning on out-of-range value for default-initialized scalar object
@ 2018-01-11  9:22 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2018-01-11  9:22 UTC (permalink / raw)
  To: gcc-patches; +Cc: Gary Dismukes

[-- Attachment #1: Type: text/plain, Size: 1080 bytes --]

When an object of a type with a specified Default_Value aspect is
declared without an explicit initial value and the default value is
out of range of the objects subtype, the compiler now issues a warning
that Constraint_Error will be raised due to an out-of-range value.

The test below must output a compiler warning as follows:

$ gcc -c -gnatj70 default_warning.adb
default_warning.adb:7:08: warning: value not in range of type
                          "Subint" defined at line 5,
                          "Constraint_Error" will be raised at run
                          time

procedure Default_Warning is

   type Int_With_Default is new Integer with Default_Value => 0;

   subtype Subint is Int_With_Default range 1 .. 100;

   S : Subint;

begin
   null;
end Default_Warning;

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

2018-01-11  Gary Dismukes  <dismukes@adacore.com>

gcc/ada/

	* exp_ch3.adb (Default_Initialize_Object): Call New_Copy_Tree on the
	result of Get_Simple_Init_Value and pass the source location of the
	object declaration's object_definition.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 541 bytes --]

--- gcc/ada/exp_ch3.adb
+++ gcc/ada/exp_ch3.adb
@@ -6052,7 +6052,10 @@ package body Exp_Ch3 is
            and then not Has_Init_Expression (N)
          then
             Set_No_Initialization (N, False);
-            Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
+            Set_Expression
+              (N, New_Copy_Tree
+                    (Get_Simple_Init_Val (Typ, N, Esize (Def_Id)),
+                     New_Sloc => Sloc (Obj_Def)));
             Analyze_And_Resolve (Expression (N), Typ);
          end if;
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-01-11  9:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-11  9:22 [Ada] Warning on out-of-range value for default-initialized scalar object Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).