<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 - Crash on invalid on NVRO within template instantiation"
   href="https://bugs.llvm.org/show_bug.cgi?id=51708">51708</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Crash on invalid on NVRO within template instantiation
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>mizvekov@gmail.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rtrieu@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This crash was first observed at this revision:
<a href="https://github.com/llvm/llvm-project/commit/f2d5fce86e81a8b37fbc0829a1c68b6eb48f8365">https://github.com/llvm/llvm-project/commit/f2d5fce86e81a8b37fbc0829a1c68b6eb48f8365</a>

As seen in the test case below, the crash happens when the VarDecl being tested
for NVRO is invalid during template instantiation.

Testing shows that in TemplateDeclInstantiator::VisitVarDecl, if the call to
SemaRef.getNamedReturnInfo is skipped when Var->isInvalidDecl(), then the crash
can be avoided.  Although a better fix would be to handle invalid Decl's when
checking alignment.

$ cat test.ii
template <class>
class a
;
template <typename b>
b f() {
  b d;
  return d;
}
auto e = {
  f< a< int > >() };

$ clang test.ii
test.ii:10:3: error: implicit instantiation of undefined template 'a<int>'
  f< a< int > >() };
  ^
test.ii:2:7: note: template is declared here
class a
      ^
test.ii:5:3: error: implicit instantiation of undefined template 'a<int>'
b f() {
  ^
test.ii:10:3: note: in instantiation of function template specialization
'f<a<int>>' requested here
  f< a< int > >() };
  ^
test.ii:2:7: note: template is declared here
class a
      ^
test.ii:6:5: error: implicit instantiation of undefined template 'a<int>'
  b d;
    ^
test.ii:2:7: note: template is declared here
class a
      ^
clang/lib/AST/RecordLayoutBuilder.cpp:3256: const clang::ASTRecordLayout
&clang::ASTContext::getASTRecordLayout(const clang::RecordDecl *) const:
Assertion `D && "Cannot get layout of forward declarations!"' failed.</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>