From patchwork Tue Oct 2 08:08:42 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 188422 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 6DC602C00A8 for ; Tue, 2 Oct 2012 18:08:56 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1349770136; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=ySUpx5K9VUIxGQLjba32 pLpLnvk=; b=qWRpGHXtQ+lGG9QW3RVb0yj4esvC+tFybHJOmbsoVQJQ6A8skyVK 7/7EBfLvO25VhGGE5nLsuDElwqrU3VOmBS7s727Io45rkyEkKxOJUO4vLXkr7D2J lj2v0h0ByA4bsbsXw+sF66PXkADI0lHClN2R7+2rtlcCEHzUj1PFw4w= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=wIuO5SU76GPVWFkO3vHt3aG5UNSOGoWJDhWuqwFHyP5u7qLyrEHOJP4wfk9UP5 p6Lqh0jM1pL0ZzDF8AJUtioVilPgJhDba5Uy6aBK53pNNbvjWySKdCBV5xr3LFBI uEyPaSW5gMVz4Y1ArwhkzN39GCdD1BW0ddWe02ya07CK0=; Received: (qmail 26600 invoked by alias); 2 Oct 2012 08:08:50 -0000 Received: (qmail 26404 invoked by uid 22791); 2 Oct 2012 08:08:48 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, KHOP_SPAMHAUS_DROP, RCVD_IN_HOSTKARMA_NO X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 02 Oct 2012 08:08:42 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 285591C793B; Tue, 2 Oct 2012 04:08:42 -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 XRxBfpsXtnPO; Tue, 2 Oct 2012 04:08:42 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 072351C7664; Tue, 2 Oct 2012 04:08:42 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 06FF3919E3; Tue, 2 Oct 2012 04:08:42 -0400 (EDT) Date: Tue, 2 Oct 2012 04:08:42 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Warning for quantified expressions over null domains Message-ID: <20121002080841.GA28960@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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 A universal quantified expression over a null domain is True. This result will be counterintuitive for newcomers to Ada 2012, so it deserves an explicit warning. This patch also removes a spurious warning for such empty loops. The commands: gnatmake -q -gnat12 alfa alfa must yield: alfa.adb:6:29: warning: universal quantified expression over a null range has value True alfa.adb:7:29: warning: existential quantified expression over a null range has value False TRUE FALSE --- with Ada.Text_IO; use Ada.Text_IO; procedure Alfa is type zzz is array (Integer range <>) of Integer; B : zzz (2 .. 1) := (others => 0); begin Put_Line (Boolean'Image (for all E of B => E = 1)); Put_Line (Boolean'Image (for some E of B => E = 1)); end Alfa; --- Tested on x86_64-pc-linux-gnu, committed on trunk 2012-10-02 Ed Schonberg * sem_ch4.adb (Analyze_Quantified_Expression): If the iterator in a quantified expression is statically known to be null (e.g. a array with an empty index type) emit a warning. Index: sem_ch4.adb =================================================================== --- sem_ch4.adb (revision 191900) +++ sem_ch4.adb (working copy) @@ -3404,6 +3404,38 @@ procedure Analyze_Quantified_Expression (N : Node_Id) is QE_Scop : Entity_Id; + function Is_Empty_Range (Typ : Entity_Id) return Boolean; + -- If the iterator is part of a quantified expression, and the range is + -- known to be statically empty, emit a warning and replace expression + -- with its static value. + + function Is_Empty_Range (Typ : Entity_Id) return Boolean is + Loc : constant Source_Ptr := Sloc (N); + + begin + if Is_Array_Type (Typ) + and then Size_Known_At_Compile_Time (Typ) + and then RM_Size (Typ) = 0 + then + if All_Present (N) then + Error_Msg_N ("?universal quantified expression " + & "over a null range has value True", N); + Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); + + else + Error_Msg_N ("?existential quantified expression " + & "over a null range has value False", N); + Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); + end if; + + Analyze (N); + return True; + + else + return False; + end if; + end Is_Empty_Range; + begin Check_SPARK_Restriction ("quantified expression is not allowed", N); @@ -3425,6 +3457,13 @@ if Present (Iterator_Specification (N)) then Preanalyze (Iterator_Specification (N)); + + if Is_Entity_Name (Name (Iterator_Specification (N))) + and then Is_Empty_Range (Etype (Name (Iterator_Specification (N)))) + then + return; + end if; + else Preanalyze (Loop_Parameter_Specification (N)); end if;