<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 - In Sema::SubstExceptionSpec: Assertion `ESI.Type != EST_Uninstantiated' failed"
   href="https://bugs.llvm.org/show_bug.cgi?id=43151">43151</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>In Sema::SubstExceptionSpec: Assertion `ESI.Type != EST_Uninstantiated' failed
          </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>normal
          </td>
        </tr>

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

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

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

        <tr>
          <th>Reporter</th>
          <td>aaronpuchert@alice-dsl.net
          </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>Created <span class=""><a href="attachment.cgi?id=22445" name="attach_22445" title="Small reproducer for the bug, compile with -std=c++17.">attachment 22445</a> <a href="attachment.cgi?id=22445&action=edit" title="Small reproducer for the bug, compile with -std=c++17.">[details]</a></span>
Small reproducer for the bug, compile with -std=c++17.

Compiling the following code with "-fsyntax-only -std=c++17" produces an
assertion failure:

template<typename T>                                                            
struct X {                                                                      
  using self = X<T>;                                                            

  template<bool noex>
  void f() noexcept(noex)
  {
    auto x = &self::f<noex>;
  }
};

void g()
{
  X<int> x;
  x.f<true>();
}

The trace shows:

1.      <eof> parser at end of file
2.      template-crash.cpp:6:8: instantiating function definition
'X<int>::f<true>'

The issue disappears when removing "-std=c++17", probably because the exception
specification is not part of the type then.

The function is called from clang::Sema::SubstituteExplicitTemplateArguments,
where we have EPI.ExceptionSpec.Type = clang::EST_Uninstantiated and
EPI.ExceptionSpec.SourceDecl = Function. On the other hand, the type of
EPI.ExceptionSpec.SourceTemplate has an ExceptionSpec with Type =
clang::EST_DependentNoexcept. So it seems we should be passing that one to
Sema::SubstExceptionSpec instead.

Indeed, the following seems to fix the assertion (and not introduce new
issues):

--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -3112,6 +3112,13 @@ Sema::SubstituteExplicitTemplateArguments(
     // so substitution into the type must also substitute into the exception
     // specification.
     SmallVector<QualType, 4> ExceptionStorage;
+    if (EPI.ExceptionSpec.Type == EST_Uninstantiated) {
+      assert(Function == EPI.ExceptionSpec.SourceDecl);
+      const auto *TplType = EPI.ExceptionSpec.SourceTemplate->getType()
+                                ->getAs<FunctionProtoType>();
+      EPI.ExceptionSpec = TplType->getExtProtoInfo().ExceptionSpec;
+      assert(EPI.ExceptionSpec.Type != EST_Uninstantiated);
+    }
     if (getLangOpts().CPlusPlus17 &&
         SubstExceptionSpec(
             Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage,

But this seems like a hack, since in other instances the ExceptionSpec is
already instantiated correctly.</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>