From patchwork Fri Jan 7 16:27:01 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: 49701 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F91F3858421 for ; Fri, 7 Jan 2022 16:34:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F91F3858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641573268; bh=5MeSs1SHOV/Rr7iW3iiJiSpfQptieW6r+0EDD2biC5k=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=wKiq/mdOLm/g0vTUOQqRPw/Nv9luqU0Wm2jj7LcFc5hrLlyRBvCUi+ztXEBYTYM3D jZKcC1WN0HWWwV7Yjeu0zsrBVC3vC5BHuvBeJWtYGnDOTj0vp2ERtXaLUUKdZl4nvx PyiIIOjAtVFOtycSLqbbaK1Bs+tlAlUPYoBy1kYE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 252E83857C72 for ; Fri, 7 Jan 2022 16:27:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 252E83857C72 Received: by mail-wr1-x430.google.com with SMTP id e9so10547423wra.2 for ; Fri, 07 Jan 2022 08:27:03 -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=5MeSs1SHOV/Rr7iW3iiJiSpfQptieW6r+0EDD2biC5k=; b=F2M7fUycLYr2FM39aFPLmCHjd1SMypItJQWuxbBVHDH0mWrpgHHlzUdXYqwJZ1d4rj GnjtqmtfOKKSVG/pdsS+J6XxdhemuWBYJtOTdsFjNLwTHAIWQIRtZlN6xMSUF7pZm9Xr CGC+m44dkN146wLlA6TNxMKw46xsk4URIsBDvEB5i0Mr2qLNDY7GnzoR74c3YINZGtE1 q3uDlvHI1/a6spHQ6FLA6P50QnSZb4MYa0LTf2m8+DXpiPoZBATzdIlA5WVYyKrOQAK0 6cjWypbkGk4wIpBxXYhai9Wzy6ezjhfeVytyyFFBV8/qD5XU0iHm5uOJvK4W6yPXM6+b CH7A== X-Gm-Message-State: AOAM53023R+7TGYl0Di2KmDXbKcuDHsJZGZZHjD2TYdxU/pxTvRClvaw F45hBMUwvsJgi1Y+5Qyf4nYeFj22goMSsA== X-Google-Smtp-Source: ABdhPJzMMqKUZxRqJl8JnVVj5tNIRe4Yesj2cCY5V33B+mUePqdMmij/m2CWul1J3qvFckdYV2SqGw== X-Received: by 2002:adf:dd92:: with SMTP id x18mr2915584wrl.699.1641572822226; Fri, 07 Jan 2022 08:27:02 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id n14sm5493302wrf.107.2022.01.07.08.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jan 2022 08:27:01 -0800 (PST) Date: Fri, 7 Jan 2022 16:27:01 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Check scalar range in arrays constructed by concatenation Message-ID: <20220107162701.GA948162@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: Etienne Servais Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When concatenating scalars, we should check their range as in the following example: type uint8 is range 0 .. 255; type Array_Type is array (Positive range <>) of uint8; Array_1 : Array_Type := 42 & 256; This commit leads to emitting: - a warning if a constraint error is expected but the scalar fits in the base type. - an error if it does not fit in the base type Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_res.adb (Resolve_Op_Concat_Arg): Check range when concatenating scalars. 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 @@ -10185,7 +10185,7 @@ package body Sem_Res is ("\\interpretation as call yields&", Arg, Typ); Error_Msg_NE ("\\interpretation as indexing of call yields&", - Arg, Component_Type (Typ)); + Arg, Ctyp); else Error_Msg_N ("ambiguous operand for concatenation!", Arg); @@ -10208,10 +10208,30 @@ package body Sem_Res is end; end if; - Resolve (Arg, Component_Type (Typ)); + Resolve (Arg, Ctyp); if Nkind (Arg) = N_String_Literal then - Set_Etype (Arg, Component_Type (Typ)); + Set_Etype (Arg, Ctyp); + + elsif Is_Scalar_Type (Etype (Arg)) + and then Compile_Time_Known_Value (Arg) + then + -- Determine if the out-of-range violation constitutes a + -- warning or an error according to the expression base type, + -- according to Ada 2022 RM 4.9 (35/2). + + if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then + Apply_Compile_Time_Constraint_Error + (Arg, "value not in range of}", CE_Range_Check_Failed, + Ent => Base_Type (Ctyp), + Typ => Base_Type (Ctyp)); + + elsif Is_Out_Of_Range (Arg, Ctyp) then + Apply_Compile_Time_Constraint_Error + (Arg, "value not in range of}??", CE_Range_Check_Failed, + Ent => Ctyp, + Typ => Ctyp); + end if; end if; if Arg = Left_Opnd (N) then