From patchwork Sun Jan 23 03:18:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1583083 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=Amx+Iuqm; 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 4JhJLj5bnnz9sCD for ; Sun, 23 Jan 2022 14:21:49 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 728373858424 for ; Sun, 23 Jan 2022 03:21:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 728373858424 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642908107; bh=ptBoxnEgwy+x+oJfKjB4GyL52VtWdF4f337N0E1qyJo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Amx+Iuqm6w9T4SlT/SbMsbmVSII9PfIgT7n3EqJ2/tA+8fGq1SpXSTUzhB8+dn2ff N9MsvMFbowS34usDLPHXQTGzplOD2upY7gxlj1pM6hO25HI/V+CKdGWgQZyPTkfW4N NU22am8eu7Jrxf1n+bgpaGBLZKBesjHLWLFCza5U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5A23D385841E for ; Sun, 23 Jan 2022 03:19:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5A23D385841E Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-436-q5305GhoPS6ehG_rDeDjvg-1; Sat, 22 Jan 2022 22:19:01 -0500 X-MC-Unique: q5305GhoPS6ehG_rDeDjvg-1 Received: by mail-qk1-f197.google.com with SMTP id 22-20020a05620a079600b0047cfe6c3cb2so2076516qka.1 for ; Sat, 22 Jan 2022 19:19:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=ptBoxnEgwy+x+oJfKjB4GyL52VtWdF4f337N0E1qyJo=; b=Zor44ynU/vXmolEyRXU2u0onglz0ID1gBxMG9SgmR40pLoVY78RtCvMDSThBrLnLVk PUuMyBqYL9qQvN2fSTIj85yAweIGCUZ5TducDqi1yFbjOha2PzsJmIC1qcZmO5QPXTwv VyWKsLiIlvAABzraoVA5CdfKjUoNTky5xu6hgatDieFL4V/GoRJ2kP7oB2/PPGPT6YuC HwKcVLMd68nR5xmewBIv+UB5AxKa65YAlnnSpHDfnz8GmpE7vTUjiNYjz9sam9AIdmCn ydc2TpjkM+IJFnnlSS3e0vcF+m5N92IxEKmSK+hKkX70KtG+W4lQpxXkCr/vbAAaIxbu ogRg== X-Gm-Message-State: AOAM532jm24XyeEWGFp6KxCSBc8zEqCLXVWOdElPJDEbM2SgqjEVvC7i UrD4pqfsPJcs0s5a2TMxDgKIfmN58QtR8pCI77jdilgyjtYqPc7Ag5di8A10f8TcsiSqXpzie1M Ys+kNo7yToGdLnLWbXLcqfRne0XvWJvPkeTKwaxsrh+/TDdF+xM4AMvAmiTClpT8KvA== X-Received: by 2002:ac8:5e0c:: with SMTP id h12mr8457108qtx.65.1642907941022; Sat, 22 Jan 2022 19:19:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9IlgeukHYOzLGYNXYYQ/E5JFIW2p15GtIGEHkAd4t5zUkpj7GGcBlkMxFhwUizei18fs8Lw== X-Received: by 2002:ac8:5e0c:: with SMTP id h12mr8457095qtx.65.1642907940686; Sat, 22 Jan 2022 19:19:00 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id h21sm5478628qth.16.2022.01.22.19.18.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Jan 2022 19:18:59 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: array temporary at file scope [PR104182] Date: Sat, 22 Jan 2022 22:18:57 -0500 Message-Id: <20220123031857.685783-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This is the same issue as PR104031, but that patch doesn't fix this testcase because in this case, current_function_decl isn't set when we get to cp_genericize_target_expr. But there seems to be no need for is_local_temp to check for function scope; !TREE_STATIC should be enough. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/104182 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_genericize_target_expr): Make sure nothing has set DECL_INITIAL on a TARGET_EXPR slot. * tree.cc (is_local_temp): Don't check DECL_CONTEXT. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist127.C: New test. --- gcc/cp/cp-gimplify.cc | 6 +--- gcc/cp/tree.cc | 3 +- gcc/testsuite/g++.dg/cpp0x/initlist127.C | 39 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist127.C base-commit: d43be9dcc1bef95d70fb411d98b4de0d83c6b85d prerequisite-patch-id: 2611088858b4dd4c0ce09bad7e27f4a837282865 diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 0166ef690d2..d1c653c5fda 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -931,13 +931,9 @@ static void cp_genericize_target_expr (tree *stmt_p) { tree slot = TARGET_EXPR_SLOT (*stmt_p); - /* If TARGET_EXPR is created for some global var initializer, the slot - will have NULL and so is_local_temp will return false for it. If - this is a ssdf, set DECL_CONTEXT now. */ - if (DECL_CONTEXT (slot) == NULL_TREE) - DECL_CONTEXT (slot) = current_function_decl; cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p), TARGET_EXPR_INITIAL (*stmt_p), slot); + gcc_assert (!DECL_INITIAL (slot)); } /* Genericization context. */ diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index f88006aec4f..5d453e49717 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -557,8 +557,7 @@ bool is_local_temp (tree decl) { return (VAR_P (decl) && DECL_ARTIFICIAL (decl) - && !TREE_STATIC (decl) - && DECL_FUNCTION_SCOPE_P (decl)); + && !TREE_STATIC (decl)); } /* Set various status flags when building an AGGR_INIT_EXPR object T. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist127.C b/gcc/testsuite/g++.dg/cpp0x/initlist127.C new file mode 100644 index 00000000000..ebe5924bbf9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist127.C @@ -0,0 +1,39 @@ +// PR c++/104182 +// { dg-do run { target c++11 } } + +#include + +int b; + +struct stringy { + const char *p; + stringy(const char *p): p(p) { ++b; } + const char& operator[](int i) const { return p[i]; } + ~stringy() { --b; } +}; + +struct S +{ + int A; + stringy B; +}; + +struct veccy { + S s; + veccy (const std::initializer_list &l): s(*l.begin()) {} + const S& operator[](int i) const { return s; } +}; + +struct V +{ + veccy v; +}; + +static const V v{ { { { 237, "2" } } } }; + +int main() +{ + if (v.v[0].A != 237 || v.v[0].B[0] != '2') + __builtin_abort(); + return 0; +}