From: Jason Merrill <jason@redhat.com>
To: Martin Sebor <msebor@gmail.com>
Cc: Gcc Patch List <gcc-patches@gcc.gnu.org>
Subject: Re: [RFC PATCH] avoid applying attributes to explicit specializations (PR 83871)
Date: Mon, 12 Feb 2018 17:11:00 -0000 [thread overview]
Message-ID: <CADzB+2=rOxJao8AxwnExb8ziO+JedeJptcude4J9n-VmYMa6sQ@mail.gmail.com> (raw)
In-Reply-To: <1bb37a5d-c85a-1b6a-a7d6-d57141d68855@gmail.com>
On Mon, Feb 12, 2018 at 11:59 AM, Martin Sebor <msebor@gmail.com> wrote:
> On 02/12/2018 09:30 AM, Jason Merrill wrote:
>>
>> On Fri, Feb 9, 2018 at 6:57 PM, Martin Sebor <msebor@gmail.com> wrote:
>>>
>>> On 02/09/2018 12:52 PM, Jason Merrill wrote:
>>>>
>>>> On 02/08/2018 04:52 PM, Martin Sebor wrote:
>>>>>
>>>>>
>>>>> I took me a while to find DECL_TEMPLATE_RESULT. Hopefully
>>>>> that's the right way to get the primary from a TEMPLATE_DECL.
>>>>
>>>>
>>>> Yes.
>>>>
>>>>>>> Attached is an updated patch. It hasn't gone through full
>>>>>>> testing yet but please let me know if you'd like me to make
>>>>>>> some changes.
>>>>>>
>>>>>>
>>>>>>
>>>>>>> + const char* const whitelist[] = {
>>>>>>> + "error", "noreturn", "warning"
>>>>>>> + };
>>>>>>
>>>>>>
>>>>>>
>>>>>> Why whitelist noreturn? I would expect to want that to be consistent.
>>>>>
>>>>>
>>>>> I expect noreturn to be used on a primary whose definition
>>>>> is provided but that's not meant to be used the way the API
>>>>> is otherwise expected to be. As in:
>>>>>
>>>>> template <class T>
>>>>> T [[noreturn]] foo () { throw "not implemented"; }
>>>>>
>>>>> template <> int foo<int>(); // implemented elsewhere
>>>>
>>>>
>>>> Marking that template as noreturn seems pointless, and possibly harmful;
>>>> the deprecated, warning, or error attributes would be better for this
>>>> situation.
>>>
>>>
>>> I meant either:
>>>
>>> template <class T>
>>> T __attribute__ ((noreturn)) foo () { throw "not implemented"; }
>>>
>>> template <> int foo<int>(); // implemented elsewhere
>>>
>>> or (sigh)
>>>
>>> template <class T>
>>> [[noreturn]] T foo () { throw "not implemented"; }
>>>
>>> template <> int foo<int>(); // implemented elsewhere
>>>
>>> It lets code like this
>>>
>>> int bar ()
>>> {
>>> return foo<char>();
>>> }
>>>
>>> be diagnosed because it's likely a bug (as Clang does with
>>> -Wunreachable-code). It doesn't stop code like the following
>>> from compiling (which is good) but it instead lets them throw
>>> at runtime which is what foo's author wants.
>>>
>>> void bar ()
>>> {
>>> foo<char>();
>>> }
>>>
>>> It's the same as having an "unimplemented" base virtual function
>>> throw an exception when it's called rather than making it pure
>>> and having calls to it abort. Declaring the base virtual function
>>> noreturn is useful for the same reason (and also diagnosed by
>>> Clang). I should remember to add the same warning in GCC 9.
>>
>>
>> Yes, I understood the patterns you had in mind, but I disagree with
>> them. My point about harmful is that declaring a function noreturn
>> because it's unimplemented could be a problem for when the function is
>> later implemented, and callers were optimized inappropriately. This
>> seems like a rather roundabout way to get a warning about calling an
>> unimplemented function, and not worth overriding the normal behavior.
>
>
> Removing noreturn from the whitelist means having to prevent
> the attribute from causing conflicts with the attributes on
> the blacklist. E.g., in this:
>
> template <class T> [[malloc]] void* allocate (int);
>
> template <> [[noreturn]] void* allocate<void> (int);
>
> -Wmissing-attributes would warn for the missing malloc but
> -Wattributes will warn once malloc is added. Ditto for all
> other attributes noreturn is considered to conflict with such
> as alloc_size and warn_unused_result.
This example seems rather unlikely, and the solution is to remove
[[noreturn]]. I don't think this is worth worrying about for GCC 8.
> I anticipate the warning code to ultimately end up in
> the middle-end so it can handle Joseph's case as well, and
> so it can also be integrated with the attribute conflict
> machinery. It also needs to be in the middle-end to become
> usable by -Wsuggest-attribute. But I wasn't thinking of
> making any of these bigger changes until GCC 9.
> Do you want me to integrate it with the conflict stuff now?
No, leaving it for GCC 9 makes sense to me.
Jason
>>> I actually had some misgivings about both warning and deprecated
>>> for the white-listing, but not for noreturn. My (only mild)
>>> concern is that both warning and deprecated functions can and
>>> likely will in some cases still be called, and so using them to
>>> suppress the warning runs the risk that their calls might be
>>> wrong and no one will notice. Warning cannot be suppressed
>>> so it seems unlikely to be ignored, but deprecated can be.
>>> So I wonder if the white-listing for deprecated should be
>>> conditional on -Wdeprecated being enabled.
>>>
>>>>> - /* Merge the type qualifiers. */
>>>>> - if (TREE_READONLY (newdecl))
>>>>> - TREE_READONLY (olddecl) = 1;
>>>>> if (TREE_THIS_VOLATILE (newdecl))
>>>>> TREE_THIS_VOLATILE (olddecl) = 1;
>>>>> - if (TREE_NOTHROW (newdecl))
>>>>> - TREE_NOTHROW (olddecl) = 1;
>>>>> +
>>>>> + if (merge_attr)
>>>>> + {
>>>>> + /* Merge the type qualifiers. */
>>>>> + if (TREE_READONLY (newdecl))
>>>>> + TREE_READONLY (olddecl) = 1;
>>>>> + }
>>>>> + else
>>>>> + {
>>>>> + /* Set the bits that correspond to the const function
>>>>> attributes. */
>>>>> + TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
>>>>> + }
>>>>
>>>>
>>>>
>>>> Let's limit the const/volatile handling here to non-functions, and
>>>> handle the const/noreturn attributes for functions in the later hunk
>>>> along with nothrow/malloc/pure.
>>>
>>>
>>>
>>> I had to keep the TREE_HAS_VOLATILE handling as is since it
>>> applies to functions too (has side-effects). Otherwise the
>>> attr-nothrow-2.C test fails.
>>
>>
>> When I mentioned "noreturn" above I was referring to
>> TREE_HAS_VOLATILE; sorry I wasn't clear. For functions it should be
>> handled along with nothrow/readonly/malloc/pure.
>>
>> Jason
>>
>
next prev parent reply other threads:[~2018-02-12 17:11 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-05 0:07 Martin Sebor
2018-02-05 21:53 ` Jason Merrill
2018-02-07 4:01 ` Martin Sebor
2018-02-07 19:42 ` Jason Merrill
2018-02-08 21:52 ` Martin Sebor
2018-02-09 19:52 ` Jason Merrill
2018-02-09 23:58 ` Martin Sebor
2018-02-12 16:30 ` Jason Merrill
2018-02-12 16:59 ` Martin Sebor
2018-02-12 17:11 ` Jason Merrill [this message]
2018-02-12 23:40 ` Martin Sebor
2018-02-13 14:47 ` Jason Merrill
2018-02-13 18:00 ` Martin Sebor
2018-02-13 18:34 ` Jason Merrill
2018-02-21 23:04 ` Martin Sebor
2018-02-22 22:12 ` Jason Merrill
2018-02-27 4:20 ` Martin Sebor
2018-02-27 14:49 ` Jason Merrill
2018-02-27 23:49 ` Jakub Jelinek
2018-02-28 0:52 ` Martin Sebor
2018-02-28 14:49 ` [PATCH] Fix i18n in warn_spec_missing_attributes " Jakub Jelinek
2018-02-28 16:38 ` Jason Merrill
2018-02-28 16:10 ` [RFC PATCH] avoid applying attributes to explicit specializations " Jason Merrill
2018-02-28 9:49 ` Jakub Jelinek
2018-02-28 9:51 ` Jakub Jelinek
2018-02-28 16:05 ` Martin Sebor
2018-02-13 15:59 ` Michael Matz
2018-02-13 17:34 ` Martin Sebor
2018-02-05 23:44 ` Joseph Myers
2018-02-06 0:44 ` Martin Sebor
2018-02-06 0:55 ` Joseph Myers
2018-02-06 2:48 ` Martin Sebor
2018-02-27 23:21 David Edelsohn
2018-02-27 23:30 ` Martin Sebor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CADzB+2=rOxJao8AxwnExb8ziO+JedeJptcude4J9n-VmYMa6sQ@mail.gmail.com' \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=msebor@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).