From patchwork Thu Apr 27 16:39:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1774560 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=2620:52:3:1:0:246e:9693:128c; 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=sEoWm4l6; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q6hLc4sdyz23vC for ; Fri, 28 Apr 2023 02:40:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D82CF3858C66 for ; Thu, 27 Apr 2023 16:40:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D82CF3858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682613637; bh=pzhVuUSZP/CrP/rtBzAyJBEwuOy9BGCNpyoWcbR6Ugo=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=sEoWm4l6TOhphn1BIuenliJ3nwsif4xXqspJ17JkQlXlWau/sNKMXyBdfPGul9I1x mT3mmmu77nhIK5unp+60bJITZYTAhC5msSZ8EJgrvupjKhEI42ZuzYih/zGo48k2fa cBTQR6yegN5j53qv2hUgR9yhgCdRPCXbeJWvdXfw= 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 4362F3858D38 for ; Thu, 27 Apr 2023 16:40:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4362F3858D38 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-380-F9aF_ri3ONe6-9C7pfpR7g-1; Thu, 27 Apr 2023 12:40:05 -0400 X-MC-Unique: F9aF_ri3ONe6-9C7pfpR7g-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3ef3b962f24so58651631cf.2 for ; Thu, 27 Apr 2023 09:39:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682613599; x=1685205599; 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=pzhVuUSZP/CrP/rtBzAyJBEwuOy9BGCNpyoWcbR6Ugo=; b=DXk8w5I3GtXAQjXyihJ/TIaYMUZbWaDfkbiXSyisbn6u1xTTnKvtRGu7VSGybbkh0J 01C+fsPy0PjTmZhXBlvkSvqXlLx3uckAPcQgR/ZGevHJMoziqwvFwgrh/RMVD2Loj02C P/mcOyo43m4m1QcPyXCu9GGcbzWk8c38W4nnnxqYW/sTiLKEY8tQcuYdPcmtwoKqBxkx E7sZxyVD4czTdF57uF0V8NqD83qNlrbMivz1/AxxBNeJHw9AKGuIQ6B3zuVkJGJ5kIjo y1ZSepowBVVtP4icNiJopEcM3fVLdVa9IdljUw5PQw1IShPTPCpU7jYF8DDvEvBBtuDc hs3A== X-Gm-Message-State: AC+VfDwvGRgwFefwDr/RYpZHvYEhNQcGGogHEmQmCK2IX5RszX3JN1sa dLbR5y33udvOZWx6Tz7FImkhzxSd/O3+3kEQTvxOHbT8Mandr1a0MYiHvhbwLRepiyMM6Mgvt2O Av6R0/197ukXboQQvissIZpLSCEFW/6iWJllbf2qE3wOU8zgPlkogAheBIaShvSsgbz5JW3tOAy w= X-Received: by 2002:a05:622a:20a:b0:3ef:3b04:b8d8 with SMTP id b10-20020a05622a020a00b003ef3b04b8d8mr3874191qtx.0.1682613598701; Thu, 27 Apr 2023 09:39:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ67muF8JNR/oPYwakS5QU8linBVxXd6Ma6FmX7qX/OgYh41a1wV2yuyvk8ai72PLVhWGcvVTg== X-Received: by 2002:a05:622a:20a:b0:3ef:3b04:b8d8 with SMTP id b10-20020a05622a020a00b003ef3b04b8d8mr3874139qtx.0.1682613598280; Thu, 27 Apr 2023 09:39:58 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id h6-20020a05620a244600b0074a1d2a17c8sm6049801qkn.29.2023.04.27.09.39.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 09:39:57 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: NSDMI instantiation from template context [PR109506] Date: Thu, 27 Apr 2023 12:39:56 -0400 Message-ID: <20230427163956.3051552-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.1.423.g2807bd2c10 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.8 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The testcase from this PR fails to link when -fchecking=2 with the error: /usr/bin/ld: /tmp/ccpHiXEY.o: in function `bar::bar()': ...: undefined reference to `foo::foo()' ultimately because we end up instantiating the NSDMI for bar::alloc_ from the template context func1 for which in_template_function is true and thus mark_used is inhibited from scheduling other templates for instantiation. So we end up never instantiating foo's ctor. Although maybe_instantiate_nsdmi_init does call push_to_top_level, which would have gotten us out of the template context, it doesn't happen in this case because currently_open_class (ctx) is true, thanks to an earlier call to push_scope from synthesized_method_walk. We could perhaps arrange to call push_to_top_level unconditionally in maybe_instantiate_nsdmi_init or even from from synthesized_method_walk, but that seems rather heavyweight for this situation. Ideally we just want a way to allow mark_used to work here despite being in a template context. To that end, this patch first generalizes the in_template_function test in mark_used to instead test current_template_parms, which has two benefits: it works for all template contexts, not just function template contexts, and it can be cheaply disabled by simply clearing current_template_parms. This patch then makes us disable this test from maybe_instantiate_nsdmi_init. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? This doesn't seem worth backporting since the bug seems to manifest only with -fchecking=2. PR c++/109506 gcc/cp/ChangeLog: * decl2.cc (mark_used): Check current_template_parms instead of in_template_function. * init.cc (maybe_instantiate_nsdmi_init): Clear current_template_parms before instantiating. gcc/testsuite/ChangeLog: * g++.dg/warn/Waddress-of-packed-member2.C: No longer expect a "used but never defined" warning due to the use from an uninstantiated template. * g++.dg/template/non-dependent25.C: New test. --- gcc/cp/decl2.cc | 6 ++++- gcc/cp/init.cc | 3 +++ gcc/testsuite/g++.dg/cpp0x/nsdmi-template26.C | 23 +++++++++++++++++++ .../g++.dg/warn/Waddress-of-packed-member2.C | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template26.C diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 9594be4092c..b9d37d76bf6 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -5781,7 +5781,11 @@ mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */) && DECL_OMP_DECLARE_REDUCTION_P (decl))) maybe_instantiate_decl (decl); - if (processing_template_decl || in_template_function ()) + /* We don't want to instantiate templates based on uses from other + uninstantiated templates. Since processing_template_decl is cleared + during instantiate_non_dependent_expr, we check current_template_parms + as well. */ + if (processing_template_decl || current_template_parms) return true; /* Check this too in case we're within instantiate_non_dependent_expr. */ diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 1dd24e30d7c..ef32ef2a8c2 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -610,6 +610,9 @@ maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain) push_deferring_access_checks (dk_no_deferred); pushed = true; } + /* Make sure current_template_parms is cleared so that mark_used + is uninhibited. */ + auto ctpo = make_temp_override (current_template_parms, NULL_TREE); /* 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. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template26.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template26.C new file mode 100644 index 00000000000..d9e17ea6724 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template26.C @@ -0,0 +1,23 @@ +// PR c++/109506 +// { dg-do link { target c++11 } } +// { dg-additional-options "-fchecking=2" } + +template +struct foo { + foo() { }; +}; + +template +class bar { + foo alloc_{}; +}; + +template +bar func1() { + return bar{}; +} + +int main() { + func1(); +} + diff --git a/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C b/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C index e9bf7cac04c..d619b28cfe1 100644 --- a/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C +++ b/gcc/testsuite/g++.dg/warn/Waddress-of-packed-member2.C @@ -1,7 +1,7 @@ // PR c++/89973 // { dg-do compile { target c++14 } } -constexpr int a(); // { dg-warning "used but never defined" } +constexpr int a(); template constexpr void *b = a(); // { dg-error "invalid conversion" }