public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-9501] c++: Check module attachment instead of just purview when necessary [PR112631]
@ 2024-03-16 11:18 Nathaniel Shead
  0 siblings, 0 replies; only message in thread
From: Nathaniel Shead @ 2024-03-16 11:18 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ead3075406ece9daaad65a01ae539150aee43f5a

commit r14-9501-gead3075406ece9daaad65a01ae539150aee43f5a
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Tue Mar 12 23:24:27 2024 +1100

    c++: Check module attachment instead of just purview when necessary [PR112631]
    
    Block-scope declarations of functions or extern values are not allowed
    when attached to a named module. Similarly, class member functions are
    not inline if attached to a named module. However, in both these cases
    we currently only check if the declaration is within the module purview;
    it is possible for such a declaration to occur within the module purview
    but not be attached to a named module (e.g. in an 'extern "C++"' block).
    This patch makes the required adjustments.
    
            PR c++/112631
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (named_module_attach_p): New function.
            * decl.cc (start_decl): Check for attachment not purview.
            (grokmethod): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/block-decl-1_a.C: New test.
            * g++.dg/modules/block-decl-1_b.C: New test.
            * g++.dg/modules/block-decl-2.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>

Diff:
---
 gcc/cp/cp-tree.h                              |  2 ++
 gcc/cp/decl.cc                                | 10 +++++-----
 gcc/testsuite/g++.dg/modules/block-decl-1_a.C |  9 +++++++++
 gcc/testsuite/g++.dg/modules/block-decl-1_b.C | 10 ++++++++++
 gcc/testsuite/g++.dg/modules/block-decl-2.C   | 21 +++++++++++++++++++++
 5 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 14895bc6585..05913861e06 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7381,6 +7381,8 @@ inline bool module_attach_p ()
 
 inline bool named_module_purview_p ()
 { return named_module_p () && module_purview_p (); }
+inline bool named_module_attach_p ()
+{ return named_module_p () && module_attach_p (); }
 
 /* We're currently exporting declarations.  */
 inline bool module_exporting_p ()
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index dbc3df24e77..7a97b867199 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -6092,10 +6092,10 @@ start_decl (const cp_declarator *declarator,
     {
       /* A function-scope decl of some namespace-scope decl.  */
       DECL_LOCAL_DECL_P (decl) = true;
-      if (named_module_purview_p ())
+      if (named_module_attach_p ())
 	error_at (declarator->id_loc,
-		  "block-scope extern declaration %q#D not permitted"
-		  " in module purview", decl);
+		  "block-scope extern declaration %q#D must not be"
+		  " attached to a named module", decl);
     }
 
   /* Enter this declaration into the symbol table.  Don't push the plain
@@ -18907,10 +18907,10 @@ grokmethod (cp_decl_specifier_seq *declspecs,
   check_template_shadow (fndecl);
 
   /* p1779 ABI-Isolation makes inline not a default for in-class
-     definitions in named module purview.  If the user explicitly
+     definitions attached to a named module.  If the user explicitly
      made it inline, grokdeclarator will already have done the right
      things.  */
-  if ((!named_module_purview_p ()
+  if ((!named_module_attach_p ()
        || flag_module_implicit_inline
       /* Lambda's operator function remains inline.  */
        || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-1_a.C b/gcc/testsuite/g++.dg/modules/block-decl-1_a.C
new file mode 100644
index 00000000000..e7ffc629192
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-1_a.C
@@ -0,0 +1,9 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi bla }
+
+export module bla;
+
+export extern "C++" inline void fun() {
+  void oops();  // { dg-bogus "block-scope extern declaration" }
+  oops();
+}
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-1_b.C b/gcc/testsuite/g++.dg/modules/block-decl-1_b.C
new file mode 100644
index 00000000000..c0d724f25ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-1_b.C
@@ -0,0 +1,10 @@
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import bla;
+
+void oops() {}
+
+int main() {
+  fun();
+}
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-2.C b/gcc/testsuite/g++.dg/modules/block-decl-2.C
new file mode 100644
index 00000000000..974e26f9b7a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-2.C
@@ -0,0 +1,21 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi !mod }
+
+export module mod;
+
+namespace {
+  void internal() {}
+}
+
+export extern "C++" auto foo() {
+  struct X {
+    // `foo` is not attached to a named module, and as such
+    // `X::f` should be implicitly `inline` here
+    void f() {  // { dg-error "references internal linkage entity" }
+      internal();
+    }
+  };
+  return X{};
+}
+
+// { dg-prune-output "failed to write compiled module" }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-16 11:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-16 11:18 [gcc r14-9501] c++: Check module attachment instead of just purview when necessary [PR112631] Nathaniel Shead

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).