From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5E3683857BAE for ; Wed, 14 Feb 2024 02:45:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E3683857BAE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E3683857BAE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707878725; cv=none; b=MbmKAZCA9HPepkWAIvuP+74Qpi8ahXnEBV5snqUZsmNWmcPeU3FzQ418Z3BOncvcq3HsGeqrQSjE62lap/WSq2gk1B6KmLm8sK0fciA6VZ6wTUB6/+NZDY50dYhRnZW4AAKlLo0waCTxqmDunHY5ahxA72ifW3Vi/15CbjDk0TE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707878725; c=relaxed/simple; bh=gkdDXKA15gk+6/MeGz31QZFuhBXIljQmXUKfwYZ2Fvc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=dKmdbXYT/ftbqA4OFyoJpPteBfcl9fnQeI7RYThHQp3qYdf4tEQQjDWScCdFjjV54lXnM+7uFtDT1Pws4uVZw84lPFadAMVECYWadFNHMcQWSnFj0jI5BRL05/j0y756Kx/g3k87Y34tDifESjvvUX2j8s36lTtmvEjrexeF5q0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707878723; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WASAd/49FbvMprBVn6tRoHCMCOS+dG7YGdzeDDiHFb4=; b=O6AMsuaH2ujK5AzATZZ3sPptpveBWTRIrtATTfrwBqRPpMfyMVvLCkPWW47AY8X47snX5v khqUaLGidkU1i4CM6YNfISYnSPhJPHgGNHN7GJ2DzUS60IW4gJrE5RCmgrNhkHEGcBuAln PB5wP/0G58x5X4JApHxKGbuaTcnudLM= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-aF_YDxakNp2BEwz8OlczUQ-1; Tue, 13 Feb 2024 21:45:22 -0500 X-MC-Unique: aF_YDxakNp2BEwz8OlczUQ-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-68c4f2aca5dso90026036d6.3 for ; Tue, 13 Feb 2024 18:45:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707878721; x=1708483521; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WASAd/49FbvMprBVn6tRoHCMCOS+dG7YGdzeDDiHFb4=; b=PjrC2SKrqNFCsSchNnQXsMAfuk8ssczdv2UP5G3Bjgxr8jQUJLxxWlXGSxE6tRRCpl eRCdNrQgS//RR3A25+AvYvctLF4oJESIaAzXqvONMg99L2xvfyLLVOY2cORTYxR4nVQa KwuxyY0SJdN/4aQpilGbqD3tFqgKqfHysIW4/3zAUr2ApP/DSLb2x9sl71RCb1C0yabY APYNJNG9MEVNBEcdaOqCGvh+druJ7rgQunMoaBbDmZRYXtLZa1JWvNchJZJJ6kP9u50j rSlp/q/feV0OUzAOgoZ2f+8huknut3i3sEvOPsu1PH3ZTEKjQUfJGDUuNhJ6kDMKQK5n Vzmg== X-Gm-Message-State: AOJu0YwRkFmMGgMo3vsHHoSBIs/rNWORMtbjeFgiCMGDQiQsyWcBi67M H/SG5dFky25yY1hW2+zzUgl1URGZLrzWI7Lgh4ze5kFB5QnMTxDOGUuwiMIwl3Y5W6xDIL6JnIK 56OBllQP9dU5DHqCXqN4SCBF19C4FwhylUsN4ZHng8y+MDaPI5e+dJAg= X-Received: by 2002:ac8:59c8:0:b0:42c:dcd7:bbc9 with SMTP id f8-20020ac859c8000000b0042cdcd7bbc9mr1419022qtf.38.1707878721497; Tue, 13 Feb 2024 18:45:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxuDnoQm0KIodUmuSLuVUgzkzHG+j9TuKCbze3BVo5XARlyR5CWXO1Rgt1+5Rh+MJVT4kPPg== X-Received: by 2002:ac8:59c8:0:b0:42c:dcd7:bbc9 with SMTP id f8-20020ac859c8000000b0042cdcd7bbc9mr1419013qtf.38.1707878721185; Tue, 13 Feb 2024 18:45:21 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUFqIyqtVCaiYkTa/qwDQEkyrw8dVqAYCIoO0rRyAOYdJj2x7OV9c+P6LI+HpbHl4AFSMhKn5+c9CUIL8Rq2Q== Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id z15-20020ac86b8f000000b0042c8035641fsm1639963qts.31.2024.02.13.18.45.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Feb 2024 18:45:20 -0800 (PST) Message-ID: <147913bf-53a2-4190-9b19-896bf327f28c@redhat.com> Date: Tue, 13 Feb 2024 21:45:20 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: Fix error recovery when redeclaring enum in different module [PR99573] To: Nathaniel Shead Cc: gcc-patches@gcc.gnu.org, Nathan Sidwell References: <65c981eb.050a0220.8c171.ba05@mx.google.com> <3b56cd97-0a20-4b72-8352-6625c5cec9cb@redhat.com> <65cbf94e.620a0220.d1b24.94fa@mx.google.com> From: Jason Merrill In-Reply-To: <65cbf94e.620a0220.d1b24.94fa@mx.google.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,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 2/13/24 18:20, Nathaniel Shead wrote: > 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? OK. > -- >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" }