public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "arthur.j.odwyer at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104792] New: [g++ and/or libstdc++] Wunused-local-typedefs + C++20 concepts = annoying
Date: Fri, 04 Mar 2022 19:07:53 +0000	[thread overview]
Message-ID: <bug-104792-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104792

            Bug ID: 104792
           Summary: [g++ and/or libstdc++] Wunused-local-typedefs + C++20
                    concepts = annoying
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

This might be considered "not a bug", or "duplicate of #61596", or "bug but in
a different way from what Arthur suggests," so I'm going to give the
non-reduced test case and let one of you (@jwakely perhaps) worry about how to
reduce it.

// https://godbolt.org/z/v9Wv8vY3G
#include <iterator>
void test() {
    struct It {
        using value_type = int;
        using difference_type = int;
        int& operator*() const;
        It& operator++();       // X
        It operator++(int);     // X
    };
    static_assert(std::is_same_v<std::iter_value_t<It>, int>);
    static_assert(std::is_same_v<std::iter_rvalue_reference_t<It>, int&&>);
}

Compile with "g++ -std=c++20 -W -Wall" and libstdc++.
GCC will give a warning:

    warning: typedef 'using difference_type = int' locally defined but not used
[-Wunused-local-typedefs]
        6 |         using difference_type = int;
          |               ^~~~~~~~~~~~~~~

However, if you then delete any of the three lines marked "X", the warning will
go away again.
I believe this is because `iter_value_t<It>` relies on a C++20 Concepts
constraint where `difference_type` *is* checked when `It` is an
`input_iterator`, but is *not* checked when `It` is not an iterator. So, when
these precise three operators exist, the typedef isn't unused, but when any one
of them doesn't exist, GCC gives the unused-typedef warning.
I claim that the user-programmer shouldn't be responsible for tracking the
internal implementation details of the constraints of `std::iter_value_t`. I
just want to make a local type that has all the pieces of an iterator, without
GCC helpfully getting in the way and warning me that *right now* (according to
the internal implementation details of libstdc++) some of those pieces aren't
being used.

I admit that in this case the warning is surfaced only when I *fail* to
implement one of those three member functions, so I actually have *not*
implemented "all the pieces of an iterator" in this test case. I'm betting that
it's possible to reproduce this annoying issue, somewhere in libstdc++, even
without that caveat, though.

For now, I'll silence the warning by removing my unused typedef
`difference_type`, i.e.

#include <iterator>
void test() {
    struct It {
        using value_type = int;
        int& operator*() const;
    };
    static_assert(std::is_same_v<std::iter_value_t<It>, int>);
    static_assert(std::is_same_v<std::iter_rvalue_reference_t<It>, int&&>);
}

But I'm uncomfortable with that, because I don't know if later
revisions/bugfixes to the library will require me to re-add those pieces I've
removed.

             reply	other threads:[~2022-03-04 19:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 19:07 arthur.j.odwyer at gmail dot com [this message]
2022-03-05  0:32 ` [Bug c++/104792] " pinskia at gcc dot gnu.org
2022-03-06  1:07 ` arthur.j.odwyer at gmail dot com
2022-03-06  1:29 ` pinskia at gcc dot gnu.org
2022-03-06  1:39 ` [Bug c++/104792] Wunused-local-typedefs with typedef/type alias defined in struct that is defined in a function scope pinskia at gcc dot gnu.org
2022-03-07 10:46 ` redi at gcc dot gnu.org

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=bug-104792-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).