aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorNathaniel Shead <nathanieloshead@gmail.com>2024-03-12 23:24:27 +1100
committerNathaniel Shead <nathanieloshead@gmail.com>2024-03-16 22:18:19 +1100
commitead3075406ece9daaad65a01ae539150aee43f5a (patch)
tree08fbd5f63ae2356d4629bd54da890e309b3cc353 /gcc
parent5213047b1d50af63dfabb5e5649821a6cb157e33 (diff)
downloadgcc-ead3075406ece9daaad65a01ae539150aee43f5a.zip
gcc-ead3075406ece9daaad65a01ae539150aee43f5a.tar.gz
gcc-ead3075406ece9daaad65a01ae539150aee43f5a.tar.bz2
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>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/cp-tree.h2
-rw-r--r--gcc/cp/decl.cc10
-rw-r--r--gcc/testsuite/g++.dg/modules/block-decl-1_a.C9
-rw-r--r--gcc/testsuite/g++.dg/modules/block-decl-1_b.C10
-rw-r--r--gcc/testsuite/g++.dg/modules/block-decl-2.C21
5 files changed, 47 insertions, 5 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 14895bc..0591386 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 dbc3df2..7a97b86 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 0000000..e7ffc62
--- /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 0000000..c0d724f
--- /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 0000000..974e26f
--- /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" }