From patchwork Tue Mar 14 19:56:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1757050 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=TTHbcACz; 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 4Pbkp26jxfz1yWr for ; Wed, 15 Mar 2023 06:57:29 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 38C573858C2F for ; Tue, 14 Mar 2023 19:57:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38C573858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678823846; bh=E4wUDoEX/RBjAJI5FnviOOu16yBuVlI8YdHzkwo0pck=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=TTHbcACz/B3a6bkir4eH0C9QYw3CB0H1co2IpiBQyH4NIM8/2ddMycTOtn1F1JVxt eNJo301ioBQq6An8UWiyNwXxA5YpzCyk/xGQuNiRhwh6AdkxEban2ALF3AKncFVhRX g1QNVxl2/CRPVEqF6AWfOO5iZKfyJg2uq3Ts6cYw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050:0:465::102]) by sourceware.org (Postfix) with ESMTPS id 93C813858D39 for ; Tue, 14 Mar 2023 19:57:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 93C813858D39 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4PbknT2kr3z9skk; Tue, 14 Mar 2023 20:57:01 +0100 (CET) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix undefined reference to lambda defined in private enum [PR109108] Date: Tue, 14 Mar 2023 20:56:59 +0100 Message-Id: <20230314195659.1682947-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch fixes linker error as described in PR d/109108. Previously lambdas were connected to the module they were defined in. Now they are emitted into every referencing compilation unit, and are given one-only linkage. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, committed to mainline, and backported to releases/gcc-11 and gcc-12. Regards, Iain. --- PR d/109108 gcc/d/ChangeLog: * decl.cc (function_defined_in_root_p): Remove. (get_symbol_decl): Set DECL_LAMBDA_FUNCTION_P on function literals. (start_function): Unconditionally unset DECL_EXTERNAL (set_linkage_for_decl): Give lambda functions one-only linkage. gcc/testsuite/ChangeLog: * gdc.dg/torture/imports/pr109108.d: New test. * gdc.dg/torture/pr109108.d: New test. --- gcc/d/decl.cc | 41 ++++++------------- .../gdc.dg/torture/imports/pr109108.d | 11 +++++ gcc/testsuite/gdc.dg/torture/pr109108.d | 10 +++++ 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/torture/imports/pr109108.d create mode 100644 gcc/testsuite/gdc.dg/torture/pr109108.d diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index c451805639d..4fbabd59998 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1090,25 +1090,6 @@ build_decl_tree (Dsymbol *d) input_location = saved_location; } -/* Returns true if function FD, or any lexically enclosing scope function of FD - is defined or instantiated in a root module. */ - -static bool -function_defined_in_root_p (FuncDeclaration *fd) -{ - Module *md = fd->getModule (); - if (md && md->isRoot ()) - return true; - - for (TemplateInstance *ti = fd->isInstantiated (); ti != NULL; ti = ti->tinst) - { - if (ti->minst && ti->minst->isRoot ()) - return true; - } - - return false; -} - /* Returns true if function FD always needs to be implicitly defined, such as it was declared `pragma(inline)'. */ @@ -1474,6 +1455,12 @@ get_symbol_decl (Declaration *decl) DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl->csym) = 1; } + /* In [expression/function_literals], function literals (aka lambdas) + enable embedding anonymous functions and anonymous delegates directly + into expressions. They are defined in each referencing module. */ + if (fd->isFuncLiteralDeclaration ()) + DECL_SET_LAMBDA_FUNCTION (decl->csym, true); + /* Mark compiler generated functions as artificial. */ if (fd->isGenerated ()) DECL_ARTIFICIAL (decl->csym) = 1; @@ -2029,12 +2016,9 @@ start_function (FuncDeclaration *fd) { tree fndecl = get_symbol_decl (fd); - /* Function has been defined, check now whether we intend to send it to - object file, or it really is extern. Such as inlinable functions from - modules not in this compilation, or thunk aliases. */ - if (function_defined_in_root_p (fd)) - DECL_EXTERNAL (fndecl) = 0; - + /* Function has been defined. Whether we intend to send it to object file, or + discard it has already been determined by set_linkage_for_decl. */ + DECL_EXTERNAL (fndecl) = 0; DECL_INITIAL (fndecl) = error_mark_node; /* Add this decl to the current binding level. */ @@ -2550,9 +2534,10 @@ set_linkage_for_decl (tree decl) if (!TREE_PUBLIC (decl)) return; - /* Functions declared as `pragma(inline, true)' can appear in multiple - translation units. */ - if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)) + /* Function literals and functions declared as `pragma(inline, true)' can + appear in multiple translation units. */ + if (TREE_CODE (decl) == FUNCTION_DECL + && (DECL_DECLARED_INLINE_P (decl) || DECL_LAMBDA_FUNCTION_P (decl))) return d_comdat_linkage (decl); /* Don't need to give private or protected symbols a special linkage. */ diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr109108.d b/gcc/testsuite/gdc.dg/torture/imports/pr109108.d new file mode 100644 index 00000000000..cec5274098c --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/imports/pr109108.d @@ -0,0 +1,11 @@ +module imports.pr109108; +private enum int function(ref int)[] funs = +[ + 0: (ref idx) => 0, + 1: (ref idx) => 1, +]; + +int test109108(I)(I idx) +{ + return funs[idx](idx); +} diff --git a/gcc/testsuite/gdc.dg/torture/pr109108.d b/gcc/testsuite/gdc.dg/torture/pr109108.d new file mode 100644 index 00000000000..4a428bf85a6 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr109108.d @@ -0,0 +1,10 @@ +// { dg-additional-files "imports/pr109108.d" } +// { dg-additional-options "-I[srcdir] -fno-moduleinfo" } +// { dg-do link } +// { dg-skip-if "needs gcc/config.d" { ! d_runtime } } +import imports.pr109108; + +extern(C) int main() +{ + return test109108(0); +}