[llvm-bugs] [Bug 35886] New: Inconsistent narrowing errors
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Jan 10 04:48:35 PST 2018
https://bugs.llvm.org/show_bug.cgi?id=35886
Bug ID: 35886
Summary: Inconsistent narrowing errors
Product: clang
Version: unspecified
Hardware: PC
OS: All
Status: NEW
Severity: enhancement
Priority: P
Component: C++11
Assignee: unassignedclangbugs at nondot.org
Reporter: FreeBSD at Shaneware.biz
CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
I'm using freebsd - testing projects/clang600-import for future 12-release but
I can get the same with earlier clang versions by setting
-Werror=c++11-narrowing
It appears that clang 6.0 imported into freebsd base is forcing c++11-narrowing
warnings to be treated as an error were earlier releases don't. I'm ok with
that but find the reports inconsistent.
--
There is an inconsistency were assigning a long to an int gives an error, but
passing a long to a function that takes an int does not. Also returning a long
from an int func() does not give an error.
So assigning to a smaller size triggers a narrowing error, while passing a
larger variable to/from a function does not.
--
This code appears to be treating a literal 1.0 as a double, so it promotes the
float to a double and then errors when the double result is being shortened to
be stored in a float. Yet the line before treats the 0.5 literal as a float and
has no error. The difference would be that the error comes from an array
initialisation. Changing 1.0 to 1.0f can remove this error.
The following code snippet is taken from the 2.1 branch of the godot project -
https://github.com/godotengine/godot/tree/2.1/servers/spatial_sound/spatial_sound_server_sw.cpp
//lines 691-692
float p = sd.panning.x * 0.5 + 0.5; // OK
float panf[2] = { (1.0 - p), p }; // narrowing error
The same error shows in 7 other similar lines of float array initialisations.
--
example code demonstrating report --
clang++ -std=c++11 -Werror=c++11-narrowing test.cpp
int testcall(int idx)
{
long x[] = {1,2,3,4,5,6};
// returning a long as an int doesn't give an error
return x[idx];
}
struct Property {
int format, nitems;
};
int main(int argc, char ** argv)
{
int actual_format = 5;
long nitems = 25;
// this gives a c++11-narowing error for long to int
Property p = { actual_format, nitems };
// passing a long to an int parameter doesn't give an error
testcall(nitems);
float a = 1.8;
float b = 1.0 + a * 0.5; // OK
float c[2] = { 1.0 - b, b }; // error
return 0;
}
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180110/c72de61e/attachment-0001.html>
More information about the llvm-bugs
mailing list