From patchwork Fri May 5 17:36:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 90524 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp579585vqo; Fri, 5 May 2023 10:37:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74XnbpOFD4TLlykhSQ9yl/AxCVDbrRzRNSp2gwUIjyRl57HIHgHai/nF1gpMTd2sdEdyvr X-Received: by 2002:a17:907:3686:b0:94f:2852:1d2b with SMTP id bi6-20020a170907368600b0094f28521d2bmr1544394ejc.72.1683308278124; Fri, 05 May 2023 10:37:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683308278; cv=none; d=google.com; s=arc-20160816; b=LfKNsAKT9RIP35gal2QtIqNY13kTwsMd/WY5pEAlXCfkKS/ccoi0U8D9390Qzqwo2a iIlYxyzjYhmSDzkz/gxH8IdcKoUvltb5aIoSmP3Azhj9Yzp4/1alGntrl3gUO8LoUklU UCFtho7hTHEiaR4AfUq/zp05moyN6oPBRVEtvC8uG3RtGSXslyWfs6ihyF7NhO4fLemm 2kcyRW+ylqxZST3SQMwmeGjO+zK5wdxJyRAkvgC5FCzBpisQRYih+MH/TbygoGvTM8ST a3tzw5ottIAyUqeiSMbFJbIZEothseC4ObuDsdlRNKx9WTAeL3MPEv5g2pCo/BrBYNS2 Z3jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=sJWB+DOPMdM9exYllQYhx/DRDhOdKHE+jp8VnZNEPWA=; b=iGPR5T9H1NvvtMnkSU6U8p77tBx1QdM6+ws4qSniyurr3NshDGFI20PXD89OKwejgH HbyVp+21PDWLU2Cs5m9NUUKjWoACR98p1xqMD89eYjAqR/Kzb8WWptCd6ukR0VAZPNgZ vAzLHJZrqcJqzZBO3492sXCEZwAjcZQJQVUAQMbhKRy6Y5dhaxWTpy0lUBBca6RtvB7J iUMdkscmMLsjpq7NeO4VpNy8Cb99Cq3t+wk2mdElNMwpQuJHPnfxGfWNoNncaxJAC6ny YJav4yZ1iRpQ2IJQodoRKFfOqtd7DpPkj/iSCW/O5IDZvQDXdVRDWDd+mmI8U9ekA3jb +P6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=aRNykOeJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w10-20020a170906d20a00b00965bd5200b5si1549479ejz.611.2023.05.05.10.37.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 May 2023 10:37:58 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=aRNykOeJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D2A963856090 for ; Fri, 5 May 2023 17:37:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D2A963856090 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683308272; bh=sJWB+DOPMdM9exYllQYhx/DRDhOdKHE+jp8VnZNEPWA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=aRNykOeJVEkkdDV9P3Mg47jMYC+a/Jr+gYQOLwiyKVJqwHBBkO06Gd8eYDREfjl8M X8CRx/s2ASTN14Xe05AG91r3oZzIF9EC5xKwmTfcIXbjDBszPi2KZEv2kyoOSMtByE HpK7IOlUbluw1kQ6yYtplC8EYD+DbYj8tIIpscfo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 608C43856DDF for ; Fri, 5 May 2023 17:36:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 608C43856DDF Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-16-qayDe-2mPhOZUhF485SxQg-1; Fri, 05 May 2023 13:36:52 -0400 X-MC-Unique: qayDe-2mPhOZUhF485SxQg-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-751319bd93cso1257763485a.0 for ; Fri, 05 May 2023 10:36:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683308211; x=1685900211; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sJWB+DOPMdM9exYllQYhx/DRDhOdKHE+jp8VnZNEPWA=; b=afwabVPPyvYCp+XSPNs5dNhUvkLYo1c9jMLcPrOa+mCK53x3oxncM6dgXVsGkvShG3 uNVeZ2RSOjdR6Qrm9Sih3v6URDpQSEY1GoS+tQAAL1F7YFKGAkipjpnDe3I6xRrnPYRC A8+VO0GmhU/DWDKGjgwwnwDbrYJCrQo0HbeAtQ+lg/uH3ybgho5E11I9a0oHwsu4vm9r RJCditMHErwWPNk+jDF6jd/37gmGUuopIQGA551N8eAFc6WSCFMlcNZHXCHWWJZOGV8t LtdhAOBbUbe+f9ZyC4ItwzO5w203VFwA9IkdnzxstNF1D0LV9xRtfcRfK8LgJfh92aFg NZtA== X-Gm-Message-State: AC+VfDxhdOb4MIp6FZV1AaCekwumikdUgEX8KSfVdKYn2zeI/4nP8Zdq iJAlP5Tq9GLjl4pw4hCWbQS/7dWnH8teegBnmJy6tLGh431f/UxMjVWLQ4MJwNHW/psvyTxnTWz VOse14ZFIHYXA1cWJkST6+jHmg6bHtjMQ9IyHbznuGBR/C4GziSZky7+UuGcyNkiCY2qrfLK8ao c= X-Received: by 2002:ad4:5be4:0:b0:5e7:c44f:5098 with SMTP id k4-20020ad45be4000000b005e7c44f5098mr14761689qvc.13.1683308211304; Fri, 05 May 2023 10:36:51 -0700 (PDT) X-Received: by 2002:ad4:5be4:0:b0:5e7:c44f:5098 with SMTP id k4-20020ad45be4000000b005e7c44f5098mr14761650qvc.13.1683308210814; Fri, 05 May 2023 10:36:50 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id e29-20020a0cb45d000000b005dd8b9345cdsm730656qvf.101.2023.05.05.10.36.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 May 2023 10:36:50 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: goto entering scope of obj w/ non-trivial dtor [PR103091] Date: Fri, 5 May 2023 13:36:48 -0400 Message-ID: <20230505173648.1593931-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.1.476.g69c786637d MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765076661182327993?= X-GMAIL-MSGID: =?utf-8?q?1765076661182327993?= It seems DR 2256 permitted goto to cross the initialization of a trivially initialized object with a non-trivial destructor. We already supported this as an -fpermissive extension, so this patch just makes us unconditionally support this. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? DR 2256 PR c++/103091 gcc/cp/ChangeLog: * decl.cc (decl_jump_unsafe): Return bool instead of int. Don't consider TYPE_HAS_NONTRIVIAL_DESTRUCTOR. (check_previous_goto_1): Simplify now that decl_jump_unsafe returns bool instead of int. (check_goto): Likewise. gcc/testsuite/ChangeLog: * g++.old-deja/g++.other/init9.C: Don't expect diagnostics for goto made valid by DR 2256. * g++.dg/init/goto4.C: New test. --- gcc/cp/decl.cc | 56 ++++++-------------- gcc/testsuite/g++.dg/init/goto4.C | 22 ++++++++ gcc/testsuite/g++.old-deja/g++.other/init9.C | 7 +-- 3 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 gcc/testsuite/g++.dg/init/goto4.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 71d33d2b7a4..23a2b2fef0b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -69,7 +69,7 @@ enum bad_spec_place { static const char *redeclaration_error_message (tree, tree); -static int decl_jump_unsafe (tree); +static bool decl_jump_unsafe (tree); static void require_complete_types_for_parms (tree); static tree grok_reference_init (tree, tree, tree, int); static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *, @@ -3548,10 +3548,9 @@ declare_local_label (tree id) return ent ? ent->label_decl : NULL_TREE; } -/* Returns nonzero if it is ill-formed to jump past the declaration of - DECL. Returns 2 if it's also a real problem. */ +/* Returns true if it is ill-formed to jump past the declaration of DECL. */ -static int +static bool decl_jump_unsafe (tree decl) { /* [stmt.dcl]/3: A program that jumps from a point where a local variable @@ -3562,18 +3561,11 @@ decl_jump_unsafe (tree decl) preceding types and is declared without an initializer (8.5). */ tree type = TREE_TYPE (decl); - if (!VAR_P (decl) || TREE_STATIC (decl) - || type == error_mark_node) - return 0; - - if (DECL_NONTRIVIALLY_INITIALIZED_P (decl) - || variably_modified_type_p (type, NULL_TREE)) - return 2; - - if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) - return 1; - - return 0; + return (type != error_mark_node + && VAR_P (decl) + && !TREE_STATIC (decl) + && (DECL_NONTRIVIALLY_INITIALIZED_P (decl) + || variably_modified_type_p (type, NULL_TREE))); } /* A subroutine of check_previous_goto_1 and check_goto to identify a branch @@ -3625,27 +3617,18 @@ check_previous_goto_1 (tree decl, cp_binding_level* level, tree names, new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls) : TREE_CHAIN (new_decls))) { - int problem = decl_jump_unsafe (new_decls); + bool problem = decl_jump_unsafe (new_decls); if (! problem) continue; if (!identified) { - complained = identify_goto (decl, input_location, locus, - problem > 1 - ? DK_ERROR : DK_PERMERROR); + complained = identify_goto (decl, input_location, locus, DK_ERROR); identified = 1; } if (complained) - { - if (problem > 1) - inform (DECL_SOURCE_LOCATION (new_decls), - " crosses initialization of %q#D", new_decls); - else - inform (DECL_SOURCE_LOCATION (new_decls), - " enters scope of %q#D, which has " - "non-trivial destructor", new_decls); - } + inform (DECL_SOURCE_LOCATION (new_decls), + " crosses initialization of %q#D", new_decls); } if (b == level) @@ -3790,9 +3773,9 @@ check_goto (tree decl) FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad) { - int u = decl_jump_unsafe (bad); + bool problem = decl_jump_unsafe (bad); - if (u > 1 && DECL_ARTIFICIAL (bad)) + if (problem && DECL_ARTIFICIAL (bad)) { /* Can't skip init of __exception_info. */ if (identified == 1) @@ -3806,15 +3789,8 @@ check_goto (tree decl) saw_catch = true; } else if (complained) - { - if (u > 1) - inform (DECL_SOURCE_LOCATION (bad), - " skips initialization of %q#D", bad); - else - inform (DECL_SOURCE_LOCATION (bad), - " enters scope of %q#D which has " - "non-trivial destructor", bad); - } + inform (DECL_SOURCE_LOCATION (bad), + " skips initialization of %q#D", bad); } if (complained) diff --git a/gcc/testsuite/g++.dg/init/goto4.C b/gcc/testsuite/g++.dg/init/goto4.C new file mode 100644 index 00000000000..90ae52e6455 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/goto4.C @@ -0,0 +1,22 @@ +// DR 2256 +// PR c++/103091 +// { dg-do run } + +static int i; + +struct A { + ~A() { ++i;} +}; + +void f() { + goto L; + A a; +L: + return; +} + +int main() { + f(); + if (i != 1) + __builtin_abort(); +} diff --git a/gcc/testsuite/g++.old-deja/g++.other/init9.C b/gcc/testsuite/g++.old-deja/g++.other/init9.C index 46d99028bdd..0194cd1b49a 100644 --- a/gcc/testsuite/g++.old-deja/g++.other/init9.C +++ b/gcc/testsuite/g++.old-deja/g++.other/init9.C @@ -24,9 +24,10 @@ struct X { }; void b() { - goto bar; // { dg-message "" } jump from here - X x; // { dg-message "" } jump crosses initialization - bar: // { dg-error "" } jump to here + // This was ill-formed until DR 2256. + goto bar; + X x; + bar: ; }