public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Iain Buclaw <ibuclaw@gdcproject.org>
To: gcc-patches@gcc.gnu.org
Cc: Iain Buclaw <ibuclaw@gdcproject.org>
Subject: [committed] d: Fix undefined reference to lambda defined in private enum [PR109108]
Date: Tue, 14 Mar 2023 20:56:59 +0100	[thread overview]
Message-ID: <20230314195659.1682947-1-ibuclaw@gdcproject.org> (raw)

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);
+}
-- 
2.37.2


                 reply	other threads:[~2023-03-14 19:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230314195659.1682947-1-ibuclaw@gdcproject.org \
    --to=ibuclaw@gdcproject.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).