From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 225823858D20 for ; Mon, 12 Feb 2024 02:26:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 225823858D20 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 225823858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707704814; cv=none; b=ABKsajiit5pERkdJQrr5/e6nHxwSeOgna+Q6JJ6uQaY6faBGV1uDEnH2z/ZCfuEsiIDq8Ats+84hPbYbL07/jhSeo6OBXlbxkImPW0JIGv8UvJbmSbYDifr8jvkOhxz87lR1vwREvXK9fbEKkB7czg7IvBN0Or4uHYqag3fIB6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707704814; c=relaxed/simple; bh=ioIEpJvsRYU5AhVO15mDSvzHKCr+a7O+FE7IIn/xNQI=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=HSDqYK9U2nHxOL2HK84hrVhwOawuKXBmwAwDInnTprhFeDqPE9VOHH//94kS152w63AP/d77jds6oJlH953PVYFONFV7mGJX1ndg/+7A1dswq1dn+2gXYIygdqpYGAuTMRqvjOZvxYKRGoo+uZNJhs0Eq8ETxwfCQPwrshHmG/c= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6e2e44aad03so392925a34.2 for ; Sun, 11 Feb 2024 18:26:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707704812; x=1708309612; 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=VllzKEYXmPOixcpSo/x7FK/oOIrr3Zgg7xbVLOvo3T4=; b=AuiFDFSKXjixCAeFzk0dMpkBaPxsVWeN5CdNyhC3T21gtz1bpM2+6A5j2ranPH+g+C h/4Hktft08/vh5R7jEQFfdkCuIMyzIOJsvPh3cI70zGVyWHOJCM4fZkb5BeYgM+Sh2hY EcKAbttwr2RLktyjOg/u1Z5M2SAEHd9LDoqLwPV4bQ+E8V99rkIIQaEhueizrF8ogxVa IkUDOQG8sngbzsHtFjt6w2YEFGmdt982QLLSJ6l+h0U5lroNyqogy/WLptNoiyzOxXKh M47Izco8YpHR+BYSmj4JVolT/YJIZiKfLV9y11z5wIaNJH8UaxHkircl7kUY2e78biX4 IUcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707704812; x=1708309612; 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=VllzKEYXmPOixcpSo/x7FK/oOIrr3Zgg7xbVLOvo3T4=; b=puLL34JF8vSO3pv+bWXssQx6V57zwqTkHGZOYCqG+eKYv4nWjcFdjlR++4VhT2gP6N loZAND7HKp4/D/GYPvQZSk3n2PK1tnruoW16w2mK1YPxgPT4bSqfA2S9cmd8Rxm3iPIX Zk8sZmddIpLxLLZiqatleLeVA+tTtSi2muPKUAi3l+sJVNie2cSkGbt9yLkdunQtA4hz 4QnrNXXsguLIr3hvE/XODbkIokLRPWhL7m47swgg8F2cTO2ZKATVCUkb1JP7/F/fSGok bqBvZ1DRPlznUg85K1dgPpuF11ffXdRzfKUjKyaQgZHvxKaxzJkZyqVRTIzblRqwjpqe Mm3g== X-Gm-Message-State: AOJu0YyMCZc+DSqAL04tDR7SaYwb/v0ZQ9YuzT8lM4OhwLY3PUpOqcYv fFrNUOOZ3cgU/2BpThHBxt7VDkMFhQVW/2GtgjLdWqP2+7e53jh7dJJ61Tlv X-Google-Smtp-Source: AGHT+IHCGTimHTX5NsGZa6Qb+uy0XMTrQ80qg5YR2mSOFdBLQZKFQKTHKnte0Pevsbv4Wc84HQbpxA== X-Received: by 2002:a05:6830:1492:b0:6e2:e765:cbba with SMTP id s18-20020a056830149200b006e2e765cbbamr1915525otq.27.1707704812190; Sun, 11 Feb 2024 18:26:52 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVcRIWzR/oqtGBd/uMtRpoi+7+fVSkjCGeKiNaLLEQUeQD5S3b61/D2gtF/bTMc29dInYZnSRB5sydH6ZlJqw== Received: from Thaum. (123-243-206-49.tpgi.com.au. [123.243.206.49]) by smtp.gmail.com with ESMTPSA id lb9-20020a056a004f0900b006e086ac9d5bsm4286070pfb.67.2024.02.11.18.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 18:26:51 -0800 (PST) Message-ID: <65c981eb.050a0220.8c171.ba05@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 12 Feb 2024 13:26:47 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++: Fix error recovery when redeclaring enum in different module [PR99573] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-11.8 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: 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) { 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 +16981,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 +16992,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