This patch fixes a compiler crash on a function call when validity checks on actuals are enabled (-gnatVi) and the target type is a scalar type.
Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ 2017-10-09 Ed Schonberg <schonb...@adacore.com> * exp_attr.adb (Expand_Attribute_Reference, case 'Valid): The prefix of the attribute is an object, but it may appear within a conversion. The object itself must be retrieved when generating the range test that implements the validity check on a scalar type. gcc/testsuite/ 2017-10-09 Ed Schonberg <schonb...@adacore.com> * gnat.dg/validity_check2.adb, gnat.dg/validity_check2_pkg.ads: New testcase.
Index: exp_attr.adb =================================================================== --- exp_attr.adb (revision 253546) +++ exp_attr.adb (working copy) @@ -6512,7 +6512,9 @@ begin -- The prefix of attribute 'Valid should always denote an object -- reference. The reference is either coming directly from source - -- or is produced by validity check expansion. + -- or is produced by validity check expansion. The object may be + -- wrapped in a conversion in which case the call to Unqual_Conv + -- will yield it. -- If the prefix denotes a variable which captures the value of -- an object for validation purposes, use the variable in the @@ -6523,7 +6525,7 @@ -- if not Temp in ... then if Is_Validation_Variable_Reference (Pref) then - Temp := New_Occurrence_Of (Entity (Pref), Loc); + Temp := New_Occurrence_Of (Entity (Unqual_Conv (Pref)), Loc); -- Otherwise the prefix is either a source object or a constant -- produced by validity check expansion. Generate: Index: ../testsuite/gnat.dg/validity_check2.adb =================================================================== --- ../testsuite/gnat.dg/validity_check2.adb (revision 0) +++ ../testsuite/gnat.dg/validity_check2.adb (revision 0) @@ -0,0 +1,11 @@ +-- { dg-do compile } +-- { dg-options "-gnatVi -gnatws" } + +with Validity_Check2_Pkg; use Validity_Check2_Pkg; + +procedure Validity_Check2 (R : access Rec) is +begin + if Op_Code_To_Msg (R.Code) in Valid_Msg then + raise Program_Error; + end if; +end; Index: ../testsuite/gnat.dg/validity_check2_pkg.ads =================================================================== --- ../testsuite/gnat.dg/validity_check2_pkg.ads (revision 0) +++ ../testsuite/gnat.dg/validity_check2_pkg.ads (revision 0) @@ -0,0 +1,16 @@ +with Ada.unchecked_conversion; + +package Validity_Check2_Pkg is + + type Op_Code is (One, Two, Three, Four); + + subtype Valid_Msg is Integer range 0 .. 15; + + function Op_Code_To_Msg is + new Ada.Unchecked_Conversion (Source => Op_code, Target => Valid_Msg); + + type Rec is record + Code : Op_Code; + end record; + +end Validity_Check2_Pkg;