From patchwork Tue Sep 5 11:08:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 137503 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp1607125vqo; Tue, 5 Sep 2023 04:13:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE4getPldsmVE52T44+XHVX1ooe/kLh0yOqw8VhgLUB9LZ7RpRdnGuSK4No7Nx+sEH/PMqm X-Received: by 2002:a17:907:2bc9:b0:9a1:f449:ebd5 with SMTP id gv9-20020a1709072bc900b009a1f449ebd5mr8833656ejc.33.1693912397252; Tue, 05 Sep 2023 04:13:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693912397; cv=none; d=google.com; s=arc-20160816; b=okIFaonLYT07pLRUk3vNnzLlDJXn9TL1Y5pyTtW1uFs90DR+i5ie0vaP3/l2cqDeve sll/onPK+OfZaBsAtMO0H4LHhka/dXKCO6i4KzjzX15g6461kWZY2g92P0UMJiwnPTPD onrtkezaMMDJWMfOEYubOupp/1Sn+Dw9n3PpUXN2NKK69Rxybt6TfMnVVIt1Qa79T12J nrGI3liPZ2rTb7x4DGg+CKpR81GOx5y39RP3bkZSNSFRGqA2ZAEFghWfBmIlNE6caJsB q03j6EdI+wZndv+z5fODcoiHbal9dPAAtZL4vLm2vg8gft7pIFBeSfuI6P7pFRD2pLIS rZNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=B1OGJMaC02ZrGMMNwLucVFO6EC8iKFjvpFU9LghVVu0=; fh=xMTG+Z2BAn45iekV7HxLGBKXrsSIb8vOWtd2ivRmbts=; b=HxDw+bzNz9ALezIBtP0eN6FvQ49c3aZ4tHJkxrBBYd3+mSgxfhDUF9rjrfgT2WwrT0 w8MlWWHxanv7AIBCY7pI1aAwbyGGxOCO3d3GjlSxKpfa2QJ9P3t7MYQ4V6Jq5J2AwUir 1jsPmQRn3yK+e4QzY1z9e5WK8hFJuq7eYn0Wzf+qXwm2MeEJIV7EosKmvgV5j3FKly7r GKor6wTAjJn+YUgYmQgDSs/54TKTwLfBjrtWxHHZXTwQRF2Vk+Oskl+MHahFVeC12wXZ poke7F9feF+WpMt9Tp9i++HaGlGeAd/UpiF8YlpAaZUhSHOZ3vppp+/T+DPrw2Ff4blX MDwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pVZBLD1r; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id io8-20020a17090780c800b009a5cc4f04d1si7464988ejc.1034.2023.09.05.04.13.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 04:13:17 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pVZBLD1r; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59C57388A413 for ; Tue, 5 Sep 2023 11:09:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59C57388A413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693912197; bh=B1OGJMaC02ZrGMMNwLucVFO6EC8iKFjvpFU9LghVVu0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=pVZBLD1rYHDpQRzPiMTkaHJ4lGReJxOJSPNT5YyxdTxa0ytcTkWLld49riSDL7wSi OAz184fkyT38f6Pwa37sfnt3ACDHL7rZHdO1eOmlIloPcJvdhLMVPvYGg7cqCDBSeI hP5nQF1x9YNRBuEYfeZRGjFddrH9n3ZkzgZ4O5sU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 155B2385C6EA for ; Tue, 5 Sep 2023 11:08:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 155B2385C6EA Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2bcbfb3705dso36583911fa.1 for ; Tue, 05 Sep 2023 04:08:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693912096; x=1694516896; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B1OGJMaC02ZrGMMNwLucVFO6EC8iKFjvpFU9LghVVu0=; b=fW5L9YDDcUBQJM5ULfjGxJoceZYo+n4AkgWjlrn4RhUY92EdvbEBW6GCcb6Em/HgXB V+dpee/2MSX3Yd9h1USWBgQCLlgv/Zz4dpWElaB2C+kDUaJGxT0ylN2bgbGRoa+o3HOi XQg/KGwUxnnG8VVg3LmhfIp1DbUvblbfm/JrrqKerhsQzE80puq93BPr2b+JXoDnohVd 8R0ET1H+KeEl2Hj8ge5oVbjtLO4vnRLjfkht3isqWtFkA5Oq19TzJNGyvQnZ2/q1EMcV KnMv78h5qEtFpj4byrk5d8Bao3hiKjSfux/xqpxq2ROyIPIYScRWO/zSXMhoVbYtx7sj blMw== X-Gm-Message-State: AOJu0YwHdawThv5cNFH3PdjKa/xo6or8pw+d5RZZS84uf3X/8raVz/ZS NiiaxadZVhSwNfiHh1AEsaHqgbrOPMQIPDE36kTXVQ== X-Received: by 2002:a05:651c:210:b0:2b9:5fd2:763a with SMTP id y16-20020a05651c021000b002b95fd2763amr8914535ljn.35.1693912095846; Tue, 05 Sep 2023 04:08:15 -0700 (PDT) Received: from poulhies-Precision-5550.lan ([2001:861:3382:1a90:20fc:79e4:455c:1075]) by smtp.gmail.com with ESMTPSA id 17-20020a05600c249100b003fbc30825fbsm16465289wms.39.2023.09.05.04.08.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 04:08:15 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Javier Miranda Subject: [COMMITTED] ada: Crash on function returning empty Ada 2022 aggregate Date: Tue, 5 Sep 2023 13:08:14 +0200 Message-Id: <20230905110814.562829-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776195885861550854 X-GMAIL-MSGID: 1776195885861550854 From: Javier Miranda The compiler crashes processing a function that returns an empty aggregate when its returned type is a record type which defined its container aggregate aspects. gcc/ada/ * exp_aggr.adb (Expand_Container_Aggregate): Report warning on infinite recursion if an empty container aggregate appears in the return statement of its Empty function. Fix typo in comment. * sem_aggr.adb (Resolve_Aggregate): Resolve Ada 2022 empty aggregate that initializes a record type that has defined its container aggregate aspects. (Resolve_Iterated_Association): Protect access to attribute Etype. * sem_ch13.adb (Resolve_Aspect_Aggregate): Fix typo in comment. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_aggr.adb | 23 ++++++++++++++++++++++- gcc/ada/sem_aggr.adb | 14 ++++++++++++++ gcc/ada/sem_ch13.adb | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb index cd5cc0b7669..cdca24b7d5d 100644 --- a/gcc/ada/exp_aggr.adb +++ b/gcc/ada/exp_aggr.adb @@ -6917,6 +6917,10 @@ package body Exp_Aggr is Siz := Aggregate_Size; + --------------------- + -- Empty function -- + --------------------- + if Ekind (Entity (Empty_Subp)) = E_Function and then Present (First_Formal (Entity (Empty_Subp))) then @@ -6984,7 +6988,7 @@ package body Exp_Aggr is Append (Init_Stat, Aggr_Code); - -- Size is dynamic: Create declaration for object, and intitialize + -- Size is dynamic: Create declaration for object, and initialize -- with a call to the null container, or an assignment to it. else @@ -7013,6 +7017,23 @@ package body Exp_Aggr is Append (Init_Stat, Aggr_Code); end if; + -- Report warning on infinite recursion if an empty container aggregate + -- appears in the return statement of its Empty function. + + if Ekind (Entity (Empty_Subp)) = E_Function + and then Nkind (Parent (N)) = N_Simple_Return_Statement + and then Is_Empty_List (Expressions (N)) + and then Is_Empty_List (Component_Associations (N)) + and then Entity (Empty_Subp) = Current_Scope + then + Error_Msg_Warn := SPARK_Mode /= On; + Error_Msg_N + ("!empty aggregate returned by the empty function of a container" + & " aggregate<<<", Parent (N)); + Error_Msg_N + ("\this will result in infinite recursion??", Parent (N)); + end if; + --------------------------- -- Positional aggregate -- --------------------------- diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb index 364217d03db..e929fea3bb6 100644 --- a/gcc/ada/sem_aggr.adb +++ b/gcc/ada/sem_aggr.adb @@ -1065,6 +1065,19 @@ package body Sem_Aggr is Resolve_Container_Aggregate (N, Typ); + -- Check Ada 2022 empty aggregate [] initializing a record type that has + -- aspect aggregate; the empty aggregate will be expanded into a call to + -- the empty function specified in the aspect aggregate. + + elsif Has_Aspect (Typ, Aspect_Aggregate) + and then Ekind (Typ) = E_Record_Type + and then Is_Homogeneous_Aggregate (N) + and then Is_Empty_List (Expressions (N)) + and then Is_Empty_List (Component_Associations (N)) + and then Ada_Version >= Ada_2022 + then + Resolve_Container_Aggregate (N, Typ); + elsif Is_Record_Type (Typ) then Resolve_Record_Aggregate (N, Typ); @@ -3328,6 +3341,7 @@ package body Sem_Aggr is if Present (Add_Unnamed_Subp) and then No (New_Indexed_Subp) + and then Present (Etype (Add_Unnamed_Subp)) and then Etype (Add_Unnamed_Subp) /= Any_Type then declare diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb index 7cd0800a56c..f89135983cf 100644 --- a/gcc/ada/sem_ch13.adb +++ b/gcc/ada/sem_ch13.adb @@ -16470,7 +16470,7 @@ package body Sem_Ch13 is Op_Name := Chars (First (Choices (Assoc))); -- When verifying the consistency of aspects between the freeze point - -- and the end of declarqtions, we use a copy which is not analyzed + -- and the end of declarations, we use a copy which is not analyzed -- yet, so do it now. Subp_Id := Expression (Assoc);