]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
d: Fix undefined reference to lambda defined in private enum [PR109108]
authorIain Buclaw <ibuclaw@gdcproject.org>
Tue, 14 Mar 2023 12:16:11 +0000 (13:16 +0100)
committerIain Buclaw <ibuclaw@gdcproject.org>
Tue, 14 Mar 2023 19:33:54 +0000 (20:33 +0100)
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.

PR d/109108

gcc/d/ChangeLog:

* decl.cc (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/torture.exp (srcdir): New proc.
* gdc.dg/torture/imports/pr109108.d: New test.
* gdc.dg/torture/pr109108.d: New test.

(cherry picked from commit 423d34f61c43e400f0d5b837fe93c83963b2ecdd)

gcc/d/decl.cc
gcc/testsuite/gdc.dg/torture/imports/pr109108.d [new file with mode: 0644]
gcc/testsuite/gdc.dg/torture/pr109108.d [new file with mode: 0644]
gcc/testsuite/gdc.dg/torture/torture.exp

index c77f10fe61577704bc85e2d6b66310b3707bf8b1..bc494d3e7cacb439919c7325d87fcf982321304d 100644 (file)
@@ -1320,6 +1320,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->generated)
        DECL_ARTIFICIAL (decl->csym) = 1;
@@ -1873,9 +1879,10 @@ 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.  */
+  /* 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;
+
   TemplateInstance *ti = fd->isInstantiated ();
   if (ti && ti->needsCodegen ())
     {
@@ -1885,14 +1892,6 @@ start_function (FuncDeclaration *fd)
          warning (OPT_Wtemplates, "%s %qs instantiated",
                   ti->kind (), ti->toPrettyChars (false));
        }
-
-      DECL_EXTERNAL (fndecl) = 0;
-    }
-  else
-    {
-      Module *md = fd->getModule ();
-      if (md && md->isRoot ())
-       DECL_EXTERNAL (fndecl) = 0;
     }
 
   DECL_INITIAL (fndecl) = error_mark_node;
@@ -2413,16 +2412,17 @@ set_linkage_for_decl (tree decl)
   if (!TREE_PUBLIC (decl))
     return;
 
+  /* 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.  */
   if ((TREE_PRIVATE (decl) || TREE_PROTECTED (decl))
       && !DECL_INSTANTIATED (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))
-    return d_comdat_linkage (decl);
-
   /* If all instantiations must go in COMDAT, give them that linkage.
      This also applies to other extern declarations, so that it is possible
      for them to override template declarations.  */
diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr109108.d b/gcc/testsuite/gdc.dg/torture/imports/pr109108.d
new file mode 100644 (file)
index 0000000..cec5274
--- /dev/null
@@ -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 (file)
index 0000000..4a428bf
--- /dev/null
@@ -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);
+}
index da3297b47b26369cf458abf8c669d8dcf0782a9c..13be801823e8ca0996d68d51fc9d7c68939fd62f 100644 (file)
 # Load support procs.
 load_lib gdc-dg.exp
 
+# Helper function allows adding tests that use imports/*, but don't compile
+# the sources in with dg-additional-sources.
+global testdir
+set testdir $srcdir/$subdir
+proc srcdir {} {
+    global testdir
+    return $testdir
+}
+
 # The default option list can be overridden by
 # TORTURE_OPTIONS="{ { list1 } ... { listN } }"