public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/95765] New: std::vector should be built without warnings with -Wconversion and/or -Wsystem-headers
@ 2020-06-19 12:11 redboltz at gmail dot com
  2020-06-19 19:09 ` [Bug libstdc++/95765] " cvs-commit at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: redboltz at gmail dot com @ 2020-06-19 12:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95765
           Summary: std::vector should be built without warnings with
                    -Wconversion and/or -Wsystem-headers
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redboltz at gmail dot com
  Target Milestone: ---

This is similar issue to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50871 but
different one.

Tested on x86-64 g++ 10.1

The following code converts from std::uint32_t to std::uint16_t. It is
dangerous. Even if compile with -Wconversion option, no warnings are reported
because conversion code is in the standard library.

#include <vector>
#include <cstdint>

struct info {
    explicit info(std::uint16_t v): v { v } {}
    std::uint16_t v;
};

int main() {
    std::uint32_t a = 0x10000;
    std::vector<info> i;

    // convert from std::uint32_t to std::uint16_t internally
    // Warning is reported only if -Wsystem-headers
    i.emplace_back(a); 
}

Compile result: https://godbolt.org/z/_LJPAT


In order to report warning, -Wsystem-headers option is required.

Compile result: https://godbolt.org/z/jmkc5W

/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h:150:4:
warning: conversion from 'unsigned int' to 'uint16_t' {aka 'short unsigned
int'} may change value [-Wconversion]

  150 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

      |    ^

is the expected warning.

However, many other warnings are reported.
It is difficult to find the expected warning.


By the way, user can use the following workaround to report the expected
warning without -Wsystem-headers.

struct info {
    // Possible workaround without -Wconversion
    // Move conversion point to user code
    template <typename T>
    explicit info(T v): v { v } {}
    std::uint16_t v;
};

Compile result: https://godbolt.org/z/f4YKgh

But I think that the std::vector implementation should be compiled without
warnings on -Wconversion and -Wsystem-headers.

I'm not sure the policy which warning checking should be satisfied the standard
library.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-03-29 20:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19 12:11 [Bug libstdc++/95765] New: std::vector should be built without warnings with -Wconversion and/or -Wsystem-headers redboltz at gmail dot com
2020-06-19 19:09 ` [Bug libstdc++/95765] " cvs-commit at gcc dot gnu.org
2020-06-19 19:10 ` redi at gcc dot gnu.org
2020-06-19 19:12 ` redi at gcc dot gnu.org
2020-06-19 23:23 ` redboltz at gmail dot com
2021-03-29 20:05 ` cvs-commit at gcc dot gnu.org

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).