From patchwork Wed Dec 1 15:16:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 48368 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 30AFE385C412 for ; Wed, 1 Dec 2021 15:26:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 30AFE385C412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638372403; bh=EsujFBZdMSGzw6PEbFnhe9ZeImXDVPvY00QXH+/A4n0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eqFY0T1nfui6mTMiIbbXo8mxaK/GwC3rwhB9BM62Sg54GAigdMpyuFpnwme9al/FE 3Gi8JJvVitAxyvOaVEv0zcvVDAUEyb2/gHewJYAVRDS42jaQjUQ6iL2ZDkO6X/kOr4 gRy5S87jlK3Mw5CQpE2lpiHj5ko36INLUeGpdRng= 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 C2DB2385C409 for ; Wed, 1 Dec 2021 15:16:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2DB2385C409 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-434-Zbt1uOoXP3ezqGKSUR4pBQ-1; Wed, 01 Dec 2021 10:16:37 -0500 X-MC-Unique: Zbt1uOoXP3ezqGKSUR4pBQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A328D81CCB4 for ; Wed, 1 Dec 2021 15:16:36 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55C265D6BA; Wed, 1 Dec 2021 15:16:36 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: ICE with auto(0) in requires-expression [PR103408] Date: Wed, 1 Dec 2021 10:16:29 -0500 Message-Id: <20211201151629.1497940-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.2 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we ICE on int f() requires (auto(0)); in do_auto_deduction when handling the auto: we're in a non-templated requires-expression which are parsed under processing_template_decl == 1 and empty current_template_parms, so 'current_template_args ()' will crash. This code is invalid as per "C++20 CA378: Remove non-templated constrained functions", but of course we shouldn't crash. Since in the scenario above it's expected that current_template_parms is null, I've just added a check, and let grokfndecl issue an error. I guess another approch would be to fake up a template parameter list before calling do_auto_deduction. For good measure, I've added several well-formed cases with auto(x) in a requires-expression. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/103408 gcc/cp/ChangeLog: * pt.c (do_auto_deduction): Check current_template_parms before current_template_args (). gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast9.C: New test. --- gcc/cp/pt.c | 2 +- gcc/testsuite/g++.dg/cpp23/auto-fncast9.C | 27 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast9.C base-commit: 6b8ecbc6d6652d061d7c72c64352d51eca2df6ca diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index f4b9d9673fb..012ca5d06c0 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -30041,7 +30041,7 @@ do_auto_deduction (tree type, tree init, tree auto_node, (but we still may have used them for constraint checking above). */; else if (context == adc_unify) targs = add_to_template_args (outer_targs, targs); - else if (processing_template_decl) + else if (processing_template_decl && current_template_parms) targs = add_to_template_args (current_template_args (), targs); return tsubst (type, targs, complain, NULL_TREE); } diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C new file mode 100644 index 00000000000..45a0ff9b460 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C @@ -0,0 +1,27 @@ +// PR c++/103408 +// { dg-do compile { target c++23 } } + +int bad1() requires (auto(true)); // { dg-error "constraints on a non-templated function" } + +template +int f1() requires (auto(B)); + +template +struct S { T t; constexpr operator bool() { return true; } }; + +int bad2() requires (bool(S{1})); // { dg-error "constraints on a non-templated function" } +int bad3() requires (bool(S(1))); // { dg-error "constraints on a non-templated function" } + +template +int f2() requires (bool(S{N})); + +template +int f3() requires (bool(S(N))); + +void +g () +{ + f1(); + f2<42>(); + f3<42>(); +}