From patchwork Mon Aug 12 09:02:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1145567 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-506662-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="iF3LP8jJ"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 466VXg5KFBz9sP8 for ; Mon, 12 Aug 2019 19:14:27 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=h8qjDSYk9sThrxnVDf3st+3EN7w5Erpsk27t1zmcvWFC8IHHXF /rMGSxx9Naua4J5zs9NjXRaLwMMBQuJGWUdB/Shv4IUekLG1FhdMolDUhOufbTG5 ahuIyAaAiv4WIBcQPgAwYJ9QaP1WNyKciQbPmeoTcTzBQmA8x8hCKnKZM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=QiARrRyqzuBrnwPu/1v8KsKVreY=; b=iF3LP8jJwdUkRORRXZbI GzsoIU1vkZC6byAT6Bji6Npn2VSaFnS93fzWzCPjl7hMHqZ7eB3a80GrHIu3Qp7E YtgUxQNTwijZ1bGY5XPzCwWYfXPMSmwHb9Mb5FCflBJ5QpA1EFQm+wQ2ouE2a1MW YqN/mIRMMHU21+AbBGIogZY= Received: (qmail 114558 invoked by alias); 12 Aug 2019 09:10:22 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 49470 invoked by uid 89); 12 Aug 2019 09:02:58 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=constitute X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 12 Aug 2019 09:02:55 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hx6E8-0005rc-Oh for gcc-patches@gcc.gnu.org; Mon, 12 Aug 2019 05:02:54 -0400 Received: from rock.gnat.com ([205.232.38.15]:51666) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hx6E8-0005rP-JB for gcc-patches@gcc.gnu.org; Mon, 12 Aug 2019 05:02:52 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 061DA560C0; Mon, 12 Aug 2019 05:02:50 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ixi0O68RPXNT; Mon, 12 Aug 2019 05:02:49 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id E649D11647A; Mon, 12 Aug 2019 05:02:49 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id E514B6AF; Mon, 12 Aug 2019 05:02:49 -0400 (EDT) Date: Mon, 12 Aug 2019 05:02:49 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Justin Squirek Subject: [Ada] Inconsistent compile time Constraint_Error warning Message-ID: <20190812090249.GA22550@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 205.232.38.15 X-IsSubscribed: yes This patch corrects several bugs within the compiler which led to inconsistent handling of compile time Constraint_Errors. Notibly, subtype out of range checks which are only out of range of the subtype must be warnings while out of range checks where the value is out of range of the base type must be an error. Also, type conversions and qualified expressions on literals constitute errors on any out of range value. The compiler needed many of these cases clarified. ------------ -- Source -- ------------ -- main.ads with System; package Main is type T_Enum is (Enum_1, Enum_2, Unknown) with Default_Value => Unknown; subtype T_Valid_Enum is T_Enum range Enum_1 .. Enum_2; Value : T_Valid_Enum; -- WARNING generic type T_Element is (<>); Init : T_Element; package Generic_Test is Value : T_Element := Init; end; package titi is new Generic_Test (T_Valid_Enum, Unknown); -- WARNING type My_Float is digits System.Max_Base_Digits; My_Float_Last : constant := My_Float'Last; Out_Of_Range : constant := My_Float_Last + 1.0; Flt1 : My_Float := Out_Of_Range; -- ERROR A : Positive := Positive (16#9999_9999_9999#); -- ERROR B : Positive := 16#9999_9999_9999#; -- ERROR C : Positive := 0; -- WARNING D : Positive := Positive (0); -- ERROR E : Positive := Positive'(16#9999_9999_9999#); -- ERROR F : Positive := Positive'(0); -- ERROR end; ----------------- -- Compilation -- ----------------- $ gnatmake -q -gnatw_a main.adb main.ads:9:12: warning: value not in range of type "T_Valid_Enum" defined at line 7 main.ads:9:12: warning: "Constraint_Error" will be raised at run time main.ads:18:52: warning: value not in range of type "T_Element" defined at line 12, instance at line 18 main.ads:18:52: warning: "Constraint_Error" will be raised at run time main.ads:25:23: value not in range of type "My_Float" defined at line 20 main.ads:25:23: static expression fails Constraint_Check main.ads:27:19: value not in range of type "Standard.Positive" main.ads:27:19: static expression fails Constraint_Check main.ads:28:19: value not in range of type "Standard.Positive" main.ads:28:19: static expression fails Constraint_Check main.ads:29:19: warning: value not in range of type "Standard.Positive" main.ads:29:19: warning: "Constraint_Error" will be raised at run time main.ads:30:19: value not in range of type "Standard.Positive" main.ads:30:19: static expression fails Constraint_Check main.ads:31:27: value not in range of type "Standard.Positive" main.ads:31:27: static expression fails Constraint_Check main.ads:32:27: value not in range of type "Standard.Positive" main.ads:32:27: static expression fails Constraint_Check gnatmake: "main.ads" compilation error Tested on x86_64-pc-linux-gnu, committed on trunk 2019-08-12 Justin Squirek gcc/ada/ * sem_eval.adb (Check_Non_Static_Context): Add a condition to determine if a range violation constitues a warning or an error. (Out_Of_Range): Add a condition to determine if a range violation constitues a warning or an error. --- gcc/ada/sem_eval.adb +++ gcc/ada/sem_eval.adb @@ -562,23 +562,31 @@ package body Sem_Eval is elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then Out_Of_Range (N); - -- Give warning if outside subtype (where one or both of the bounds of - -- the subtype is static). This warning is omitted if the expression - -- appears in a range that could be null (warnings are handled elsewhere - -- for this case). + -- Give a warning or error on the value outside the subtype. A + -- warning is omitted if the expression appears in a range that could + -- be null (warnings are handled elsewhere for this case). elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then if Is_In_Range (N, T, Assume_Valid => True) then null; elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then - -- Ignore out of range values for System.Priority in CodePeer -- mode since the actual target compiler may provide a wider -- range. if CodePeer_Mode and then T = RTE (RE_Priority) then Set_Do_Range_Check (N, False); + + -- Determine if the out of range violation constitutes a warning + -- or an error based on context according to RM 4.9 (34/3). + + elsif Nkind_In (Original_Node (N), N_Type_Conversion, + N_Qualified_Expression) + and then Comes_From_Source (Original_Node (N)) + then + Apply_Compile_Time_Constraint_Error + (N, "value not in range of}", CE_Range_Check_Failed); else Apply_Compile_Time_Constraint_Error (N, "value not in range of}<<", CE_Range_Check_Failed); @@ -5515,8 +5523,18 @@ package body Sem_Eval is -- CodePeer mode where the target runtime may have more priorities. elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then - Apply_Compile_Time_Constraint_Error - (N, "value not in range of}", CE_Range_Check_Failed); + -- Determine if the out of range violation constitutes a warning + -- or an error based on context according to RM 4.9 (34/3). + + if Nkind (Original_Node (N)) = N_Type_Conversion + and then not Comes_From_Source (Original_Node (N)) + then + Apply_Compile_Time_Constraint_Error + (N, "value not in range of}??", CE_Range_Check_Failed); + else + Apply_Compile_Time_Constraint_Error + (N, "value not in range of}", CE_Range_Check_Failed); + end if; end if; -- Here we generate a warning for the Ada 83 case, or when we are in an