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.

Reply via email to