Bug 87519 - -Wsign-conversion -Wconversion explicit cast fails to silence warning
Summary: -Wsign-conversion -Wconversion explicit cast fails to silence warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.3.0
: P3 normal
Target Milestone: 9.3
Assignee: Marek Polacek
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2018-10-04 13:16 UTC by Ivan Fefer
Modified: 2024-04-04 22:52 UTC (History)
3 users (show)

See Also:
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 (116 bytes, text/x-csrc)
2018-10-04 13:16 UTC, Ivan Fefer
Details
Full stderr of compiler (1.30 KB, text/plain)
2018-10-04 13:18 UTC, Ivan Fefer
Details
Code after preprocessing (862 bytes, text/plain)
2018-10-04 13:19 UTC, Ivan Fefer
Details
Temp compiler file (370 bytes, text/plain)
2018-10-04 13:20 UTC, Ivan Fefer
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Fefer 2018-10-04 13:16:46 UTC
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
Comment 1 Ivan Fefer 2018-10-04 13:18:12 UTC
Created attachment 44786 [details]
Full stderr of compiler
Comment 2 Ivan Fefer 2018-10-04 13:19:46 UTC
Created attachment 44787 [details]
Code after preprocessing
Comment 3 Ivan Fefer 2018-10-04 13:20:15 UTC
Created attachment 44788 [details]
Temp compiler file
Comment 4 Ivan Fefer 2018-10-04 13:23:56 UTC
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.
Comment 5 Ivan Fefer 2018-10-04 13:25:31 UTC
Same code at Godbolt.org: https://gcc.godbolt.org/
Comment 6 Ivan Fefer 2018-10-04 13:32:12 UTC
https://gcc.godbolt.org/z/hOpMyc
Comment 7 Manuel López-Ibáñez 2018-10-04 20:31:06 UTC
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);
}
Comment 8 Keith Thompson 2019-08-08 01:37:32 UTC
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
Comment 9 Marek Polacek 2019-08-08 02:22:40 UTC
I think I have a fix.  Will test tomorrow morning.
Comment 10 Marek Polacek 2019-08-08 14:52:47 UTC
Tested fine:
https://gcc.gnu.org/ml/gcc-patches/2019-08/msg00553.html
Comment 11 Marek Polacek 2019-08-08 15:38:18 UTC
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
Comment 12 Marek Polacek 2019-08-08 15:40:45 UTC
Fixed on trunk, will backport to 9.3 later.
Comment 13 Marek Polacek 2019-08-15 18:33:04 UTC
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
Comment 14 Marek Polacek 2019-08-15 18:36:57 UTC
Fixed in 9.3+.