From patchwork Thu Aug 31 07:20:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 137235 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp73986vqu; Thu, 31 Aug 2023 00:21:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGx/+vyCjRo+i9rwSLaWrDwSbQMc4Zc7D/e/LKaZhBvmCQ/kDC/1Qs1kWYFULKvcf0C5gon X-Received: by 2002:aa7:d898:0:b0:523:ba10:d795 with SMTP id u24-20020aa7d898000000b00523ba10d795mr3862808edq.11.1693466464892; Thu, 31 Aug 2023 00:21:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693466464; cv=none; d=google.com; s=arc-20160816; b=eYQg8Fb+cEwrxJK9NEewivNhq72ZbjDjNh3Wz48a+i6inEZ6+YSQAiStmMqkeMQyGa +ON9fUT8D6JxbSXz1a6YjPxxJuyxWgY1XwcKk/yNzOTv0O0hNOQkIyU+YSxjiec/zH4z fCJp7SqxpZWkoD8k64Hj6OMnnUD4uRvX59OpA7w7JsnkwcE55iLaIITRKkM1rWKZKXJv X3XMS2hEl4WDbnD/DgOx19hrqhpnb6yxBwFKhRvYW+Uycd+9//Ne7QbkVvhcj6XMcNt1 P8PFrtLrjtbSHR2Yw99FgBVIHH/vqBHX+nfBpVxj1dNdbvlCz9RdII7R8/Z0c1TYO22p FdJw== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=1gSAqENLj8uOIjRS0yZhYaG1ga/HITzQwfOJh9zQATI=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=0KAQhfMxKsolo/8fdyT8H+gFqSg1QYPBVMMseELjX3/8VxnTt/kEXJ0nNWMupNmZNK e5pdrudg+JGY5drI8RXRNo9OznbG1LUIH7erZhxc8s1OqTh47qrJZwkS6SRB8yllrXa0 NdFObOUjY32pFGZM9JeA5UQzsxN+B8LC/3Ak4N3BkWQdYQ8FQFus1NgCHq+F9auKZq4Y TprWuKOWd8c5Vh7tI5uKWg1hQGmf3A31fjLlfiq1abd2gR6LDZ8XgubApYsNCYMmKRl3 jCihMve3hzCLzcg7elGc5yXl/9PP4NQelp/ZLHRhN8xfw9tX0IkdSZ+rhs5Le8ylt7A3 ZCnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WYCtJ9xu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f18-20020a056402161200b005236885749csi640336edv.431.2023.08.31.00.21.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 00:21:04 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WYCtJ9xu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 57E033858291 for ; Thu, 31 Aug 2023 07:21:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57E033858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693466463; bh=1gSAqENLj8uOIjRS0yZhYaG1ga/HITzQwfOJh9zQATI=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=WYCtJ9xuG3KzyGjbn8wTCdB3hgR+nIi7aJYT2kLHTpkuxON+YN6oH+YkOKwm6KqxK CQp/rJO2Lcne8aQWu2ZffwTYdaJr2aZsAgp293kYs9K/Cv6yAKTIiDr8QrqSVeQIRl qTrUQNF/vROklvxLkBjpo0inp3vfXijmYzlyC8vg= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id E4CA73858C62 for ; Thu, 31 Aug 2023 07:20:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4CA73858C62 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-561-NrlQQgmjOa6MNL2ssODrLQ-1; Thu, 31 Aug 2023 03:20:17 -0400 X-MC-Unique: NrlQQgmjOa6MNL2ssODrLQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8D7B129AB3E3 for ; Thu, 31 Aug 2023 07:20:17 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C7911121315; Thu, 31 Aug 2023 07:20:17 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37V7KFOX264949 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 31 Aug 2023 09:20:16 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37V7KFLb264948; Thu, 31 Aug 2023 09:20:15 +0200 Date: Thu, 31 Aug 2023 09:20:15 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Diagnose [basic.scope.block]/2 violations even in compound-stmt of function-try-block [PR52953] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775728292296114770 X-GMAIL-MSGID: 1775728292296114770 Hi! As the following testcase shows, while check_local_shadow diagnoses most of the [basic.scope.block]/2 violations, it doesn't diagnose when parameter's name is redeclared inside of the compound-stmt of a function-try-block. There is in that case an extra scope (sk_try with parent artificial sk_block with for FUNCTION_NEEDS_BODY_BLOCK another sk_block and only then sk_function_param). The following patch fixes that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-08-31 Jakub Jelinek PR c++/52953 * cp-tree.h (struct language_function): Add x_in_function_try_block member. * semantics.cc (begin_function_try_block): Set it. (finish_function_try_block): Clear it. * name-lookup.cc (check_local_shadow): If in_function_try_block and current_binding_level->kind == sk_try, skip another level. * g++.dg/diagnostic/redeclaration-3.C: New test. Jakub --- gcc/cp/cp-tree.h.jj 2023-08-30 10:42:53.579169729 +0200 +++ gcc/cp/cp-tree.h 2023-08-30 19:28:20.712246064 +0200 @@ -2110,6 +2110,7 @@ struct GTY(()) language_function { BOOL_BITFIELD returns_null : 1; BOOL_BITFIELD returns_abnormally : 1; BOOL_BITFIELD infinite_loop: 1; + BOOL_BITFIELD in_function_try_block : 1; BOOL_BITFIELD x_in_function_try_handler : 1; BOOL_BITFIELD x_in_base_initializer : 1; --- gcc/cp/semantics.cc.jj 2023-08-28 10:33:11.064187885 +0200 +++ gcc/cp/semantics.cc 2023-08-30 19:28:44.295897427 +0200 @@ -1626,6 +1626,7 @@ begin_function_try_block (tree *compound *compound_stmt = begin_compound_stmt (0); r = begin_try_block (); FN_TRY_BLOCK_P (r) = 1; + cp_function_chain->in_function_try_block = 1; return r; } @@ -1662,6 +1663,7 @@ finish_cleanup (tree cleanup, tree try_b void finish_function_try_block (tree try_block) { + cp_function_chain->in_function_try_block = 0; finish_try_block (try_block); /* FIXME : something queer about CTOR_INITIALIZER somehow following the try block, but moving it inside. */ --- gcc/cp/name-lookup.cc.jj 2023-08-24 15:36:59.272791101 +0200 +++ gcc/cp/name-lookup.cc 2023-08-30 19:40:09.396172794 +0200 @@ -3146,6 +3146,11 @@ check_local_shadow (tree decl) them there. */ cp_binding_level *b = current_binding_level->level_chain; + if (current_binding_level->kind == sk_try + && cp_function_chain->in_function_try_block) + /* Skip the function-try-block level. */ + b = b->level_chain; + if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) /* Skip the ctor/dtor cleanup level. */ b = b->level_chain; --- gcc/testsuite/g++.dg/diagnostic/redeclaration-3.C.jj 2023-08-30 18:51:42.252179367 +0200 +++ gcc/testsuite/g++.dg/diagnostic/redeclaration-3.C 2023-08-30 19:44:06.314933892 +0200 @@ -0,0 +1,204 @@ +// PR c++/52953 +// { dg-do compile } +// { dg-options "-pedantic-errors -Wno-switch-unreachable" } + +void +foo (int x) // { dg-message "'int x' previously declared here" } +{ + int x; // { dg-error "declaration of 'int x' shadows a parameter" } +} + +void +bar (int x) // { dg-message "'int x' previously declared here" } +try +{ + int x; // { dg-error "declaration of 'int x' shadows a parameter" } +} +catch (...) +{ +} + +volatile int v; + +void +baz () +{ +#if __cplusplus >= 201103L + auto f = [] (int x) { int x; }; // { dg-error "declaration of 'int x' shadows a parameter" "" { target c++11 } } + // { dg-message "'int x' previously declared here" "" { target c++11 } .-1 } +#endif + if (int x = 1) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + } + if (int x = 0) // { dg-message "'int x' previously declared here" } + ; + else + { + int x; // { dg-error "redeclaration of 'int x'" } + } + if (int x = 1) // { dg-message "'int x' previously declared here" } + int x; // { dg-error "redeclaration of 'int x'" } + if (int x = 0) // { dg-message "'int x' previously declared here" } + ; + else + int x; // { dg-error "redeclaration of 'int x'" } + switch (int x = 1) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + default:; + } + switch (int x = 1) // { dg-message "'int x' previously declared here" } + int x; // { dg-error "redeclaration of 'int x'" } + while (int x = v) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + } + while (int x = v) // { dg-message "'int x' previously declared here" } + int x; // { dg-error "redeclaration of 'int x'" } + for (int x = v; x; ++x) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + } + for (int x = v; x; ++x) // { dg-message "'int x' previously declared here" } + int x; // { dg-error "redeclaration of 'int x'" } + for (; int x = v; ) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + } + for (; int x = v; ) // { dg-message "'int x' previously declared here" } + int x; // { dg-error "redeclaration of 'int x'" } + try + { + } + catch (int x) // { dg-message "'int x' previously declared here" } + { + int x; // { dg-error "redeclaration of 'int x'" } + } + if (int x = 1) + if (int x = 1) + ; + if (int x = 0) + ; + else + if (int x = 1) + ; + if (int x = 1) + switch (int x = 1) + ; + if (int x = 0) + while (int x = v) + ; + if (int x = 0) + for (int x = v; x; ++x) + ; + switch (int x = 1) + switch (int x = 1) + { + case 1:; + } + while (int x = 0) + if (int x = 1) + ; + for (int x = v; x; ++x) + for (int x = v; x; ++x) + ; +} + +void +qux (int x) // { dg-message "'int x' previously declared here" } +try +{ +} +catch (int x) // { dg-error "redeclaration of 'int x'" } +{ +} + +void +corge (int x) // { dg-message "'int x' previously declared here" } +try +{ +} +catch (...) +{ + int x; // { dg-error "redeclaration of 'int x'" } +} + +void +fred (int x) // { dg-message "'int x' previously declared here" } +try +{ +} +catch (int) +{ +} +catch (long) +{ + int x; // { dg-error "redeclaration of 'int x'" } +} + +void +garply (int x) +{ + try + { + int x; + } + catch (...) + { + int x; + } +} + +struct S +{ + S (int x) // { dg-message "'int x' previously declared here" } + try : s (x) + { + int x; // { dg-error "declaration of 'int x' shadows a parameter" } + } + catch (...) + { + } + int s; +}; + +struct T +{ + T (int x) // { dg-message "'int x' previously declared here" } + try : t (x) + { + } + catch (...) + { + int x; // { dg-error "redeclaration of 'int x'" } + } + int t; +}; + +struct U +{ + U (int x) : u (x) + { + try + { + int x; + } + catch (...) + { + int x; + } + } + int u; +}; + +struct V +{ + V (int x) : v (x) + { + { + int x; + } + } + int v; +};