From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 68B1938515F8 for ; Tue, 6 Sep 2022 07:15:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 68B1938515F8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x42c.google.com with SMTP id e13so14034232wrm.1 for ; Tue, 06 Sep 2022 00:15:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date; bh=PqbfTZMaLFUgIytZ4+ymGzRRqGQeDWnY0WF4+rn55VM=; b=cX3+UDVD59D4D2a2pCRpC6Oa2DxnFP7uytzK1KNj5kNDlMLJAhq9701gujJddaB44w Sjx1fp8PE7e5saK3HJqdPk6Ml9XbXcDDis5zNzcaf3X2GHNGTeg9EKy7OL9clsP3elr2 PDliB3aUiW5KUCGEqp2uns6fzYsVas+Syt0bWRWCrAQisRGJ8R1KBIIW1GBUHJD7vL2T i/ILjkyBBgZuLxCVG9Gzg+Kvuskjd9Hcm96X/PCDz9eET8s96YPi8vEvcsNxbTKAkz7i pTE8zO4l6+71hoDV3aw3P6XbpDHDC0JUYuimgvZuzYu7rhW5VwTrdqlvNZy3qUZ5jonN +vjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=PqbfTZMaLFUgIytZ4+ymGzRRqGQeDWnY0WF4+rn55VM=; b=F7BnL5Ccxp60+hwV7pZ9lATgg9YdPYakLHn0MZJDqx/TG0STKOSsePMzQ0BzZyJkYi pCZ0GhMApVmRhH70pjTpc6TTIW9Jxit2aTisFBA8gi3aB8ioOUHlEYQby2Wbbn0bqXVf kgi10K4hqQmBtod6rvFDpHpulsgJpaHC7KBIBFO02ghKsBbSbVT025ARkgThOn/fZKiv n0SE+pagtLNkPczSHkni9+Vxi7+zZntXSq2yK3BOfOkCHVgLwA0FT8UCPTIIs99SndUs TK1qzgMyS3UETs4Itk1ww4ndrVEqjOhreBTLpNqzu00GnSm8V/cGjshV9EgTUIOoEA8f J//g== X-Gm-Message-State: ACgBeo2m2oryav+86H6XGme4GyjS8jTAK17Oe+UJmfAS1didKQ2W//pz QE8vzSv6ohMx4LrsldmRM01ooli27X5+JA== X-Google-Smtp-Source: AA6agR43Wjhts0e7MgD6m+8gfAV1YFtRrPft/Ap6xQGGuONj93DLYWd/2WFCzqZW4HRnl3Xl0V273A== X-Received: by 2002:adf:e98e:0:b0:228:5f8b:cda6 with SMTP id h14-20020adfe98e000000b002285f8bcda6mr7353507wrm.601.1662448545230; Tue, 06 Sep 2022 00:15:45 -0700 (PDT) Received: from poulhies-Precision-5550 (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id l27-20020a05600c2cdb00b003a5c7a942edsm21237405wmc.28.2022.09.06.00.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 00:15:44 -0700 (PDT) Date: Tue, 6 Sep 2022 09:15:44 +0200 From: Marc =?iso-8859-1?Q?Poulhi=E8s?= To: gcc-patches@gcc.gnu.org Cc: Steve Baird Subject: [Ada] Bad Valid_Scalars result if signed int component type signed has partial view. Message-ID: <20220906071544.GA1280263@poulhies-Precision-5550> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+QahgC5+KEYLbs62" Content-Disposition: inline X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --+QahgC5+KEYLbs62 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline For an object X of a composite type, the attribute X'Valid_Scalars should return False if and only if there exists at least one invalid scalar subcomponent of X. The validity test for a scalar part may include a range test. In some cases involving a private type that is implemented as a signed integer type, this range test was incorrectly implemented using unsigned comparisons. For an enclosing object X, this could result in X'Valid_Scalars yielding the wrong Boolean result. Such an incorrect result would almost always be False, although an incorrect True result is theoretically possible (this would require that both bounds of the component subtype are negative and that the invalid component has a positive value). Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_attr.adb (Make_Range_Test): In determining which subtype's First and Last attributes are to be queried as part of a range test, call Validated_View in order to get a scalar (as opposed to private) subtype. (Attribute_Valid): In determining whether to perform a signed or unsigned comparison for a range test, call Validated_View in order to get a scalar (as opposed to private) type. Also correct a typo which, by itself, is the source of the problem reported for this ticket. --+QahgC5+KEYLbs62 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb --- a/gcc/ada/exp_attr.adb +++ b/gcc/ada/exp_attr.adb @@ -7103,7 +7103,8 @@ package body Exp_Attr is -- See separate sections below for the generated code in each case. when Attribute_Valid => Valid : declare - PBtyp : Entity_Id := Base_Type (Ptyp); + PBtyp : Entity_Id := Base_Type (Validated_View (Ptyp)); + -- The scalar base type, looking through private types Save_Validity_Checks_On : constant Boolean := Validity_Checks_On; -- Save the validity checking mode. We always turn off validity @@ -7150,21 +7151,27 @@ package body Exp_Attr is Temp := Duplicate_Subexpr (Pref); end if; - return - Make_In (Loc, - Left_Opnd => Unchecked_Convert_To (PBtyp, Temp), - Right_Opnd => - Make_Range (Loc, - Low_Bound => - Unchecked_Convert_To (PBtyp, - Make_Attribute_Reference (Loc, - Prefix => New_Occurrence_Of (Ptyp, Loc), - Attribute_Name => Name_First)), - High_Bound => - Unchecked_Convert_To (PBtyp, - Make_Attribute_Reference (Loc, - Prefix => New_Occurrence_Of (Ptyp, Loc), - Attribute_Name => Name_Last)))); + declare + Val_Typ : constant Entity_Id := Validated_View (Ptyp); + begin + return + Make_In (Loc, + Left_Opnd => Unchecked_Convert_To (PBtyp, Temp), + Right_Opnd => + Make_Range (Loc, + Low_Bound => + Unchecked_Convert_To (PBtyp, + Make_Attribute_Reference (Loc, + Prefix => + New_Occurrence_Of (Val_Typ, Loc), + Attribute_Name => Name_First)), + High_Bound => + Unchecked_Convert_To (PBtyp, + Make_Attribute_Reference (Loc, + Prefix => + New_Occurrence_Of (Val_Typ, Loc), + Attribute_Name => Name_Last)))); + end; end Make_Range_Test; -- Local variables @@ -7186,13 +7193,6 @@ package body Exp_Attr is Validity_Checks_On := False; - -- Retrieve the base type. Handle the case where the base type is a - -- private enumeration type. - - if Is_Private_Type (PBtyp) and then Present (Full_View (PBtyp)) then - PBtyp := Full_View (PBtyp); - end if; - -- Floating-point case. This case is handled by the Valid attribute -- code in the floating-point attribute run-time library. @@ -7462,7 +7462,7 @@ package body Exp_Attr is Uns : constant Boolean := Is_Unsigned_Type (Ptyp) or else (Is_Private_Type (Ptyp) - and then Is_Unsigned_Type (Btyp)); + and then Is_Unsigned_Type (PBtyp)); Size : Uint; P : Node_Id := Pref; --+QahgC5+KEYLbs62--