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 CCDF2385783B for ; Fri, 19 Nov 2021 22:27:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CCDF2385783B Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-13-rqUKkFP9MnmhakkAok96bQ-1; Fri, 19 Nov 2021 17:27:01 -0500 X-MC-Unique: rqUKkFP9MnmhakkAok96bQ-1 Received: by mail-qk1-f197.google.com with SMTP id u8-20020a05620a454800b00468482aac5dso9012535qkp.18 for ; Fri, 19 Nov 2021 14:27:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=PMRwEmyJ5bZDi+kn/ymwsQ0v7RUGe5cXj9segO3esVo=; b=3V2Btk8PnlOaVZmrXF/OFtg6fdA9OWPNbqh2Z29DjmSZ9h3jDBRqtB5CjYvNIzp+B9 JV7ssZYzDh0ShbVrrJo5ymuU1kelMpSE+1B47d0ayoO8+/N0CRoAytklhXe9XN4KPI8D hMx5TpOKZG5qiuGxheCWBFG5aBzqi5OQzv2kYyl6Z2wwhIQRZE3XXxQmE4xIt3eoCrSx tkdwOIFS144c130n/Fhbe3II1CDmjhIIE78Cg1E+KgV02Wbn++fr9u3WSizv9PkjGPQ1 ZuPqcQUhtpDusACfQn8giXPxV15q75DRPowQpFawYEYyEemPCIawqOFrb0G0qYxYpp56 hoaQ== X-Gm-Message-State: AOAM530/YFvXpdWEhh/J2TjgrtfG6NdihTfevnuODsJ0Ef0lW4Cv0YvY ++hajQ1jFBOrEWT2+opfKEobKZK1sQtaagx8WW7O9ScULlCKEdk/zFC3+rROybGqAZRWwR1WZOd lMWBD6dqOdagBdxqqtg== X-Received: by 2002:a05:620a:25c8:: with SMTP id y8mr30753954qko.42.1637360820362; Fri, 19 Nov 2021 14:27:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJx4R6T/wDHATOMbCNDZdE8qRE+2jxx4X1P+0ZEIdqkB58HADrH4frKX622GPjyKduBGIj9ywg== X-Received: by 2002:a05:620a:25c8:: with SMTP id y8mr30753924qko.42.1637360819922; Fri, 19 Nov 2021 14:26:59 -0800 (PST) Received: from [192.168.1.149] (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 x7sm572551qko.109.2021.11.19.14.26.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 Nov 2021 14:26:59 -0800 (PST) Message-ID: <47662158-37ba-457a-ba02-fd4c9aaaae31@redhat.com> Date: Fri, 19 Nov 2021 17:26:57 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.3.1 Subject: Re: [RFC] c++: Print function template parms when relevant (was: [PATCH v4] c++: Add gnu::diagnose_as attribute) To: Matthias Kretz , gcc-patches@gcc.gnu.org References: <4361366.VLH7GnMWUR@minbar> <1845985.Z2AExFMA8N@excalibur> <77278857-45f0-f76e-bbab-52598052b9eb@redhat.com> <2941803.SbB2uZ23bb@excalibur> From: Jason Merrill In-Reply-To: <2941803.SbB2uZ23bb@excalibur> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, WEIRD_PORT 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-List-Received-Date: Fri, 19 Nov 2021 22:27:04 -0000 On 11/19/21 04:53, Matthias Kretz wrote: > On Thursday, 18 November 2021 20:24:36 CET Jason Merrill wrote: >> On 11/17/21 17:51, Matthias Kretz wrote: >>> Right, I had already added a `gcc_assert (!TMPL_ARGS_HAVE_MULTIPLE_LEVELS >>> (args))` to my new set_non_default_template_args_count function and found >>> cp/ constraint.cc:2896 (get_mapped_args), which calls >>> SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT on the outer TREE_VEC. Was this >>> supposed to apply to all inner TREE_VECs? Or is deleting the line the >>> correct fix? >> >> That should apply to the inner TREE_VECs (and probably use list.length) > > Like this? Yes. > @@ -2890,10 +2890,11 @@ get_mapped_args (tree map) > tree level = make_tree_vec (list.length ()); > for (unsigned j = 0; j < list.length(); ++j) > TREE_VEC_ELT (level, j) = list[j]; > + /* None of the args at any level are defaulted. */ > + SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (level, list.length()); > SET_TMPL_ARGS_LEVEL (args, i + 1, level); > list.release (); > } > - SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0); > > return args; > } > >>>>> __FUNCTION__ was 'fun' all the time, but __PRETTY_FUNCTION__ was >>>>> 'void fun(T) [with T = int]'. >>>> >>>> Isn't that true for instantiations, as well? >>> >>> No, instantiations don't have template args/parms in __FUNCTION__. >> >> Hmm, that inconsistency seems like a bug, though I'm not sure whether it >> should have the template args or not; I lean toward not. The standard >> says that the value of __func__ is implementation-defined, the GCC >> manual says it's "the unadorned name of the function". > > So you think f1 in testsuite/g++.old-deja/g++.ext/pretty3.C needs to test > for > > if (strcmp (function, "f1")) > bad = true; > if (strcmp (pretty, "void f1(T) [with T = int]")) > bad = true; I think so. >>>>> It's more consistent that __PRETTY_FUNCTION__ contains __FUNCTION__, >>>>> IMHO >>>> >>>> I suppose, but I don't see that as a strong enough motivation to mix >>>> this up. >>> >>> What about >>> >>> template void f(); >>> template <> void f(); >>> >>> With -fpretty-templates shouldn't it print as 'void f() [with T = >>> float]' and 'void f()'? Yes, it's probably too subtle for most users >>> to notice the difference. But I find it's more consistent this way. >> >> I disagree; the function signature is the same whether a particular >> function is an explicit specialization or an instantiation. > > Yes, the call signature is the same. But what it calls is different. There's > no obvious answer for my stated goal "print template parms wherever they > would appear in the source code as well", since it depends on whether the user > is interested in recognizing the exact function body that was called. > > My motivation for printing a function template specialization differently is: > > 1. It's a different function definition that's being called. The user (caller) > might be surprised to realize this is the case as he forgot about the > specialization and was expecting his change to the general template to make a > difference. > > 2. There's no T in > > template <> void f() { > // no T here, but of course I can define one: > using T = int; > } > > so presenting the function that was called as 'void f() [with T = int]' is > not exactly correct. In this case it wasn't even the case that T was deduced > to be 'int', which we could argue to be useful information that might get > lost. On the other hand, this tells us what template this specialization is specializing, which could be unclear if there are multiple overloaded function templates. There's always -fno-pretty-templates if you want the form without template args. Incidentally, the contents of __PRETTY_FUNCTION__ probably shouldn't vary with that flag... > For > > template void f(T); > template <> void f(int); > > the whole story is "'void f(int)' was called for 'template void f(T) > [with T = int]'". What the user wants to see depends on what is more important > to fix the bug: that T was deduced to be int, or that the specialization of f > was called instead of the general template. I'd still go with 'void f(int)', > though I'd be happier if I had some indication that a template was involved. The current form tells you about the template, and the line number points you at the declaration. >>>> Ah, you're trying to omit defaulted parms from the ? I'm not sure >>>> that's necessary, leaving them out of the [with ...] list should be >>>> sufficient. >>> >>> I was thinking about all the std::allocator defaults in the standard >>> library. I don't want to see them. E.g. vector::clear() on const >>> object: >>> >>> error: passing 'const std::vector' as 'this' argument discards >>> qualifiers [...]/stl_vector.h:1498:7: note: in call to 'void >>> std::vector<_Tp, _Alloc>::clear() [with _Tp = int; _Alloc = >>> std::allocator]' >>> >>> With my patch the last line becomes >>> [...]/stl_vector.h:1498:7: note: in call to 'void >>> std::vector<_Tp>::clear() [with _Tp = int]' >>> >>> >>> Another case I didn't consider before: >>> >>> template struct A { >>> >>> [[deprecated]] void f(U); >>> >>> }; >>> >>> A a; a.f(1); >>> >>> With my patch it prints 'void A::f(U) [with T = float]', with your >>> suggestion 'void A::f(U) [with T = float]'. Both are missing >>> important information in the substitution list, IMHO. Would 'void A>> = int>::f(U) [with T = float]' be an improvement? Or should >>> find_typenames (in cp/error.c) find defaulted template parms and add them >>> to its list? IIUC find_typenames would find all template parms and >>> couldn't know whether they're defaulted. >> >> That sounds good: omit defaulted parms only if they don't appear in the >> signature (other than as another default template argument). > > Let me check whether I have the right idea: > > I could extend find_typenames (which walks the complete) tree to look for > TEMPLATE_TYPE_PARM (and the 3 others I don't recall right now). But since that > walks the *complete* tree, it'll simply find all parms with no indication > whether they appear in the signature. Ideas: Hmm, since it walks DECL_TEMPLATE_RESULT, I wouldn't expect it to find template parms that aren't in the function signature. > 1. Count occurrences: with 2 occurrences, one of them must be a use in the > signature. > > 2. Walk only over TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (fn))) to > collect TEMPLATE_TYPE_PARMs. Jason