From patchwork Thu Jan 6 17:13:19 2022 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: 1576217 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=rxt2Oh4E; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JVDCJ6qF4z9sXM for ; Fri, 7 Jan 2022 04:40:28 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BD0A3858022 for ; Thu, 6 Jan 2022 17:40:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BD0A3858022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641490826; bh=IHAWiis5jVHI+CSmZBgBnvXdMg/XV2BOBpBuzg2SF8w=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=rxt2Oh4E/M02n5+DjdePboWfJORjlCl78U0612n7NF4f9N1urjQ4E/voLKVCT2/LU ht3Wgx+lhnRh+Vi1dm0NHDahc6NENM1V7P3WFH9DgER9pqG2azm7gdNa7kudEtSy/Y L908qF3w0aJ3eKMzS5YvQNMtr0H1UvoU83UfF0TQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id B15FD385802F for ; Thu, 6 Jan 2022 17:13:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B15FD385802F Received: by mail-wr1-x432.google.com with SMTP id s1so6116582wra.6 for ; Thu, 06 Jan 2022 09:13:21 -0800 (PST) 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=IHAWiis5jVHI+CSmZBgBnvXdMg/XV2BOBpBuzg2SF8w=; b=nktysowqroAleoJGhLEgq4Y3u0SzXz900Cs+DVtLB9DOsD5ZC2u58X+3kQnbTkyHlP YhcU3rODhPxsKNo2sfyQDIwzvCEuo5UxKdmigwPvA1MLd+d7Mx7C+1dF/xwhas+Xy4SD MA21WLrYS87daM8OsjPyAB/FJadvNX6RPlvJ3UB64ZDsgdfKIfkJjEXPY2gkR5+iWm19 qUmQ5zvmz3pMFp0MLeOaehEz9hKppi9p4XJAQdenZlFHeW8TvSb4VVxg3I7R91AeWX9R JgUEytlbQa1E1di0KvLmCCjaSQH2INmz7CNl/ozlUUDkJ22GIfnd9wLyG3RpaBkCLu0F LRBA== X-Gm-Message-State: AOAM532YbWjeTt9af75K8Ql+k7/8Eg1pQBECQgrtiu+lbS/Hp6HLx4Hg AbgRAB+ubnWtH9UBgOx58BtqmyqIdnvsSw== X-Google-Smtp-Source: ABdhPJzfd+RaP0+wz3YeB2Pelk/NTVxgcUo5UdLtb42mbr2CZr8vwyOKXpyTZdgP4skDsUQzk4A3bw== X-Received: by 2002:a5d:6a4f:: with SMTP id t15mr1420577wrw.272.1641489200794; Thu, 06 Jan 2022 09:13:20 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id l13sm2984900wrs.73.2022.01.06.09.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 09:13:20 -0800 (PST) Date: Thu, 6 Jan 2022 17:13:19 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Warn on subtype declaration of null range Message-ID: <20220106171319.GA2921875@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.1 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-Patchwork-Original-From: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Bob Duff Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch adds a warning on a subtype declaration with a compile-time-known range constraint that is a null range. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_res.adb (Resolve_Range): Warn on null range, unless we are inside a generic unit or an instance thereof. * sem_ch3.adb (Analyze_Subtype_Indication): Minor: avoid double negative. diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb --- a/gcc/ada/sem_ch3.adb +++ b/gcc/ada/sem_ch3.adb @@ -6048,13 +6048,13 @@ package body Sem_Ch3 is begin Analyze (T); - if R /= Error then + if R = Error then + Set_Error_Posted (R); + Set_Error_Posted (T); + else Analyze (R); Set_Etype (N, Etype (R)); Resolve (R, Entity (T)); - else - Set_Error_Posted (R); - Set_Error_Posted (T); end if; end Analyze_Subtype_Indication; diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -10754,6 +10754,30 @@ package body Sem_Res is Fold_Uint (H, Expr_Value (H), Static => True); end if; end if; + + -- If we have a compile-time-known null range, we warn, because that is + -- likely to be a mistake. (Dynamic null ranges make sense, but often + -- compile-time-known ones do not.) Warn only if this is in a subtype + -- declaration. We do this here, rather than while analyzing a subtype + -- declaration, in case we decide to expand the cases. We do not want to + -- warn in all cases, because some are idiomatic, such as an empty + -- aggregate (1 .. 0 => <>). + + -- We don't warn in generics or their instances, because there might be + -- some instances where the range is null, and some where it is not, + -- which would lead to false alarms. + + if not (Inside_A_Generic or In_Instance) + and then Comes_From_Source (N) + and then Compile_Time_Compare + (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT + and then Nkind (Parent (N)) = N_Range_Constraint + and then Nkind (Parent (Parent (N))) = N_Subtype_Indication + and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration + and then Is_OK_Static_Range (N) + then + Error_Msg_N ("null range??", N); + end if; end Resolve_Range; --------------------------