From patchwork Tue Feb 4 22:26:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1233584 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-518914-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=BwZbsCkF; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZUKHs3/N; 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 48BzpB5N1Jz9sRY for ; Wed, 5 Feb 2020 09:27:12 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type :content-transfer-encoding; q=dns; s=default; b=KR8kKSCCu15IjLH+ VjGJaJlKpiYKTiWY3UYk+Gjj+6BL3GSHZDNWygydYrDnVCkCE5DRvodVvFps8RKi 5vfIGvqpTYfEfmnk0eSgPreRDAFdweW3ypWL/E/ZUyCYwevYCfeJa8DX66YX0+P5 LEr3MgjRK6PE5FzgbPXzmc3DvlM= 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:from :to:subject:date:message-id:content-type :content-transfer-encoding; s=default; bh=Y35emdpj+x33SlPG4KH2Cd j9yrc=; b=BwZbsCkF7wTOVVNAUYy4crwb/n8D3mvjvABJ1ueOa4HXGfr6HeKZqL AifrKirTyBEb/oTKmM7JEBsowDYu7/Un7mn+fju6mg/d209VxHbdr/QaseS/1uA8 zWL+cqvJkln7dEtncaTKtQlu2thjpeQ9Z14ooSrlDFytZBHM0QOh8= Received: (qmail 128175 invoked by alias); 4 Feb 2020 22:27:02 -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 128103 invoked by uid 89); 4 Feb 2020 22:27:01 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Feb 2020 22:26:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580855218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+lusgeLIyeoAT2mC+Six6KRZzp5Lzwq5uteM7zzxNWA=; b=ZUKHs3/Ng4h0/W+j5z7jVL/dJZ3BTFsqiwONeJDKACZr3962EfhNE0mG2jLNSf/a9xq1Ka VOAaPlKRSmIBmOt1vfzXXHJRSnjrTXdHbhf8BUjMCZTB1S4BFM2BbCrSE0c2GfDch/PGDE h85w8soY/Ko8Sqm1Hw+coTdm+sYdGpg= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-302-qlEqeG2mNCq5Rc7KgtsXCA-1; Tue, 04 Feb 2020 17:26:56 -0500 Received: by mail-qt1-f197.google.com with SMTP id b5so13482098qtt.10 for ; Tue, 04 Feb 2020 14:26:56 -0800 (PST) Received: from barrymore.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id w41sm12828653qtj.49.2020.02.04.14.26.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2020 14:26:54 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [COMMITTED] c++: Fix constexpr vs. omitted aggregate init. Date: Tue, 4 Feb 2020 17:26:53 -0500 Message-Id: <20200204222653.6935-1-jason@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes Value-initialization is importantly different from {}-initialization for this testcase, where the former calls the deleted S constructor and the latter initializes S happily. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/90951 * constexpr.c (cxx_eval_array_reference): {}-initialize missing elements instead of value-initializing them. --- gcc/cp/constexpr.c | 12 ++++++++++-- gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C base-commit: a1c9c9ff06ab15e697d5bac6ea6e5da2df840cf5 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index c35ec5acc97..8a02c6e0713 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -3324,8 +3324,16 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, } /* If it's within the array bounds but doesn't have an explicit - initializer, it's value-initialized. */ - tree val = build_value_init (elem_type, tf_warning_or_error); + initializer, it's initialized from {}. But use build_value_init + directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ + tree val; + if (CP_AGGREGATE_TYPE_P (elem_type)) + { + tree empty_ctor = build_constructor (init_list_type_node, NULL); + val = digest_init (elem_type, empty_ctor, tf_warning_or_error); + } + else + val = build_value_init (elem_type, tf_warning_or_error); return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, overflow_p); } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C new file mode 100644 index 00000000000..538969830ba --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C @@ -0,0 +1,10 @@ +// PR c++/90951 +// { dg-do compile { target c++11 } } + +#define assert(expr) static_assert (expr, #expr) + +struct S { const char a[2]; }; + +constexpr struct S a[1][1][1] = { }; + +assert ('\0' == *a[0][0][0].a);