]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: module-contained PRIVATE procedures must have global ELF linkage [PR125430]
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Sat, 23 May 2026 04:56:34 +0000 (21:56 -0700)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 28 May 2026 18:47:01 +0000 (11:47 -0700)
Assisted by: Claude Sonnet 4.6

gcc/fortran/ChangeLog:

PR fortran/125430
* trans-decl.cc (build_function_decl): Set TREE_PUBLIC for all
module-contained procedures so submodules compiled as separate
translation units can reach them via host association.  Also set
DECL_VISIBILITY to VISIBILITY_HIDDEN for PRIVATE procedures,
matching the existing treatment of module variables.

gcc/testsuite/ChangeLog:

PR fortran/125430
* gfortran.dg/module_private_2.f90: Remove scan-tree-dump-times
assertion for 'priv'; PRIVATE module procedures now have global
linkage with hidden visibility and are no longer optimized away.
* gfortran.dg/public_private_module_2.f90: Add xfail markers to
scan-assembler-not for 'two' and 'six'; update comment to mention
procedures alongside variables.
* gfortran.dg/public_private_module_7.f90: Add xfail marker to
scan-assembler-not for '__m_common_attrs_MOD_other'.
* gfortran.dg/public_private_module_8.f90: Add xfail marker to
scan-assembler-not for '__m_MOD_myotherlen'.
* gfortran.dg/submodule_private_host.f90: New test.
* gfortran.dg/submodule_private_host_aux.f90: New auxiliary file.
* gfortran.dg/warn_unused_function_2.f90: Remove 'defined but not
used' expectation for s1; PRIVATE module procedures now have
global linkage and no longer trigger the unused-function warning.

gcc/fortran/trans-decl.cc
gcc/testsuite/gfortran.dg/module_private_2.f90
gcc/testsuite/gfortran.dg/public_private_module_2.f90
gcc/testsuite/gfortran.dg/public_private_module_7.f90
gcc/testsuite/gfortran.dg/public_private_module_8.f90
gcc/testsuite/gfortran.dg/submodule_private_host.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/submodule_private_host_aux.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/warn_unused_function_2.f90

index 1bcbfdfd2c9782e172ef92bcb4f2f31b5e043db9..e6c9eaf1796f88be8e6911132af039d2e9271977 100644 (file)
@@ -2575,11 +2575,27 @@ build_function_decl (gfc_symbol * sym, bool global)
              && flag_module_private)))
     sym->attr.access = ACCESS_PRIVATE;
 
+  bool in_module_contains = sym->module && sym->ns->proc_name
+                            && sym->ns->proc_name->attr.flavor == FL_MODULE;
+
   if (!current_function_decl
       && !sym->attr.entry_master && !sym->attr.is_main_program
       && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
-         || sym->attr.public_used))
-    TREE_PUBLIC (fndecl) = 1;
+         || sym->attr.public_used || in_module_contains))
+    {
+      TREE_PUBLIC (fndecl) = 1;
+
+      /* Mirror the variable treatment (see gfc_finish_var_decl): PRIVATE
+        module procedures get global linkage but hidden visibility so the
+        symbol is reachable from submodules in the same link without being
+        exported to external DSOs.  */
+      if (in_module_contains && sym->attr.access == ACCESS_PRIVATE
+         && !sym->attr.public_used)
+       {
+         DECL_VISIBILITY (fndecl) = VISIBILITY_HIDDEN;
+         DECL_VISIBILITY_SPECIFIED (fndecl) = true;
+       }
+    }
 
   if (sym->attr.referenced || sym->attr.entry_master)
     TREE_USED (fndecl) = 1;
index 58dbb1e23fe56716429cae0b6d17dec6403839d3..926da3641826c596a5a362bcdc6548ace3fee39c 100644 (file)
@@ -29,6 +29,7 @@ contains
     b => export1
   end subroutine pub
 end module m
-! { dg-final { scan-tree-dump-times "priv" 0 "optimized" } }
+! priv now has TREE_PUBLIC (VISIBILITY_HIDDEN) for submodule host-association,
+! so it is no longer optimized away.
 ! { dg-final { scan-tree-dump-times "export1 \\(\\)" 1 "optimized" } }
 ! { dg-final { scan-tree-dump-times "export2 \\(\\)" 1 "optimized" } }
index 87276ccdfd187e4fd51d5db404722de59cad9f0e..e37cfbd57ccce3b357d6627cad296ef63871a7ab 100644 (file)
@@ -19,8 +19,10 @@ module mod
   integer, bind(C,name='') :: qq
 end module mod
 
-! The two xfails below have appeared with the introduction of submodules. 'iii' and
+! The xfails below have appeared with the introduction of submodules. 'iii' and
 ! 'mmm' now are TREE_PUBLIC but has DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN set.
+! Similarly 'two' and 'six' (PRIVATE procedures) now have TREE_PUBLIC +
+! VISIBILITY_HIDDEN so that submodules can reach them via host association.
 
       ! { dg-final { scan-assembler "__mod_MOD_aa" } }
       ! { dg-final { scan-assembler-not "iii" { xfail *-*-* } } }
@@ -64,11 +66,11 @@ CONTAINS
 END MODULE
 
 ! { dg-final { scan-assembler "__m_MOD_one" } }
-! { dg-final { scan-assembler-not "two" } }
+! { dg-final { scan-assembler-not "two" { xfail *-*-* } } }
 ! { dg-final { scan-assembler "three" } }
 ! { dg-final { scan-assembler-not "four" } }
 ! { dg-final { scan-assembler "five" } }
-! { dg-final { scan-assembler-not "six" } }
+! { dg-final { scan-assembler-not "six" { xfail *-*-* } } }
 ! { dg-final { scan-assembler "seven" } }
 ! { dg-final { scan-assembler "nine" } }
 ! { dg-final { scan-assembler "__m_MOD_ten" } }
index d03b7047a126aed151eeae0a5abe7a658df967ba..4b62d23dc6f338699595b70a313264c3cc6ce72d 100644 (file)
@@ -25,5 +25,5 @@ contains
   end function get_key
 end module m_common_attrs
 
-! { dg-final { scan-assembler-not "__m_common_attrs_MOD_other" } }
+! { dg-final { scan-assembler-not "__m_common_attrs_MOD_other" { xfail *-*-* } } }
 ! { dg-final { scan-assembler "__m_common_attrs_MOD_get_key_len" } }
index bfc1b368f46ba0b8ea4a12b9358d38bddfe4a8c0..addf93440abb7c8726e1e645f572097e3489f913 100644 (file)
@@ -44,6 +44,6 @@ contains
   end subroutine bar
 end module m
 
-! { dg-final { scan-assembler-not "__m_MOD_myotherlen" } }
+! { dg-final { scan-assembler-not "__m_MOD_myotherlen" { xfail *-*-* } } }
 ! { dg-final { scan-assembler "__m_MOD_bar" } }
 ! { dg-final { scan-assembler "__m_MOD_mylen" } }
diff --git a/gcc/testsuite/gfortran.dg/submodule_private_host.f90 b/gcc/testsuite/gfortran.dg/submodule_private_host.f90
new file mode 100644 (file)
index 0000000..e50faaa
--- /dev/null
@@ -0,0 +1,29 @@
+! { dg-do run }
+! { dg-additional-sources submodule_private_host_aux.f90 }
+!
+! PR fortran/125430
+!
+! Module-contained procedures with PRIVATE access must retain global ELF
+! linkage so that submodules compiled as separate translation units can
+! reach them via host association.
+
+module m
+  implicit none
+  private
+  public :: pub
+
+  interface
+    module subroutine pub(x)
+      double precision, intent(out) :: x
+    end subroutine
+  end interface
+
+contains
+
+  pure function priv(n) result(x)
+    integer, intent(in) :: n
+    double precision :: x
+    x = dble(n) * 0.5d0
+  end function
+
+end module
diff --git a/gcc/testsuite/gfortran.dg/submodule_private_host_aux.f90 b/gcc/testsuite/gfortran.dg/submodule_private_host_aux.f90
new file mode 100644 (file)
index 0000000..e237cf2
--- /dev/null
@@ -0,0 +1,17 @@
+! Auxiliary file for submodule_private_host.f90
+! { dg-skip-if "" { *-*-* } }
+submodule(m) ms
+  implicit none
+contains
+  module procedure pub
+    x = priv(6)
+  end procedure
+end submodule
+
+program p
+  use m, only : pub
+  implicit none
+  double precision x
+  call pub(x)
+  if (abs(x - 3.0d0) > 1d-10) stop 1
+end program
index 43b211e57bfe41c2a24f2c3c39454c992f670f00..894a343b75c19b2162543341cc2a631ba87b402e 100644 (file)
@@ -13,7 +13,7 @@ module m
 
 contains
 
-  subroutine s1            ! { dg-warning "defined but not used" }
+  subroutine s1
     call s2(s3)
     contains
       subroutine s4        ! { dg-warning "defined but not used" }