| Summary: | -Wsign-conversion -Wconversion explicit cast fails to silence warning | ||
|---|---|---|---|
| Product: | gcc | Reporter: | Ivan Fefer <fefer.ivan> |
| Component: | c++ | Assignee: | Marek Polacek <mpolacek> |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | Keith.S.Thompson, manu, webrown.cpp |
| Priority: | P3 | Keywords: | diagnostic, patch |
| Version: | 7.3.0 | ||
| Target Milestone: | 9.3 | ||
| Host: | Target: | ||
| Build: | Known to work: | ||
| Known to fail: | Last reconfirmed: | 2018-10-04 00:00:00 | |
| Attachments: |
Minimal source code that triggers the bug
Full stderr of compiler Code after preprocessing Temp compiler file |
||
Created attachment 44786 [details]
Full stderr of compiler
Created attachment 44787 [details]
Code after preprocessing
Created attachment 44788 [details]
Temp compiler file
Sorry, hit the Submit button too early by accident.
Source code and compiler output are attached.
Bug is -Wsign-conversion warning triggerring:
bug.cpp:5:11: error: conversion to ‘long unsigned int’ from ‘int32_t {aka int}’ may change the sign of the result [-Werror=sign-conversion]
r = r + static_cast<uint64_t>(q);
^~~~~~~~~~~~~~~~~~~~~~~~
in function
void f(uint64_t a, int32_t q) {
auto& r = a;
r = r + static_cast<uint64_t>(q);
}
If we replace 'auto& r = a' with 'uint64_t& r = a' warning with disappear.
Same code at Godbolt.org: https://gcc.godbolt.org/ Nothing to do with deduced types:
typedef unsigned long int uint64_t ;
void f(unsigned long int a, int q) {
a = a + static_cast<uint64_t>(q);
}
Another test case:
#include <cstddef>
int main() {
int i = 42;
size_t s0 = sizeof (int) + (size_t)i;
size_t s1 = sizeof (int) + static_cast<size_t>(i);
}
https://stackoverflow.com/q/57403497/827263
https://stackoverflow.com/a/57404123/827263
I think I have a fix. Will test tomorrow morning. Author: mpolacek Date: Thu Aug 8 15:37:46 2019 New Revision: 274211 URL: https://gcc.gnu.org/viewcvs?rev=274211&root=gcc&view=rev Log: PR c++/87519 - bogus warning with -Wsign-conversion. * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing the types directly. * g++.dg/warn/Wsign-conversion-5.C: New test. Added: trunk/gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/typeck.c trunk/gcc/testsuite/ChangeLog Fixed on trunk, will backport to 9.3 later. Author: mpolacek Date: Thu Aug 15 18:32:33 2019 New Revision: 274545 URL: https://gcc.gnu.org/viewcvs?rev=274545&root=gcc&view=rev Log: PR c++/87519 - bogus warning with -Wsign-conversion. * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing the types directly. * g++.dg/warn/Wsign-conversion-5.C: New test. Added: branches/gcc-9-branch/gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C Modified: branches/gcc-9-branch/gcc/cp/ChangeLog branches/gcc-9-branch/gcc/cp/typeck.c Fixed in 9.3+. |
Created attachment 44785 [details] Minimal source code that triggers the bug Version and system: gcc version 7.3.0 (Ubuntu 7.3.0-21ubuntu1~16.04) Command line: gcc -v -save-temps -Werror -Wsign-conversion --std=c++17 bug.cpp Full source code and