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: 69826 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1955811wrd; Tue, 14 Mar 2023 12:57:51 -0700 (PDT) X-Google-Smtp-Source: AK7set9YtYnR+yk55jDf/mZObdq4duKgcbw7hefqfAjlvXG4H0/XFGw8p0z/mWkEgF+k/gX+ES3F X-Received: by 2002:a17:906:3c16:b0:92b:bb1b:8886 with SMTP id h22-20020a1709063c1600b0092bbb1b8886mr3668988ejg.4.1678823870914; Tue, 14 Mar 2023 12:57:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678823870; cv=none; d=google.com; s=arc-20160816; b=VRJElBZGNhC+JNQ8+8xqrTFkWCY9gV2IUNgOjNZxbx+LrspFiJeglJxCjiE+LKo5sb qrHCx1ovUzDdczDlcO8xTR2x7cI4Dwz3wKvthzw7rvMV41k8QINFFohVHZsQVYVHG9gy PtW9DMf4wXQUtR5xx24cXwHCMBqiNt+6hnG2Dhbnqzrz5RD2IRlreJwI5hmWBwial60M HfPahN2inSWiDSaamuABYjjmuBkB0KVE25DKlwtQeT9rfeFcJaJUk/Hw1X6GERNuXZI3 shZWQqeOe/JSrJVBLEx/nCgMh3tgf39eZOabA8LMHexFImf3tp5VMyn93lkyKbQOpSuG 3UVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=E4wUDoEX/RBjAJI5FnviOOu16yBuVlI8YdHzkwo0pck=; b=c5NmrwaaKW6ZklOMjdSTHHlInB+ZPAyq3rd8l7pACMxANeUUEK5DxUtUv/eGIPtQUd h7DYJVTdSFPD5hwXjzNtu4Akkb5ZTn9seZgovtvEAEO4ll5LFM8YvzEZn7yHfqgK60zl PxBDvfqTYxSwyH5qD9QFzEsdYwsCqFx2IgMPCSS2JTR83a4ebwgdG92Kt/S1PmTtGCfN sHZfJoAcjVtMV3zQaKkyxMDz1r0G7ijtBzHv3/rihfvYx9ABV4MSwQzU02pSyB3Kz3NF vPr5zR16HLiBkgjdq9tgBeIglRg3S2zXJxjSADPj8bNu7WPErp+sncPQ2twYQMTNnC5Q eRDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=t9QrydXp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id se1-20020a170906ce4100b0092127059e12si3533688ejb.57.2023.03.14.12.57.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 12:57:50 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=t9QrydXp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E2CD43858408 for ; Tue, 14 Mar 2023 19:57:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2CD43858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678823869; 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=t9QrydXpQgf71g5xVpdqEIFCKHzH/j6IMGKlZeSa3nPyqC+tEQNjLXrBOVBtj0GJj OHT3TlZDxVxU+lFWEqXHmzNJh+5V0J1f0aXAVa/E6RtGIkToJsPJTTAXLWFL9ihBXg GX8yM8cAIYi78LojX7cLWsm3n0RDNxWPWSKHo7YM= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760374419069441866?= X-GMAIL-MSGID: =?utf-8?q?1760374419069441866?= 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); +}