]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Rework optimization skipping pragma check in object declaration
authorMarc Poulhiès <poulhies@adacore.com>
Fri, 18 Mar 2022 09:32:41 +0000 (10:32 +0100)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 18 May 2022 08:41:03 +0000 (08:41 +0000)
When an object declaration is initialized with a type conversion:

 Var : Typ := Typ (Value);

we skip the check for Typ's predicate as it is already checked
during the type conversion.

This is not correct when Var's subtype and the target subtype of the
conversion do not statically match:

 Var : Typ := OtherTyp (Value);

In such case, we can't skip the check of Typ's predicate.

Fix minor typos in comment.

gcc/ada/

* sem_ch3.adb (Analyze_Object_Declaration): Skip predicate check
for type conversion if object's subtype and expression's subtype
statically match.
* exp_prag.adb (Expand_Pragma_Check): Typo fix in comment.

gcc/ada/exp_prag.adb
gcc/ada/sem_ch3.adb

index 86a3929466a4ebc8c327fe7fdf49ccf22935f3be..5f1308777e8256f25c476ba2b5d825f7b6a5b738 100644 (file)
@@ -285,7 +285,7 @@ package body Exp_Prag is
       --  expression is not usually the best choice here, because it points to
       --  the location of the topmost tree node, which may be an operator in
       --  the middle of the source text of the expression. For example, it gets
-      --  located on the last AND keyword in a chain of boolean expressiond
+      --  located on the last AND keyword in a chain of boolean expressions
       --  AND'ed together. It is best to put the message on the first character
       --  of the condition, which is the effect of the First_Node call here.
       --  This source location is used to build the default exception message,
index dfade0f9193e50d04e5987017a80800673f4b32d..34dac1df6246c2a6f66a83c439b61c0c1d7a6344 100644 (file)
@@ -4572,11 +4572,15 @@ package body Sem_Ch3 is
             null;
 
          --  Do not generate a predicate check if the initialization expression
-         --  is a type conversion because the conversion has been subjected to
-         --  the same check. This is a small optimization which avoid redundant
+         --  is a type conversion whose target subtype statically matches the
+         --  object's subtype because the conversion has been subjected to the
+         --  same check. This is a small optimization which avoids redundant
          --  checks.
 
-         elsif Present (E) and then Nkind (E) = N_Type_Conversion then
+         elsif Present (E)
+           and then Nkind (E) in N_Type_Conversion
+           and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
+         then
             null;
 
          else