From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id EE37B3857C67 for ; Wed, 20 Oct 2021 19:27:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EE37B3857C67 Received: by mail-lj1-x22e.google.com with SMTP id r6so362279ljg.6 for ; Wed, 20 Oct 2021 12:27:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=cgxobIxUv2VZJ3Z3V7emKNsm4cjzcK4nPihnaJ9EbNM=; b=yJWodGfBzf6F+CG00AAJgftCeBj+4adBX87nZ9GDw84O03A4Xh5NRMnzAWevil3CIs KRL4dvbryyGolUThaGDxm8/H1zIDzO+GnJw7Wg8ztUeJ8mlf0l3VeksIPlIcyC2coQ0+ WrgIbC57sbsNFqdeWm89d8R8qvi13K+u2NWWOrJ7Cz3fVmegSvAcIORFeMgrD9EfECnS nxLDWNPHbJS2/wG9lrtoLJeluwEdhGrQfcZr48kzcu+gzdWotUB2feRWuZOPF+L5zWCO gSBBtbN2JyEXFN5b+cxSlYGdrrGSvkwcITb9UdTZp6S5+ykoKg6El70AouME0t88rZsQ L0wg== X-Gm-Message-State: AOAM533aZNCnWFH7KaeUSe+A3bzpB8t6Gg5UsBeAfKaGG+lBpcXXkkzt 9XWGmO96PLso+5dneSQNpyZi9IAwHDPHEeWC X-Google-Smtp-Source: ABdhPJwOvYVpPH9rAEYJoBS+P1AGxK0G9FbnrqXN/TEayNX5MiJlkAdQtPwJpi+OVkKzuuv/SZ0lGQ== X-Received: by 2002:a05:651c:38c:: with SMTP id e12mr1032874ljp.21.1634758076870; Wed, 20 Oct 2021 12:27:56 -0700 (PDT) Received: from adacore.com ([2a02:2ab8:224:2ce:72b5:e8ff:feef:ee60]) by smtp.gmail.com with ESMTPSA id i7sm260509lfe.91.2021.10.20.12.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 12:27:56 -0700 (PDT) Date: Wed, 20 Oct 2021 19:27:55 +0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [Ada] Fix problematic conversion of real literal in static context Message-ID: <20211020192755.GA3154314@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="9amGYk9869ThD9tj" Content-Disposition: inline X-Spam-Status: No, score=-13.0 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 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Oct 2021 19:27:59 -0000 --9amGYk9869ThD9tj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This gets rid of a bogus error issued for the conversion to a static floating-point subtype of a named number which is not a machine number of this floating-point subtype but happens to be very close (or equal) to one of the nominal bounds of the subtype. This conversion may not change the value of this named number in a static context but needs to take into account the stored bounds of the subtype, which are machine numbers, to raise Constraint_Error. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_eval.adb (Eval_Type_Conversion): If the target subtype is a static floating-point subtype and the result is a real literal, consider its machine-rounded value to raise Constraint_Error. (Test_In_Range): Turn local variables into constants. --9amGYk9869ThD9tj Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/sem_eval.adb b/gcc/ada/sem_eval.adb --- a/gcc/ada/sem_eval.adb +++ b/gcc/ada/sem_eval.adb @@ -4352,7 +4352,25 @@ package body Sem_Eval is Fold_Uint (N, Expr_Value (Operand), Stat); end if; - if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then + -- If the target is a static floating-point subtype, then its bounds + -- are machine numbers so we must consider the machine-rounded value. + + if Is_Floating_Point_Type (Target_Type) + and then Nkind (N) = N_Real_Literal + and then not Is_Machine_Number (N) + then + declare + Lo : constant Node_Id := Type_Low_Bound (Target_Type); + Hi : constant Node_Id := Type_High_Bound (Target_Type); + Valr : constant Ureal := + Machine_Number (Target_Type, Expr_Value_R (N), N); + begin + if Valr < Expr_Value_R (Lo) or else Valr > Expr_Value_R (Hi) then + Out_Of_Range (N); + end if; + end; + + elsif Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then Out_Of_Range (N); end if; end Eval_Type_Conversion; @@ -7342,19 +7360,12 @@ package body Sem_Eval is elsif Compile_Time_Known_Value (N) then declare - Lo : Node_Id; - Hi : Node_Id; - - LB_Known : Boolean; - HB_Known : Boolean; + Lo : constant Node_Id := Type_Low_Bound (Typ); + Hi : constant Node_Id := Type_High_Bound (Typ); + LB_Known : constant Boolean := Compile_Time_Known_Value (Lo); + HB_Known : constant Boolean := Compile_Time_Known_Value (Hi); begin - Lo := Type_Low_Bound (Typ); - Hi := Type_High_Bound (Typ); - - LB_Known := Compile_Time_Known_Value (Lo); - HB_Known := Compile_Time_Known_Value (Hi); - -- Fixed point types should be considered as such only if flag -- Fixed_Int is set to False. --9amGYk9869ThD9tj--