<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Inconsistent narrowing errors"
   href="https://bugs.llvm.org/show_bug.cgi?id=35886">35886</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Inconsistent narrowing errors
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>FreeBSD@Shaneware.biz
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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 -
<a href="https://github.com/godotengine/godot/tree/2.1/servers/spatial_sound/spatial_sound_server_sw.cpp">https://github.com/godotengine/godot/tree/2.1/servers/spatial_sound/spatial_sound_server_sw.cpp</a>

//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;
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>