From patchwork Fri Mar 8 17:02:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 86984 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EDE3E385DC1C for ; Fri, 8 Mar 2024 17:02:48 +0000 (GMT) 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 A8B11385DC0E for ; Fri, 8 Mar 2024 17:02:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A8B11385DC0E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A8B11385DC0E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709917344; cv=none; b=BHJXIR9lpWsmJB+4De1IoxJKx+WAlbVTjt60EqTJzxxDHBQZLTaz0yXS/wX1LmbfF1+QySSxwntQ0Nd0+IZ7pm64RQWmLHZStG9wdt1mJcfA1+d4S61d2cr9kLOqlk8UaveSHmdSIfakQjsoC2y3/ONBNVUzPo8n+fl3owD2fBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709917344; c=relaxed/simple; bh=3LDJEuGY28JbG8IwkkAK2y/CD29OsGzyswKd8GZjmmI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lyubcNi1NQNxFtZRr8Cxg8i/iLPnYLE/mon2Q718HX9BB4+ufNp9sns8dsBmiXgPDsL0H5drg98/7ZK0Vk+EVSewcO5qupBY5F+YDEhyFdW1rFiQCZWqu1AOK3xdfeyZRCJSM3jJZ44Skvtuoy1jrXBFVdzQJ0pAKEqB8qCGXa4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709917342; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Rp6VGHLwYLVu5p0ysE8naZzyZgQiQFHYS2nz7OQ/rcU=; b=MCMM2cj+4zbZ3TS3eTTT7bU/767c/5qnElk3EbGHx77mRyvl6ahHX+gF4c+qsW2uWEowcL H5ke5k7hKCSn9tvZ6QTqgn4F0rl3nOxdExvB5zFm6NN89v8Dot0Vbdni+SS4B969JCFVCO /tnH9GKTM+OoRNjHbr4rkaEvRQf9i5w= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-543-jOBiQswAMt-UGDSXAzsDrg-1; Fri, 08 Mar 2024 12:02:20 -0500 X-MC-Unique: jOBiQswAMt-UGDSXAzsDrg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 429793C0C8A2 for ; Fri, 8 Mar 2024 17:02:20 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id 221B747CF; Fri, 8 Mar 2024 17:02:20 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: explicit inst of template method not generated [PR110323] Date: Fri, 8 Mar 2024 12:02:15 -0500 Message-ID: <20240308170215.21919-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 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, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Consider constexpr int VAL = 1; struct foo { template void bar(typename std::conditional::type arg) { } }; template void foo::bar<1>(int arg); where we since r11-291 fail to emit the code for the explicit instantiation. That's because cp_walk_subtrees/TYPENAME_TYPE now walks TYPE_CONTEXT ('conditional' here) as well, and in a template finds the B==VAL template argument. VAL is constexpr, which implies const, which in the global scope implies static. constrain_visibility_for_template then makes "struct conditional<(B == VAL), int, float>" non-TREE_PUBLIC. Then symtab_node::needed_p checks TREE_PUBLIC, sees it's 0, and we don't emit any code. I thought the fix would be some ODR-esque check to not consider constexpr variables/fns that are used just for their value. But it turned out to be tricky. For instance, we can't skip determine_visibility in a template; we can't even skip it for value-dep expressions. For example, no-linkage-expr1.C has using P = struct {}*; template void f(int(*)[((P)0, N)]) {} where ((P)0, N) is value-dep, but N is not relevant here: we have to ferret out the anonymous type. When instantiating, it's already gone. The best I could come up with is to disregard _DECL in min_vis_expr_r in a template while still checking type_visibility, even in a template. PR c++/110323 gcc/cp/ChangeLog: * decl2.cc (min_vis_expr_r) : Do nothing in a template. gcc/testsuite/ChangeLog: * g++.dg/template/explicit-instantiation6.C: New test. * g++.dg/template/explicit-instantiation7.C: New test. --- gcc/cp/decl2.cc | 6 ++- .../g++.dg/template/explicit-instantiation6.C | 53 +++++++++++++++++++ .../g++.dg/template/explicit-instantiation7.C | 22 ++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/explicit-instantiation6.C create mode 100644 gcc/testsuite/g++.dg/template/explicit-instantiation7.C base-commit: 10c609191c4462133d6a4ea10a739167204f2cd3 diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 6c9fd415d40..3e035a7bf9f 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -2718,7 +2718,11 @@ min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data) /* Fall through. */ case VAR_DECL: case FUNCTION_DECL: - if (! TREE_PUBLIC (t)) + if (processing_template_decl) + /* In a template, we can't trust _DECLs, either. It's possible + they won't be ODR-used, and we could wrongly think the linkage + is internal (PR110323). */; + else if (! TREE_PUBLIC (t)) tpvis = VISIBILITY_ANON; else tpvis = DECL_VISIBILITY (t); diff --git a/gcc/testsuite/g++.dg/template/explicit-instantiation6.C b/gcc/testsuite/g++.dg/template/explicit-instantiation6.C new file mode 100644 index 00000000000..399c7d72756 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/explicit-instantiation6.C @@ -0,0 +1,53 @@ +// PR c++/110323 +// { dg-do compile { target c++14 } } + +template +struct conditional { using type = T; }; + +template +struct conditional { using type = F; }; + +constexpr int VAL = 1; + +static constexpr int getval () { return 1; } + +template +constexpr int TVAL = 1; + +static struct S { + constexpr operator bool() { return true; } +} s; + +struct foo { + template + void bar(typename conditional::type arg) { } + + template + void baz(typename conditional::type arg) { } + + template + void qux(typename conditional, int, float>::type arg) { } + + template + void lox(typename conditional::type arg) { } + + template + void sox(typename conditional::type arg) { } + + template + void nim(typename conditional::type arg) { } +}; + +template void foo::bar<1>(int arg); +template void foo::baz<1>(int arg); +template void foo::qux<1>(int arg); +template void foo::lox<1>(int arg); +template void foo::sox<1>(int arg); +template void foo::nim<1>(int arg); + +// { dg-final { scan-assembler "_ZN3foo3barILi1EEEvN11conditionalIXeqT_L_ZL3VALEEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3bazILi1EEEvN11conditionalIXeqT_clL_ZL6getvalvEEEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3quxILi1EEEvN11conditionalIXeqT_L_Z4TVALIiEEEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3loxILi1EEEvN11conditionalIXeqT_L_ZL1sEEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3soxILi1EEEvN11conditionalIXeqT_nxL_ZL3VALEEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3nimILi1EEEvN11conditionalIXneT_szL_ZL3VALEEifE4typeE" } } diff --git a/gcc/testsuite/g++.dg/template/explicit-instantiation7.C b/gcc/testsuite/g++.dg/template/explicit-instantiation7.C new file mode 100644 index 00000000000..9a870e808fa --- /dev/null +++ b/gcc/testsuite/g++.dg/template/explicit-instantiation7.C @@ -0,0 +1,22 @@ +// PR c++/110323 +// { dg-do compile { target c++11 } } + +using P = struct { }*; +using N = struct A { }*; + +template +struct conditional { using type = T; }; + +struct foo { + template + void bar(typename conditional<((P) 0, B), int, float>::type arg) { } + + template + void baz(typename conditional<((N) 0, B), int, float>::type arg) { } +}; + +template void foo::bar<1>(int arg); +template void foo::baz<1>(int arg); + +// { dg-final { scan-assembler-not "_ZN3foo3barILi1EEEvN11conditionalIXcmcvP1XLi0EneT_Li0EEifE4typeE" } } +// { dg-final { scan-assembler "_ZN3foo3bazILi1EEEvN11conditionalIXcmcvP1ALi0EneT_Li0EEifE4typeE" } }