From patchwork Mon May 1 20:54:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1775693 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=kRUemfpL; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q9Fpn3ybjz1ydX for ; Tue, 2 May 2023 06:55:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85AE13858C30 for ; Mon, 1 May 2023 20:55:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85AE13858C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682974524; bh=+6wI3e8AakSoZNDnu4WR8vikkHWpVKInmXOI3JuXa7A=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=kRUemfpLBs3gOIoJcj4YnMq0VAWb20NAggwFrcPJz68CS+XfaeQMolJnC2tKFIyeh NkF7ywmS99rcyqnmVSwxkYROxzQ0Ebl7R/r4VXDVwG96AKhkKRfshMTw+uJf9fKV4+ mcBDxvPYZxOsMME+RUVApyF5b5vaHFsiiPt/6MmM= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id AE7D5385840D for ; Mon, 1 May 2023 20:55:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE7D5385840D Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-673-tHpLEPhzPiyp6bZRQyG59A-1; Mon, 01 May 2023 16:54:58 -0400 X-MC-Unique: tHpLEPhzPiyp6bZRQyG59A-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-61a6bb9f808so10496016d6.3 for ; Mon, 01 May 2023 13:54:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682974498; x=1685566498; 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=+6wI3e8AakSoZNDnu4WR8vikkHWpVKInmXOI3JuXa7A=; b=R7bSIYOVAgyTir/9zW8VldtyRWvmAl/E5UMTopp+RwwSygi+fZJweg+XHntIziPwyv NNu4PrTqsNuQ7c/xuw61QLQ4uWmV4E8hrZ3GAV3FWTiZfZ03C6RANZbUqVyPEY+S6iOo 2+LjaiO47kYpYWASGq/2M3jj70rPaBhtmJUe/wvmYhGjlEksIcQ+HjTg+gfdu84y0pBa 8P8885Acp22qzZdZ4PcWulhRoG9pMxbLQc0RUD41J0/FfzHQaR517k1qS2jLJuf4nkfg R4/5DYqKJz1PIPn6ZmClVIcBsAOURs5JusOuWI/SGtJHdsBVNWOzvTroDq8Vjn/alYNe kjJA== X-Gm-Message-State: AC+VfDz8UgB6OBCw0FOh9mTorkoG4DIdrElUTJTeIUnGqlQ1lBFXTgba LpWQfODysecTtaf4HNOZGuUtfjuV0Dl4jlsHYRNba/JnidcFtoij/zJP0FHhR19Kwq+X/YhEf3a WCnlKNnTU7I/IhQq5vmOcpKIAOYqLyfWeRz3q/nhIXrFMAlTjF1nx+IbplVX5ocwc0CbRz9zOuA == X-Received: by 2002:a05:6214:2629:b0:61b:17bd:c603 with SMTP id gv9-20020a056214262900b0061b17bdc603mr2521869qvb.9.1682974497964; Mon, 01 May 2023 13:54:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ480XO1WP1kMo7+W0zfRhp37ryV7/nIaaUpa8cvoA2baO2UO2e666zX0JY9Z+OteeOJ67hpag== X-Received: by 2002:a05:6214:2629:b0:61b:17bd:c603 with SMTP id gv9-20020a056214262900b0061b17bdc603mr2521839qvb.9.1682974497496; Mon, 01 May 2023 13:54:57 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id d7-20020a0ce447000000b0061b5ad0290asm864856qvm.67.2023.05.01.13.54.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 13:54:56 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [pushed] c++: array DMI and member fn [PR109666] Date: Mon, 1 May 2023 16:54:54 -0400 Message-Id: <20230501205454.1627105-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.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" Tested x86_64-pc-linux-gnu, applying to trunk. Patrick, can you verify that this resolves 109506 and add whatever testcase(s) seem appropriate from that PR? -- 8< -- Here it turns out I also needed to adjust cfun when stepping out of the member function to instantiate the DMI. But instead of adding that tweak, let's unify with instantiate_body and just push_to_top_level instead of trying to do the minimum subset of it. There was no measurable change in compile time on stdc++.h. This should also resolve 109506 without yet another tweak. PR c++/109666 gcc/cp/ChangeLog: * name-lookup.cc (maybe_push_to_top_level) (maybe_pop_from_top_level): Split out... * pt.cc (instantiate_body): ...from here. * init.cc (maybe_instantiate_nsdmi_init): Use them. * name-lookup.h: Declare them.. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nsdmi-array2.C: New test. --- gcc/cp/name-lookup.h | 2 ++ gcc/cp/init.cc | 25 ++------------- gcc/cp/name-lookup.cc | 37 +++++++++++++++++++++++ gcc/cp/pt.cc | 20 ++---------- gcc/testsuite/g++.dg/cpp0x/nsdmi-array2.C | 15 +++++++++ 5 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-array2.C base-commit: 14e881eb0305090e5b184806b917d492373d32ea diff --git a/gcc/cp/name-lookup.h b/gcc/cp/name-lookup.h index c234cd44356..b3e708561d8 100644 --- a/gcc/cp/name-lookup.h +++ b/gcc/cp/name-lookup.h @@ -466,6 +466,8 @@ extern void push_nested_namespace (tree); extern void pop_nested_namespace (tree); extern void push_to_top_level (void); extern void pop_from_top_level (void); +extern bool maybe_push_to_top_level (tree); +extern void maybe_pop_from_top_level (bool); extern void push_using_decl_bindings (tree, tree); /* Lower level interface for modules. */ diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 1dd24e30d7c..0b35e1092e9 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -597,32 +597,14 @@ maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain) bool pushed = false; tree ctx = type_context_for_name_lookup (member); - processing_template_decl_sentinel ptds (/*reset*/false); + bool push_to_top = maybe_push_to_top_level (member); if (!currently_open_class (ctx)) { - if (!LOCAL_CLASS_P (ctx)) - push_to_top_level (); - else - /* push_to_top_level would lose the necessary function context, - just reset processing_template_decl. */ - processing_template_decl = 0; push_nested_class (ctx); push_deferring_access_checks (dk_no_deferred); pushed = true; } - /* If we didn't push_to_top_level, still step out of constructor - scope so build_base_path doesn't try to use its __in_chrg. */ - tree cfd = current_function_decl; - auto cbl = current_binding_level; - if (at_function_scope_p ()) - { - current_function_decl - = decl_function_context (current_function_decl); - while (current_binding_level->kind != sk_class) - current_binding_level = current_binding_level->level_chain; - } - inject_this_parameter (ctx, TYPE_UNQUALIFIED); start_lambda_scope (member); @@ -639,15 +621,12 @@ maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain) if (init != error_mark_node) DECL_INITIAL (member) = init; - current_function_decl = cfd; - current_binding_level = cbl; if (pushed) { pop_deferring_access_checks (); pop_nested_class (); - if (!LOCAL_CLASS_P (ctx)) - pop_from_top_level (); } + maybe_pop_from_top_level (push_to_top); input_location = sloc; } diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 477cddd7543..7c61bc3bf61 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -8236,6 +8236,43 @@ pop_from_top_level (void) free_saved_scope = s; } +/* Like push_to_top_level, but not if D is function-local. Returns whether we + did push to top. */ + +bool +maybe_push_to_top_level (tree d) +{ + /* Push if D isn't function-local, or is a lambda function, for which name + resolution is already done. */ + bool push_to_top + = !(current_function_decl + && !LAMBDA_FUNCTION_P (d) + && decl_function_context (d) == current_function_decl); + + if (push_to_top) + push_to_top_level (); + else + { + gcc_assert (!processing_template_decl); + push_function_context (); + cp_unevaluated_operand = 0; + c_inhibit_evaluation_warnings = 0; + } + + return push_to_top; +} + +/* Return from whatever maybe_push_to_top_level did. */ + +void +maybe_pop_from_top_level (bool push_to_top) +{ + if (push_to_top) + pop_from_top_level (); + else + pop_function_context (); +} + /* Push into the scope of the namespace NS, even if it is deeply nested within another namespace. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 6df16fef0dd..3f1cf139bbd 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -26815,20 +26815,7 @@ instantiate_body (tree pattern, tree args, tree d, bool nested_p) if (current_function_decl) save_omp_privatization_clauses (omp_privatization_save); - bool push_to_top - = !(current_function_decl - && !LAMBDA_FUNCTION_P (d) - && decl_function_context (d) == current_function_decl); - - if (push_to_top) - push_to_top_level (); - else - { - gcc_assert (!processing_template_decl); - push_function_context (); - cp_unevaluated_operand = 0; - c_inhibit_evaluation_warnings = 0; - } + bool push_to_top = maybe_push_to_top_level (d); mark_template_arguments_used (pattern, args); @@ -26942,10 +26929,7 @@ instantiate_body (tree pattern, tree args, tree d, bool nested_p) if (!nested_p) TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; - if (push_to_top) - pop_from_top_level (); - else - pop_function_context (); + maybe_pop_from_top_level (push_to_top); if (current_function_decl) restore_omp_privatization_clauses (omp_privatization_save); diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-array2.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-array2.C new file mode 100644 index 00000000000..5ad60f56510 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-array2.C @@ -0,0 +1,15 @@ +// PR c++/109666 +// { dg-do compile { target c++11 } } + +struct Point { + int value_; +}; +template struct StaticVector { + static StaticVector create() { + StaticVector output; + return output; + } + Point _M_elems[n]{}; + +}; +void f() { StaticVector<3>::create(); }