]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Improve errors for bad module-directives [PR115200]
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 23 May 2024 14:08:57 +0000 (00:08 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 24 May 2024 14:10:42 +0000 (00:10 +1000)
This fixes an ICE when a module directive is not given at global scope.
Although not explicitly mentioned, it seems implied from [basic.link] p1
and [module.global.frag] that a module-declaration must appear at the
global scope after preprocessing.  Apart from this the patch also
slightly improves the errors given when accidentally using a module
control-line in other situations where it is not expected.

PR c++/115200

gcc/cp/ChangeLog:

* parser.cc (cp_parser_error_1): Special-case unexpected module
directives for better diagnostics.
(cp_parser_module_declaration): Check that the module
declaration is at global scope.
(cp_parser_import_declaration): Sync error message with that in
cp_parser_error_1.

gcc/testsuite/ChangeLog:

* g++.dg/modules/mod-decl-1.C: Update error messages.
* g++.dg/modules/mod-decl-6.C: New test.
* g++.dg/modules/mod-decl-7.C: New test.
* g++.dg/modules/mod-decl-8.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/modules/mod-decl-1.C
gcc/testsuite/g++.dg/modules/mod-decl-6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/mod-decl-7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/mod-decl-8.C [new file with mode: 0644]

index 476ddc0d63ad3f597123b86e3799d10db40be2e0..779625144db405561e62da8c7553c16234324c79 100644 (file)
@@ -3230,6 +3230,19 @@ cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
       return;
     }
 
+  if (cp_token_is_module_directive (token))
+    {
+      auto_diagnostic_group d;
+      error_at (token->location, "unexpected module directive");
+      if (token->keyword != RID__EXPORT)
+       inform (token->location, "perhaps insert a line break after"
+               " %qs, or other disambiguation, to prevent this being"
+               " considered a module control-line",
+               (token->keyword == RID__MODULE) ? "module" : "import");
+      cp_parser_skip_to_pragma_eol (parser, token);
+      return;
+    }
+
   /* If this is actually a conflict marker, report it as such.  */
   if (token->type == CPP_LSHIFT
       || token->type == CPP_RSHIFT
@@ -15135,12 +15148,19 @@ cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
   parser->lexer->in_pragma = true;
   cp_token *token = cp_lexer_consume_token (parser->lexer);
 
+  tree scope = current_scope ();
   if (flag_header_unit)
     {
       error_at (token->location,
                "module-declaration not permitted in header-unit");
       goto skip_eol;
     }
+  else if (scope != global_namespace)
+    {
+      error_at (token->location, "module-declaration must be at global scope");
+      inform (DECL_SOURCE_LOCATION (scope), "scope opened here");
+      goto skip_eol;
+    }
   else if (mp_state == MP_FIRST && !exporting
       && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
     {
@@ -15217,9 +15237,9 @@ cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
       error_at (token->location, "post-module-declaration"
                " imports must be contiguous");
     note_lexer:
-      inform (token->location, "perhaps insert a line break, or other"
-             " disambiguation, to prevent this being considered a"
-             " module control-line");
+      inform (token->location, "perhaps insert a line break after"
+             " %<import%>, or other disambiguation, to prevent this"
+             " being considered a module control-line");
     skip_eol:
       cp_parser_skip_to_pragma_eol (parser, token);
     }
index 23d34483dd7c883119274503bd77496b66e48fdf..df398b3ef50a5cb1ed2a7bd8d343acd94026f8b4 100644 (file)
@@ -10,17 +10,19 @@ module foo.second; // { dg-error "only permitted as" }
 
 namespace Foo 
 {
-module third;  // { dg-error "only permitted as" }
+module third;  // { dg-error "must be at global scope" }
 }
 
 struct Baz
 {
-  module forth; // { dg-error "expected" }
+  module forth; // { dg-error "unexpected module directive" }
+  // { dg-message "line break after .module." "" { target *-*-* } .-1 }
 };
 
 void Bink ()
 {
-  module fifth; // { dg-error "expected" }
+  module fifth; // { dg-error "unexpected module directive" }
+  // { dg-message "line break after .module." "" { target *-*-* } .-1 }
 }
 
 module a.; // { dg-error "only permitted as" }
diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-6.C b/gcc/testsuite/g++.dg/modules/mod-decl-6.C
new file mode 100644 (file)
index 0000000..5fb3424
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/115200
+// { dg-additional-options "-fmodules-ts -Wno-global-module" }
+// { dg-module-cmi !M }
+
+module;
+
+namespace ns {  // { dg-message "scope opened here" }
+
+export module M;  // { dg-error "global scope" }
+
+}
diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-7.C b/gcc/testsuite/g++.dg/modules/mod-decl-7.C
new file mode 100644 (file)
index 0000000..d25a193
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/115200
+// { dg-additional-options "-fmodules-ts -Wno-global-module" }
+// { dg-module-cmi !M }
+
+module;
+
+void foo() {
+
+export module M;  // { dg-error "unexpected module directive" }
+
+}
diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-8.C b/gcc/testsuite/g++.dg/modules/mod-decl-8.C
new file mode 100644 (file)
index 0000000..9f5f1de
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi M }
+
+export module M;
+
+struct module {};
+struct import {};
+
+void foo() {
+  module
+    x;  // OK
+  import
+    y;  // OK
+}