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.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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683308214; 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; bh=sJWB+DOPMdM9exYllQYhx/DRDhOdKHE+jp8VnZNEPWA=; b=Siq4CAtj5gp0/G3cRkf5uAdubMSRwllWdzNZ0pCu/rLR2/kgX0kXtsKZXukDW7qxtpoSW8 iPsoht3UG+QhRUVC6nHpdofYy23/NXOg+QKc64ALw475Xn1DUFt/Z+QteY7IK5jPBQsOCA nHI03FutTyWVn/7IFKRAXI40Kfb9bYw= 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-Google-Smtp-Source: ACHHUZ4jg0KN1byaziQWBcWOY25BZNn+zbKQW5S4w0DUySku2SNIaIf3XVC3wmnBMuwkOY8qCaTPng== 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) From: Patrick Palka 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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: ; } -- 2.40.1.476.g69c786637d