<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 - Invalid code generation and ICE when template references local static constexpr variable by pointer"
   href="https://bugs.llvm.org/show_bug.cgi?id=39916">39916</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Invalid code generation and ICE when template references local static constexpr variable by pointer
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>release blocker
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C++'17
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>andyg1001@hotmail.co.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following minimised code fragment generates incorrect code in all clang
version from 4.0 (oldest I have) up to trunk (tested using godbolt and -O2
-std=c++1z):

  template <const int* X>
  struct S
    {
    static int Value() { return *X; }
    };

  template <typename T>
  int fn()
    {
    static constexpr int value = 10;
    return S<&value>::Value();
    }

  int test()
    {
    return fn<int>();   // will return 0 not 10
    }

There are ways that the code can be made to produce correct values:

  1. Move the 'static constexpr int value = 10' line up to the global scope
  2. Remove the template from 'int fn()' to make it a plain function
  3. Change the template parameter in struct S from 'const int* X' to 'const
int& X'

So, for example, the following compiles correctly...

  template <const int& X>
  struct S
    {
    static int Value() { return X; }
    };

  template <typename T>
  int fn()
    {
    static constexpr int value = 10;
    return S<value>::Value();
    }

  int test()
    {
    return fn<int>();   // will return 0 not 10
    }

The following, slightly more complex example, crashes the compiler:

  template <const int* X>
  struct S
    {
    static int Value() { return *X; }
    };

  template <typename T>
  int fn()
    {
    const int c = ([]() {
        static constexpr int value = 10;
        return S<&value>::Value();
      })();
    return c;
    }

  int test()
    {
    return fn<int>();
    }

Again, any one of the three "solutions" above solves the problem.  So the issue
seems to be in the specific use of pointers in the template for S when called
from another templated function.

However, while we're here, this also crashes the compiler:

 template <const int& X>
  struct S
    {
    static int Value() { return X; }
    };

  template <typename T>
  int fn()
    {
    const int c = ([]() {
        static int value = 10;     // not const or constexpr!
        return S<value>::Value();
      })();
    return c;
    }

  int test()
    {
    return fn<int>();
    }

But utilising a second "solution" from above means this is ok again:

 template <const int& X>
  struct S
    {
    static int Value() { return X; }
    };

  int fn()
    {
    const int c = ([]() {
        static int value = 10;
        return S<value>::Value();
      })();
    return c;
    }

  int test()
    {
    return fn();
    }</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>