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 317B63858421 for ; Mon, 24 Oct 2022 17:13:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 317B63858421 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=1666631610; 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: in-reply-to:in-reply-to:references:references; bh=+6izJBZmmyYO1tvBWynpWJKE9He3MfgJK7HVNxA1oGw=; b=JqQaQcpKf5+100LbCW9BZ4N3Y7xnqCuhuYFlxnis0r+PhNbFtcYzXsbAeOlA/jK5+Svmh5 jajzfcui0LarBlQSRyFHeLvyrgzcdXW7z99SAD4Uc7Jb321pghSA99wFczuoFO93T0pUtv 8ZpoAcoc+IqC1v/nYDon/EuxsrU+WWM= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-211-jDT9s6yhOT6ixiCXZ86dLQ-1; Mon, 24 Oct 2022 13:13:28 -0400 X-MC-Unique: jDT9s6yhOT6ixiCXZ86dLQ-1 Received: by mail-qt1-f197.google.com with SMTP id d25-20020ac847d9000000b0039d0dd90182so7429714qtr.3 for ; Mon, 24 Oct 2022 10:13:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+6izJBZmmyYO1tvBWynpWJKE9He3MfgJK7HVNxA1oGw=; b=CVB38MwVHnsz2rBKyP1L2Q/IIqZ4TW82BrjPrIU3bWMpoXrHB3/2qs5VKsu6Q3NYg3 00FR8L2iaHh4/SzxfqJI7+TQOnaAlsBbEiHaSdewFOMN0Pw5Vqjwd3DZ+uQ4kwWdDbOe QqSIBptfaKLVX4DeeZU/3XpIlbQb3nn+WYlG1q57UPepcbzqW2lJ7UDRRpHZIORT2wJT 0kRRZWHlJJG90YYG0RTaOsspDLy0lWD5IUGSM6uB5lH07aqlsY+A70vQ+6QyE7aWHtKa SvMCLPaDDN1rEVQWsbdoQ5bVFoZxYopYLtXAEgn7qlfajFHWXS4XXBikHFQB8/OMOW6d IjRA== X-Gm-Message-State: ACrzQf3EDDvVGD2Rt4UBT+Wpl0VyjL40S7+B9CY7eqdmKN6BB4d+sw99 HCcE+kDDXNJG8DWY93JMPCkmGbYQoMFJVcTIlaW6Qui9J29pOgHBcZkm0XpIa09nWpjKdeLEbBH lT6PnjuFu9DjB75CPiA== X-Received: by 2002:ac8:7d01:0:b0:39c:ebdf:490c with SMTP id g1-20020ac87d01000000b0039cebdf490cmr28879606qtb.179.1666631607955; Mon, 24 Oct 2022 10:13:27 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6p58vD4zdoeqKZAqNw/xs+OBGBzONSsTQzfXR4oJUOSc8vltKPIIX8SpQm2u30k8gEqP0hug== X-Received: by 2002:ac8:7d01:0:b0:39c:ebdf:490c with SMTP id g1-20020ac87d01000000b0039cebdf490cmr28879585qtb.179.1666631607630; Mon, 24 Oct 2022 10:13:27 -0700 (PDT) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id l16-20020a05622a051000b0039ee562799csm236690qtx.59.2022.10.24.10.13.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 10:13:27 -0700 (PDT) Date: Mon, 24 Oct 2022 13:13:25 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: ICE with invalid structured bindings [PR107276] Message-ID: References: <20221021232922.1093229-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.5 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 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 Mon, Oct 24, 2022 at 10:31:50AM -0400, Jason Merrill wrote: > On 10/21/22 19:29, Marek Polacek wrote: > > This test ICEs in C++23 because we reach the new code in do_auto_deduction: > > > > 30468 if (cxx_dialect >= cxx23 > > 30469 && context == adc_return_type > > 30470 && (!AUTO_IS_DECLTYPE (auto_node) > > 30471 || !unparenthesized_id_or_class_member_access_p (init)) > > 30472 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init), > > 30473 /*return*/true))) > > > > where 'init' is "VIEW_CONVERT_EXPR<<<< error >>>>(y)", and then the move > > in treat_lvalue_as_rvalue_p returns error_mark_node whereupon > > set_implicit_rvalue_p crashes. > > > > I don't think such V_C_Es are useful so let's not create them. But that > > won't fix the ICE so I'm checking the return value of move. A structured > > bindings decl can have an error type, that is set in cp_finish_decomp: > > > > 8908 TREE_TYPE (first) = error_mark_node; > > > > therefore I think treat_lvalue_as_rvalue_p just needs to cope. > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/107276 > > > > gcc/cp/ChangeLog: > > > > * typeck.cc (treat_lvalue_as_rvalue_p): Check the return value of move. > > > > gcc/ChangeLog: > > > > * tree.cc (maybe_wrap_with_location): Don't create a location wrapper > > when the type is erroneous. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp2a/decomp4.C: New test. > > --- > > gcc/cp/typeck.cc | 7 ++++++- > > gcc/testsuite/g++.dg/cpp2a/decomp4.C | 8 ++++++++ > > gcc/tree.cc | 3 ++- > > 3 files changed, 16 insertions(+), 2 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/decomp4.C > > > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > > index 16e7d85793d..5ca191759f6 100644 > > --- a/gcc/cp/typeck.cc > > +++ b/gcc/cp/typeck.cc > > @@ -10726,7 +10726,12 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p) > > if (DECL_CONTEXT (retval) != current_function_decl) > > return NULL_TREE; > > if (return_p) > > - return set_implicit_rvalue_p (move (expr)); > > + { > > + expr = move (expr); > > + if (expr == error_mark_node) > > + return NULL_TREE; > > + return set_implicit_rvalue_p (expr); > > + } > > /* if the operand of a throw-expression is a (possibly parenthesized) > > id-expression that names an implicitly movable entity whose scope does not > > diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp4.C b/gcc/testsuite/g++.dg/cpp2a/decomp4.C > > new file mode 100644 > > index 00000000000..28b3f172b53 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/decomp4.C > > @@ -0,0 +1,8 @@ > > +// PR c++/107276 > > +// { dg-do run { target c++20 } } > > + > > +auto f(auto x) { > > + auto [y] = x; // { dg-error "cannot decompose" } > > + return y; > > +} > > +int i = f(0); > > diff --git a/gcc/tree.cc b/gcc/tree.cc > > index 81a6ceaf181..4e5b1df4d85 100644 > > --- a/gcc/tree.cc > > +++ b/gcc/tree.cc > > @@ -14352,7 +14352,8 @@ maybe_wrap_with_location (tree expr, location_t loc) > > /* For now, don't add wrappers to exceptional tree nodes, to minimize > > any impact of the wrapper nodes. */ > > - if (EXCEPTIONAL_CLASS_P (expr)) > > + if (EXCEPTIONAL_CLASS_P (expr) > > + || (TREE_TYPE (expr) && EXCEPTIONAL_CLASS_P (TREE_TYPE (expr)))) > > I think check error_operand_p instead; I don't think it makes sense to look > for other exceptional nodes in TREE_TYPE. Makes sense. I don't suppose you want to replace the whole condition with error_operand_p, only what comes after ||. Updated patch below, thanks. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This test ICEs in C++23 because we reach the new code in do_auto_deduction: 30468 if (cxx_dialect >= cxx23 30469 && context == adc_return_type 30470 && (!AUTO_IS_DECLTYPE (auto_node) 30471 || !unparenthesized_id_or_class_member_access_p (init)) 30472 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init), 30473 /*return*/true))) where 'init' is "VIEW_CONVERT_EXPR<<<< error >>>>(y)", and then the move in treat_lvalue_as_rvalue_p returns error_mark_node whereupon set_implicit_rvalue_p crashes. I don't think such V_C_Es are useful so let's not create them. But that won't fix the ICE so I'm checking the return value of move. A structured bindings decl can have an error type, that is set in cp_finish_decomp: 8908 TREE_TYPE (first) = error_mark_node; therefore I think treat_lvalue_as_rvalue_p just needs to cope. PR c++/107276 gcc/cp/ChangeLog: * typeck.cc (treat_lvalue_as_rvalue_p): Check the return value of move. gcc/ChangeLog: * tree.cc (maybe_wrap_with_location): Don't create a location wrapper when the type is erroneous. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/decomp4.C: New test. --- gcc/cp/typeck.cc | 7 ++++++- gcc/testsuite/g++.dg/cpp2a/decomp4.C | 8 ++++++++ gcc/tree.cc | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/decomp4.C diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 16e7d85793d..5ca191759f6 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10726,7 +10726,12 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p) if (DECL_CONTEXT (retval) != current_function_decl) return NULL_TREE; if (return_p) - return set_implicit_rvalue_p (move (expr)); + { + expr = move (expr); + if (expr == error_mark_node) + return NULL_TREE; + return set_implicit_rvalue_p (expr); + } /* if the operand of a throw-expression is a (possibly parenthesized) id-expression that names an implicitly movable entity whose scope does not diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp4.C b/gcc/testsuite/g++.dg/cpp2a/decomp4.C new file mode 100644 index 00000000000..d1b0c90ee26 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/decomp4.C @@ -0,0 +1,8 @@ +// PR c++/107276 +// { dg-do compile { target c++20 } } + +auto f(auto x) { + auto [y] = x; // { dg-error "cannot decompose" } + return y; +} +int i = f(0); diff --git a/gcc/tree.cc b/gcc/tree.cc index 81a6ceaf181..04603c8c902 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -14352,7 +14352,7 @@ maybe_wrap_with_location (tree expr, location_t loc) /* For now, don't add wrappers to exceptional tree nodes, to minimize any impact of the wrapper nodes. */ - if (EXCEPTIONAL_CLASS_P (expr)) + if (EXCEPTIONAL_CLASS_P (expr) || error_operand_p (expr)) return expr; /* Compiler-generated temporary variables don't need a wrapper. */ base-commit: 131d18e928a3ea1ab2d3bf61aa92d68a8a254609 -- 2.37.3