From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id CE2973857C5B for ; Tue, 13 Feb 2024 23:20:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE2973857C5B 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 CE2973857C5B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707866449; cv=none; b=xw362PxPI2OC8csbdva/9VPuQ9WEos3xqKbmxFSuMKorz5d3iV76g7yjnum6Sz6u+Y4I6sXlOpXstlKGQQNd4naTelq2F0qZHfap2sQylWAxLPCpqjz/2ZpntJ1MY5uFdULzRiAstjnVSBgUanE7TWF2+a/G1q1Xphp7xca+/Go= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707866449; c=relaxed/simple; bh=9eVeXXckqUH3nJCVqUH1kJWJGZdRNLfn+d5gLx2LWiw=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=in9goBKUVbOQd80H/D6l7qOhYAoxRqu4/JIOWMxst88wSVq1v1qQwkV5uB4+qaxyYQF5Bt5sxniDQ/jxd5bJAXdiE+zS9iCWV0Sh7Z7ru5GRRogQvjpvofSBCVEnF1pnN9S0J0b4jKItXncIogV2dc7GJUZ8XIWfQb5OrjqbFEQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6e104196e6eso708529b3a.0 for ; Tue, 13 Feb 2024 15:20:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707866446; x=1708471246; darn=gcc.gnu.org; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=t8sjVYxuzV5BrpuF9ehPhM8V9Tl9/dSVTse/526RSDc=; b=Hju4PW3RNciUtP+b3n/nJFEcquEnZshGGzqJj2Ks6ZRFlBdKW3cSQtgevLnpxwkaN0 kImIQWP+DPziBF8kCRuaQIS0G5OoKGlvFnOJdIRSvEOab3JECpikGsEtmxSckYbnB7AC PtRp5lP+Ny+HpGqjlodB4gLf8UtoMvM+m43iztGhWrNuNZAUDURn+/Kl+j55dNOrNbCh 2HodKq4Qqc9aTMENDrprGJiP6k46lzaVZ6tlpAqWSR0OWhMXVDXMDnLd/2IxOkSw/8qa iXDXAN9a7JXeDmdWOhML4XiYErDjkxUQjbJt26syFjtB0crjN2fTyYfeK2hdYMluogst F3rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707866446; x=1708471246; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=t8sjVYxuzV5BrpuF9ehPhM8V9Tl9/dSVTse/526RSDc=; b=w2Ag+DkRfW91WHF8yhIkw7Gsfykc4oeKUJvCuWjyppLWPO7JVZcmWc+REZNWd6ClBS 3wdXrlKrwO1VCVXQTvqiB5AJku5ZJIZRSnxGGl4vbOrlo60KJOicUouqcOili+hv+d9s QljQC5MtfwaHGE7M/xMLhY+ArS0fEpfaCOdh89OjfNUuuQWkj6XdIixlSM/GkF8n6ftc N0RTVB4uyEYNHnByZ4x5OSHosJXUnaBlMVHxVy5qGrs154Pp2tPjCwy/XLilFjiLMIho cgN7kXPs1r4voXULclg9hILiFPbhtPeMuh9s3BBnOthRkI1AoKGpisEhcz5awF+v7ABo sIMA== X-Gm-Message-State: AOJu0Yw7FWblL2mks+aS7r29+Qsa/1bY/hyuI4hzSE7zc5PGsAdPy7xi oWqtTuKbmXL2snqJ6TTqymOpVXlpAi0EoTnTxn/6dAj4GJ0mgx3n X-Google-Smtp-Source: AGHT+IHwg3JSw5OzsjtoJ9VS47oJaKUxFz/MhAM9GslIZpFNDwyZg6U5irIhRXnxnHq5ZsYC4qCz3w== X-Received: by 2002:a05:6a21:3489:b0:19e:c0dd:5dc1 with SMTP id yo9-20020a056a21348900b0019ec0dd5dc1mr1244964pzb.33.1707866446454; Tue, 13 Feb 2024 15:20:46 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUdSWjI/QUbF4C//zt1ubhOkQGcSNJOzj9Uym0RqKTH31pgBGawqXmNDGO5i6jnfd5vBPrG/4MtwvGe1HD3NQ== Received: from Thaum. ([210.84.33.179]) by smtp.gmail.com with ESMTPSA id z5-20020a62d105000000b006e04e58f216sm8331455pfg.137.2024.02.13.15.20.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 15:20:46 -0800 (PST) Message-ID: <65cbf94e.620a0220.d1b24.94fa@mx.google.com> X-Google-Original-Message-ID: Date: Wed, 14 Feb 2024 10:20:39 +1100 From: Nathaniel Shead To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Nathan Sidwell Subject: Re: [PATCH] c++: Fix error recovery when redeclaring enum in different module [PR99573] References: <65c981eb.050a0220.8c171.ba05@mx.google.com> <3b56cd97-0a20-4b72-8352-6625c5cec9cb@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3b56cd97-0a20-4b72-8352-6625c5cec9cb@redhat.com> X-Spam-Status: No, score=-12.1 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, Feb 13, 2024 at 06:12:51PM -0500, Jason Merrill wrote: > On 2/11/24 21:26, Nathaniel Shead wrote: > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? > > > > -- >8 -- > > > > This ensures that with modules enabled, redeclaring an enum in the wrong > > module or with the wrong underlying type no longer ICEs. > > > > The patch also rearranges the order of the checks a little because I > > think it's probably more important to note that you can't redeclare the > > enum all before complaining about mismatched underlying types etc. > > > > As a drive by this patch also adds some missing diagnostic groups, and > > rewords the module redeclaration error message to more closely match the > > wording used in other places this check is done. > > > > PR c++/99573 > > > > gcc/cp/ChangeLog: > > > > * decl.cc (start_enum): Reorder check for redeclaring in module. > > Add missing auto_diagnostic_groups. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/modules/enum-12.C: New test. > > > > Signed-off-by: Nathaniel Shead > > --- > > gcc/cp/decl.cc | 31 +++++++++++++++----------- > > gcc/testsuite/g++.dg/modules/enum-12.C | 10 +++++++++ > > 2 files changed, 28 insertions(+), 13 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/modules/enum-12.C > > > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > > index 3e41fd4fa31..f982b5f88de 100644 > > --- a/gcc/cp/decl.cc > > +++ b/gcc/cp/decl.cc > > @@ -16951,12 +16951,28 @@ start_enum (tree name, tree enumtype, tree underlying_type, > > /*tag_scope=*/TAG_how::CURRENT_ONLY, > > /*template_header_p=*/false); > > + /* Attempt to set the declaring module. */ > > + if (modules_p () && enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) > > + { > > + tree decl = TYPE_NAME (enumtype); > > + if (!module_may_redeclare (decl)) > > + { > > + auto_diagnostic_group d; > > + error ("cannot declare %qD in different module", decl); > > + inform (DECL_SOURCE_LOCATION (decl), "previously declared here"); > > + enumtype = error_mark_node; > > + } > > + else > > + set_instantiating_module (decl); > > + } > > + > > /* In case of a template_decl, the only check that should be deferred > > to instantiation time is the comparison of underlying types. */ > > if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) > > { > > How about moving the module checks here, instead of a few lines higher, > where you need to duplicate the ENUMERAL_TYPE condition? So like this? -- >8 -- This ensures that with modules enabled, redeclaring an enum in the wrong module or with the wrong underlying type no longer ICEs. The patch also rearranges the order of the checks a little because I think it's probably more important to note that you can't redeclare the enum all before complaining about mismatched underlying types etc. As a drive by this patch also adds some missing diagnostic groups, and rewords the module redeclaration error message to more closely match the wording used in other places this check is done. PR c++/99573 gcc/cp/ChangeLog: * decl.cc (start_enum): Reorder check for redeclaring in module. Add missing auto_diagnostic_groups. gcc/testsuite/ChangeLog: * g++.dg/modules/enum-12.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/decl.cc | 35 +++++++++++++++----------- gcc/testsuite/g++.dg/modules/enum-12.C | 10 ++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/enum-12.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 3e41fd4fa31..d19d09adde4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -16955,8 +16955,26 @@ start_enum (tree name, tree enumtype, tree underlying_type, to instantiation time is the comparison of underlying types. */ if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) { - if (scoped_enum_p != SCOPED_ENUM_P (enumtype)) + /* Attempt to set the declaring module. */ + if (modules_p ()) { + tree decl = TYPE_NAME (enumtype); + if (!module_may_redeclare (decl)) + { + auto_diagnostic_group d; + error ("cannot declare %qD in different module", decl); + inform (DECL_SOURCE_LOCATION (decl), "previously declared here"); + enumtype = error_mark_node; + } + else + set_instantiating_module (decl); + } + + if (enumtype == error_mark_node) + ; + else if (scoped_enum_p != SCOPED_ENUM_P (enumtype)) + { + auto_diagnostic_group d; error_at (input_location, "scoped/unscoped mismatch " "in enum %q#T", enumtype); inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), @@ -16965,6 +16983,7 @@ start_enum (tree name, tree enumtype, tree underlying_type, } else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type) { + auto_diagnostic_group d; error_at (input_location, "underlying type mismatch " "in enum %q#T", enumtype); inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), @@ -16975,25 +16994,13 @@ start_enum (tree name, tree enumtype, tree underlying_type, && !same_type_p (underlying_type, ENUM_UNDERLYING_TYPE (enumtype))) { + auto_diagnostic_group d; error_at (input_location, "different underlying type " "in enum %q#T", enumtype); inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), "previous definition here"); underlying_type = NULL_TREE; } - - if (modules_p ()) - { - if (!module_may_redeclare (TYPE_NAME (enumtype))) - { - error ("cannot define %qD in different module", - TYPE_NAME (enumtype)); - inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)), - "declared here"); - enumtype = error_mark_node; - } - set_instantiating_module (TYPE_NAME (enumtype)); - } } if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE diff --git a/gcc/testsuite/g++.dg/modules/enum-12.C b/gcc/testsuite/g++.dg/modules/enum-12.C new file mode 100644 index 00000000000..57eeb85d92a --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/enum-12.C @@ -0,0 +1,10 @@ +// PR c++/99573 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi !foo } + +export module foo; +namespace std { + enum class align_val_t : decltype(sizeof(int)) {}; // { dg-error "different module" } +} + +// { dg-prune-output "not writing module" } -- 2.43.0