From: Nathan Sidwell Date: Tue, 5 Jan 2021 13:18:50 +0000 (-0800) Subject: [c++]: Improve module-decl diagnostics [PR 98327] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6ffaffd5d10e1d7daee5e1b889bfc8de06ef1a4b;p=gcc.git [c++]: Improve module-decl diagnostics [PR 98327] The diagnostic for a misplaced module decl was essentially 'computer says no', which isn't the most helpful. This adjusts it to indicate what would be acceptable. gcc/cp/ * parser.c (cp_parser_module_declaration): Alter diagnostic text to say where is permissable. gcc/testsuite/ * g++.dg/modules/mod-decl-1.C: Adjust. * g++.dg/modules/p0713-2.C: Adjust. * g++.dg/modules/p0713-3.C: Adjust. --- diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d855e034458..c713852fe93 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -13726,19 +13726,22 @@ cp_parser_module_declaration (cp_parser *parser, module_parse mp_state, cp_lexer_consume_token (parser->lexer); cp_parser_require_pragma_eol (parser, token); - if ((mp_state != MP_PURVIEW && mp_state != MP_PURVIEW_IMPORTS) + if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS) || !module_interface_p () || module_partition_p ()) error_at (token->location, - "private module fragment not permitted here"); + "private module fragment only permitted in purview" + " of module interface or partition"); else { mp_state = MP_PRIVATE_IMPORTS; sorry_at (token->location, "private module fragment"); } } - else if (mp_state != MP_FIRST && mp_state != MP_GLOBAL) + else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL)) { - error_at (token->location, "module-declaration not permitted here"); + /* Neither the first declaration, nor in a GMF. */ + error_at (token->location, "module-declaration only permitted as first" + " declaration, or ending a global module fragment"); skip_eol: cp_parser_skip_to_pragma_eol (parser, token); } diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-1.C b/gcc/testsuite/g++.dg/modules/mod-decl-1.C index b2665bec743..23d34483dd7 100644 --- a/gcc/testsuite/g++.dg/modules/mod-decl-1.C +++ b/gcc/testsuite/g++.dg/modules/mod-decl-1.C @@ -6,11 +6,11 @@ export module frist; import frist; // { dg-error {cannot import module.* in its own purview} } -module foo.second; // { dg-error "not permitted here" } +module foo.second; // { dg-error "only permitted as" } namespace Foo { -module third; // { dg-error "not permitted here" } +module third; // { dg-error "only permitted as" } } struct Baz @@ -23,7 +23,7 @@ void Bink () module fifth; // { dg-error "expected" } } -module a.; // { dg-error "not permitted" } +module a.; // { dg-error "only permitted as" } // { dg-prune-output "not writing module" } diff --git a/gcc/testsuite/g++.dg/modules/p0713-2.C b/gcc/testsuite/g++.dg/modules/p0713-2.C index c7846e450a9..cb4ccb6c5f6 100644 --- a/gcc/testsuite/g++.dg/modules/p0713-2.C +++ b/gcc/testsuite/g++.dg/modules/p0713-2.C @@ -1,3 +1,3 @@ // { dg-additional-options "-fmodules-ts" } int j; -module; // { dg-error "not permitted" } +module; // { dg-error "only permitted as" } diff --git a/gcc/testsuite/g++.dg/modules/p0713-3.C b/gcc/testsuite/g++.dg/modules/p0713-3.C index 3c539ebab3e..09d89b73b3f 100644 --- a/gcc/testsuite/g++.dg/modules/p0713-3.C +++ b/gcc/testsuite/g++.dg/modules/p0713-3.C @@ -1,6 +1,6 @@ // { dg-additional-options "-fmodules-ts" } int k; -module frob; // { dg-error "not permitted" } +module frob; // { dg-error "only permitted as" } // { dg-prune-output "failed to read" } // { dg-prune-output "fatal error:" } // { dg-prune-output "compilation terminated" }