From patchwork Wed Sep 7 17:36:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1675362 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=VH/VoIYR; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MN8bL0JK8z1yhR for ; Thu, 8 Sep 2022 03:37:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2CB14385C32E for ; Wed, 7 Sep 2022 17:37:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2CB14385C32E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662572250; bh=JX9OG0/7y/7e/uvI0gqKBl1hlZE0MpudIyDDuaBNuPo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VH/VoIYRdl/l2JQ5z16YcptHpWJpW/iFBkga2vmLUG+6a2QjNDtW6+tHuBms+dql+ hPk0VxoJWZB5N2UGF4SqRLA2JiioPnt7RXDz6WVNbkychSY376abK/9njNvFLzDEDi BE31EsI9m1rDQA7IBYa3OFYcGliC23CHudi+m+jw= 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 81C4A3858298 for ; Wed, 7 Sep 2022 17:36:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81C4A3858298 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-88-VX-8bLuwNN-BBtgi3c4dBQ-1; Wed, 07 Sep 2022 13:36:49 -0400 X-MC-Unique: VX-8bLuwNN-BBtgi3c4dBQ-1 Received: by mail-qv1-f69.google.com with SMTP id y16-20020a0cec10000000b004a5df9e16c6so6497023qvo.1 for ; Wed, 07 Sep 2022 10:36:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=JX9OG0/7y/7e/uvI0gqKBl1hlZE0MpudIyDDuaBNuPo=; b=hIyG+14g5QWv8JCineu0qhJ1PSFk5eFLRZlLtbTF3yqFMG71g8GoOyA3aPTD342xKD AxYwInBPAJFmQBLTyoWNRg1/S1uNqqxcOVB3gxMc/zTaHTrEdipPaQ+TvWnGfgqZ8XBA 5TYBSun3SDF8ZudtOGmURrwwdmSjGjeTTwBhhCeV+Q/s6P5zeAYMhpomAVjsufGA60PP kfHrtKTtNsV9WhXMCv9wHjpHmXi5v3FyAcfeJirx0m9ux7ooqaZO/AbZsH5EdYWW3lyJ bJ9giBIPILCjPoUsQjcBaq3rxZYV38PthMGKbtWtfFu51RdYmFXl5Yz8ayTqbC/nw19U MOqw== X-Gm-Message-State: ACgBeo3bsFQ4PDCmPHm6GRMT8SrlV+Bzk9krrIlvP0CrKU9hqindsd7J qfcER4UczHitMt1hRopM2yOL4crdKJBOJk/seqpx5KGCpB+2YJJ23Jv0ub5TFuAYLD+Bj1iCq5C D+XSmRQvitZPp+bq151rl9yLdutdVPd2aDkC8LBXTVKpFKDPLQksHY8hTbgDqazD28w== X-Received: by 2002:a05:620a:12fb:b0:6bb:4ba4:aa85 with SMTP id f27-20020a05620a12fb00b006bb4ba4aa85mr3586916qkl.704.1662572208304; Wed, 07 Sep 2022 10:36:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR6zRWozCtAGe7oJ+4Y7oAoJAjGVBmjipLrM7+Dgfs4+Cv+lwJ0dBt7qZDXGvlw8Q8On3fXgrQ== X-Received: by 2002:a05:620a:12fb:b0:6bb:4ba4:aa85 with SMTP id f27-20020a05620a12fb00b006bb4ba4aa85mr3586892qkl.704.1662572207810; Wed, 07 Sep 2022 10:36:47 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s11-20020a05620a29cb00b006bad20a6cfesm15296153qkp.102.2022.09.07.10.36.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 10:36:47 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: diagnostic for template placeholder in parm [PR106793] Date: Wed, 7 Sep 2022 13:36:45 -0400 Message-Id: <20220907173645.1716251-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Talking about the declarator form doesn't help when fixing that would get you a different error about placeholders not being valid in a parameter. This also adds a <> fixit, which isn't enough for most templates, but is a start. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/106793 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Improve placeholder diagnostics. * parser.cc (cp_parser_type_id_1): Add fixit. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-array2.C: Adjust. * g++.dg/cpp1z/class-deduction113.C: New test. --- gcc/cp/decl.cc | 30 ++++++++++++------- gcc/cp/parser.cc | 7 +++-- .../g++.dg/cpp1z/class-deduction113.C | 5 ++++ gcc/testsuite/g++.dg/cpp23/auto-array2.C | 4 +-- 4 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction113.C base-commit: a961ad1b13b9c294d4565344912b8e35ba71b369 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 6d20765f40c..4665a29a24d 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -12407,14 +12407,20 @@ grokdeclarator (const cp_declarator *declarator, if (cxx_dialect >= cxx17 && type && is_auto (type) && innermost_code != cdk_function + /* Placeholder in parm gets a better error below. */ + && !(decl_context == PARM || decl_context == CATCHPARM) && id_declarator && declarator != id_declarator) if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type)) - { - error_at (typespec_loc, "template placeholder type %qT must be followed " - "by a simple declarator-id", type); - inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); - type = error_mark_node; - } + { + auto_diagnostic_group g; + gcc_rich_location richloc (typespec_loc); + richloc.add_fixit_insert_after ("<>"); + error_at (&richloc, "missing template argument list after %qE; " + "for deduction, template placeholder must be followed " + "by a simple declarator-id", tmpl); + inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); + type = error_mark_node; + } staticp = 0; inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline); @@ -12892,6 +12898,7 @@ grokdeclarator (const cp_declarator *declarator, { if (!funcdecl_p || !dguide_name_p (unqualified_id)) { + auto_diagnostic_group g; error_at (typespec_loc, "deduced class " "type %qD in function return type", DECL_NAME (tmpl)); @@ -13837,12 +13844,15 @@ grokdeclarator (const cp_declarator *declarator, else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) { auto_diagnostic_group g; - error_at (typespec_loc, - "class template placeholder %qE not permitted " - "in this context", c); + gcc_rich_location richloc (typespec_loc); + richloc.add_fixit_insert_after ("<>"); + error_at (&richloc, + "missing template argument list after %qE; template " + "placeholder not permitted in parameter", c); if (decl_context == PARM && cxx_dialect >= cxx20) - inform (typespec_loc, "use % for an " + inform (typespec_loc, "or use % for an " "abbreviated function template"); + inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c); } else error_at (typespec_loc, diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 289c2142e45..841ba6ed997 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -24397,8 +24397,11 @@ cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags, location_t loc = type_specifier_seq.locations[ds_type_spec]; if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) { - error_at (loc, "missing template arguments after %qT", - auto_node); + auto_diagnostic_group g; + gcc_rich_location richloc (loc); + richloc.add_fixit_insert_after ("<>"); + error_at (&richloc, "missing template arguments after %qE", + tmpl); inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); } diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction113.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction113.C new file mode 100644 index 00000000000..8f6908e2746 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction113.C @@ -0,0 +1,5 @@ +// PR c++/106793 + +template struct A { A(T); }; +template void f(A *a); // { dg-error "placeholder.*parameter" "" { target c++17 } } +// { dg-error "" "" { target c++14_down } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp23/auto-array2.C b/gcc/testsuite/g++.dg/cpp23/auto-array2.C index 06431685b30..3fc2eae3cea 100644 --- a/gcc/testsuite/g++.dg/cpp23/auto-array2.C +++ b/gcc/testsuite/g++.dg/cpp23/auto-array2.C @@ -5,7 +5,7 @@ template struct A { A(); }; A a[3]; auto (*p)[3] = &a; A (*p2)[3] = &a; -A (*p3)[3] = &a; // { dg-error "template placeholder type" } +A (*p3)[3] = &a; // { dg-error "template placeholder" } auto (&r)[3] = a; A (&r2)[3] = a; -A (&r3)[3] = a; // { dg-error "template placeholder type" } +A (&r3)[3] = a; // { dg-error "template placeholder" }