From patchwork Thu May 23 14:54:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1938369 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=ch8lrm3o; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VlWRf2XyRz1ynR for ; Fri, 24 May 2024 00:54:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4642A386C589 for ; Thu, 23 May 2024 14:54:51 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id ECD703858C62 for ; Thu, 23 May 2024 14:54:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ECD703858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ECD703858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716476067; cv=none; b=ce9UpcQdDYDQX1rruKhzgcIuqa47SwtR0rAZG1iaeSh1QEqsRYVdgLbMJXRCsLiwa0fszXV+6qMBTW+o3RxwiZK3Fte7a+vga0h8QwUS74syWuVi2TLdEXxgX/PkqpVjeLUT8WtCX8s4st26qZQMl83KvbUuiJWrQrrGBZjiHSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716476067; c=relaxed/simple; bh=DWjrz63DQ06nEKeSDmSLuXlycSreToGa4UjC06QlQTQ=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=cq+PzlExmt10aDVZwfXPwtpjgSkyQc6BuCfSdz7HOrsPMHkx7eR01W5BoaV29rrSTdoGM11EcrcvGrwuHUnYTqVqeetLNNGkqc8cAxPuhSgeBQezAv7OeBHsYN6xJqWCZW1O4EOTkZQP+9QTRc3lof6wVmRT+Wl8BEIJAS2U02A= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1ee7963db64so55308495ad.1 for ; Thu, 23 May 2024 07:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716476064; x=1717080864; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=6dEpr7i+IrKcQvlGGc2DXO+nOPVifMg9LpWyk5u1iNI=; b=ch8lrm3ouEJ3+AE/myTY2B0HtrL+vw2Qq+daE8jWXR8YoikuGtUHL/Gsh354RePuTi H2g5G8DUcTQZJO+iAHoy85Lw68D08YKV9avd0Vk86hKUO7AIxTboDEwt8nD/u8ayahI2 uQA+K8jzxH63btGBl7DBvLeOGKLKuQ5BnCzZ3B62FVgidr6jfVNwWfeQAnvlWEDHCGrk DjbpFXpkkkQ+dwzKDs0JaDkm33lX+fjJ3N2HlDtdj36GlTaA9RTcNAZPT9auSvFxtDvx KzQaP8Fe+iBvtBekIGrIXY8PepglnsJ0ym8PVnqBt3HzaszSm6K2xds89gz3StFZPvjQ XO+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716476064; x=1717080864; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6dEpr7i+IrKcQvlGGc2DXO+nOPVifMg9LpWyk5u1iNI=; b=OoCY5gpwWuHLpnTowrUGroHm5bkxC0R+/EzdT2vKqAZF7ac8sIaIiTGrUOWv2BOZQS Z/TGxWKkagsa0aZt7BsjnSD8QC1u7fzjj3f5yL5iBhHsI9V3PacU3aqIVaR59RvF0d4g k67Erv6jqXHXuqEAopB25KTCL53iY+LyG5T0N9ze1RnWMMihITho/XnNo+YixjbrVB9J zZ3QJcS7RDSiu3rsqvdkH02gQz9SCciCVplYj5IWnGQ0xTpMus5LeSJYJ3KS6JmPulcQ 0XPnUeKUqh+OOX3S14k5PjQOm3lWKa075wA6t8RYRVcnHZI9rzHY6GKNOG8zB0O1CL6C DXpA== X-Gm-Message-State: AOJu0YyUPHguJqk8pAwLIsyZx/dLYHUlZqDXW984Yii2sh4eAvNciHGi y4b6koqVWzRXhaBBhqmDK9ASQXctmvId/eiZoafP5ixj+CoSdDvuRxoB3Q== X-Google-Smtp-Source: AGHT+IESyoUPLbspW1Z4u801dMQseaL8qfqmJM8dmAi7ZQ7phHwb0b0QMSBaMfA9/QKZiQ5pwu/SHw== X-Received: by 2002:a17:90b:4392:b0:2bf:3859:d5a5 with SMTP id 98e67ed59e1d1-2bf3859d642mr167487a91.11.1716476063711; Thu, 23 May 2024 07:54:23 -0700 (PDT) Received: from Thaum. (14-200-72-253.tpgi.com.au. [14.200.72.253]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2bdd9f1bb2csm1648561a91.37.2024.05.23.07.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 07:54:23 -0700 (PDT) Message-ID: <664f589f.170a0220.a256e.489c@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 24 May 2024 00:54:17 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Improve errors for bad module-directives [PR115200] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested (so far just modules.exp and dg.exp) on x86_64-pc-linux-gnu, OK for trunk if full regtest succeeds? -- >8 -- 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. 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. * g++.dg/modules/mod-decl-8.h: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/parser.cc | 32 +++++++++++++++++++++++ gcc/testsuite/g++.dg/modules/mod-decl-1.C | 6 ++--- gcc/testsuite/g++.dg/modules/mod-decl-6.C | 11 ++++++++ gcc/testsuite/g++.dg/modules/mod-decl-7.C | 12 +++++++++ gcc/testsuite/g++.dg/modules/mod-decl-8.C | 9 +++++++ gcc/testsuite/g++.dg/modules/mod-decl-8.h | 4 +++ 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/mod-decl-6.C create mode 100644 gcc/testsuite/g++.dg/modules/mod-decl-7.C create mode 100644 gcc/testsuite/g++.dg/modules/mod-decl-8.C create mode 100644 gcc/testsuite/g++.dg/modules/mod-decl-8.h diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 476ddc0d63a..1c0543ba154 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -3230,6 +3230,31 @@ cp_parser_error_1 (cp_parser* parser, const char* gmsgid, return; } + if (cp_token_is_module_directive (token)) + { + cp_token *next = (token->keyword == RID__EXPORT + ? cp_lexer_peek_nth_token (parser->lexer, 2) : token); + + auto_diagnostic_group d; + error_at (token->location, "unexpected module directive"); + tree scope = current_scope (); + if (next->keyword == RID__MODULE + && token->main_source_p + && scope != global_namespace) + { + /* Nicer error for unterminated scopes in GMF includes. */ + inform (token->location, + "module-declaration must be at global scope"); + inform (location_of (scope), "scope opened here"); + } + else + inform (token->location, "perhaps insert a line break, or other" + " disambiguation, to prevent this being considered a" + " module control-line"); + 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 +15160,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)) { diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-1.C b/gcc/testsuite/g++.dg/modules/mod-decl-1.C index 23d34483dd7..84fa31c7024 100644 --- a/gcc/testsuite/g++.dg/modules/mod-decl-1.C +++ b/gcc/testsuite/g++.dg/modules/mod-decl-1.C @@ -10,17 +10,17 @@ 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" } }; void Bink () { - module fifth; // { dg-error "expected" } + module fifth; // { dg-error "unexpected module directive" } } 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 index 00000000000..5fb342455e5 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/mod-decl-6.C @@ -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 index 00000000000..87eec0d2697 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/mod-decl-7.C @@ -0,0 +1,12 @@ +// PR c++/115200 +// { dg-additional-options "-fmodules-ts -Wno-global-module" } +// { dg-module-cmi !M } + +module; + +void foo() { // { dg-message "scope opened here" } + +export module M; // { dg-error "unexpected module directive" } + // { dg-message "global scope" "" { target *-*-* } .-1 } + +} 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 index 00000000000..50e125ce1bc --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/mod-decl-8.C @@ -0,0 +1,9 @@ +// { dg-additional-options "-fmodules-ts" } +// { dg-prune-output "not writing module" } + +module; + +#include "mod-decl-8.h" +// { dg-error "module control-line|unexpected module directive" "" { target *-*-* } 0 } + +export module M; diff --git a/gcc/testsuite/g++.dg/modules/mod-decl-8.h b/gcc/testsuite/g++.dg/modules/mod-decl-8.h new file mode 100644 index 00000000000..8ce03f04399 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/mod-decl-8.h @@ -0,0 +1,4 @@ +struct module { int r; }; +void foo() { + module x; +}