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: 1077 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp1250874wrt; Wed, 7 Sep 2022 10:37:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR7L5HSXYpqpDJ4K3HOwfYmqThbqO80af/DXGHx3jxRvDvsK3ESF0d3JgvAzpIO+Pg9hFsZk X-Received: by 2002:a17:907:6d9b:b0:731:1135:dc2d with SMTP id sb27-20020a1709076d9b00b007311135dc2dmr3150820ejc.76.1662572276945; Wed, 07 Sep 2022 10:37:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662572276; cv=none; d=google.com; s=arc-20160816; b=yz4pJ25V6WQaTV/Qh/beFhCJsAhzBCjAln+kVLixMcMsc69Kk3YUM+PPP5i9VDGD1I TaqhgaYzCAYdM97g6t67Kj04bi2+hOT8Kxy7OSPbMnmD6jpoZWsQd9takgf9fzyI21c6 MkjazsGgLul1JgYHRcJ/E6KjLATU2AkLEPEhFZPNo/oTND0aCcYsiE2pcW8Eg7FNKDSf fJJF2ReMGWUWFN3RN3cyBXIfug2APKp9ew1WLPC7Twhg3rVr8EZXyfIZGk1YHqSb23aC 9JkWW50e2NjZ+RTxjkdczt0rGl204Ju1miOPtTS5AE2OoONoaO23JLxsOBlyTRZwpvll xatA== 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-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=JX9OG0/7y/7e/uvI0gqKBl1hlZE0MpudIyDDuaBNuPo=; b=NnjfYNwYBRTAMI+xLQe83NVSvjcCnwC5rK0B6JRJSxBvliB/M+IkAiFM9BF4XrYBXA CkX+SI9NMLW+2AKH+RWuD6YjGJ9R0urJYJqePzfGhGaMSkNHEhntht8tddEa+50nv6ly Pq/+BR1r+GHR+ANnROB7kciD/Tr9rcuzHu7dJCxIfBAIFvmU8Q+BT0mayhTfVPPRM7pY gIONCdj8+QXjJRq06SnEcixeiI2zpzKR+Kmu/AoB5Tlko58xAQ/mK9Rt2ZCpOErCBeTl yf0xxZwjTGSKaA8glyrOn3LghgNMa9Vq7BodJZFhi/aCXhPCyWe3ptCf24Jf+uZADnRu vi7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=e3dTMCeK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id kj6-20020a170907764600b0073d8650d612si19087ejc.374.2022.09.07.10.37.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 10:37:56 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=e3dTMCeK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 D0030385AC1C for ; Wed, 7 Sep 2022 17:37:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0030385AC1C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662572275; 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=e3dTMCeKSGTIbn8+V5mcZPS2EhkMOh9P3iG5Nd7BwGpqgdHz7Q56+i10Fczul0VNE iqPaxcrJ2ubHtG7/q3uVf7KBa0kHLldaN6jSvsuZQh52Pbc0B9MyiBvfwXw6ZRfAk2 RKQlKqBABaxpbs5q7lVvOkeEJX97/xhfoS3lV8PU= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743333387686532212?= X-GMAIL-MSGID: =?utf-8?q?1743333387686532212?= 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" }