<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/82055>82055</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Assertion failure when instantiating template
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          sfc-gh-bhannel
      </td>
    </tr>
</table>

<pre>
    Reproduction in compiler explorer on clang trunk: https://godbolt.org/z/xb4froGYr

Source code:
```
#include <string_view>

template <const std::string_view& sv>
std::string_view f() {
    return sv;
}

template <int x>
void templ() {
    constexpr static std::string_view string_view = "string_view";
    f<string_view>();
}

int main() {
 templ<0>();
    return 0;
}
```

Output:
```
clang-19: /root/llvm-project/clang/lib/Sema/SemaTemplateInstantiate.cpp:4301: llvm::PointerUnion<clang::Decl*, llvm::SmallVector<clang::VarDecl*, 4>*>* clang::LocalInstantiationScope::findInstantiationOf(const clang::Decl*): Assertion `isa<LabelDecl>(D) && "declaration not instantiated in this scope"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-assertions-trunk-20240216/lib/clang/19 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../include/c++/14.0.1 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../include/c++/14.0.1/x86_64-linux-gnu -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../include/c++/14.0.1/backward -internal-isystem /opt/compiler-explorer/clang-assertions-trunk-20240216/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++17 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/example-d6e4e4.o -x c++ <source>
1.      <eof> parser at end of file
2.      <source>:9:6: instantiating function definition 'templ<0>'
3.      <source>:4:18: instantiating function definition 'f<string_view>'
 #0 0x00000000038a3f28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x38a3f28)
 #1 0x00000000038a167c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fc89ca42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007fc89ca969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007fc89ca42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007fc89ca287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007fc89ca2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007fc89ca39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000006c3bf3c clang::LocalInstantiationScope::findInstantiationOf(clang::Decl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6c3bf3c)
 #9 0x0000000006ccb848 clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6ccb848)
#10 0x0000000006c5b2fa (anonymous namespace)::TemplateInstantiator::transformNonTypeTemplateParmRef(clang::Decl*, clang::NonTypeTemplateParmDecl const*, clang::SourceLocation, clang::TemplateArgument, std::optional<unsigned int>) SemaTemplateInstantiate.cpp:0:0
#11 0x0000000006c684bd (anonymous namespace)::TemplateInstantiator::TransformDeclRefExpr(clang::DeclRefExpr*) SemaTemplateInstantiate.cpp:0:0
#12 0x0000000006c4374d clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#13 0x0000000006c454f0 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformInitializer(clang::Expr*, bool) (.part.0) SemaTemplateInstantiate.cpp:0:0
#14 0x0000000006c5a53d clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXConstructExpr(clang::CXXConstructExpr*) SemaTemplateInstantiate.cpp:0:0
#15 0x0000000006c434ff clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#16 0x0000000006c454f0 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformInitializer(clang::Expr*, bool) (.part.0) SemaTemplateInstantiate.cpp:0:0
#17 0x0000000006c458cc clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformReturnStmt(clang::ReturnStmt*) SemaTemplateInstantiate.cpp:0:0
#18 0x0000000006c7c454 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#19 0x0000000006c81c3a clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6c81c3a)
#20 0x0000000006cd2fd1 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6cd2fd1)
#21 0x0000000006cd13ef clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6cd13ef)
#22 0x0000000006cd3276 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6cd3276)
#23 0x0000000006cd13ef clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6cd13ef)
#24 0x0000000006281a69 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#25 0x0000000006282242 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6282242)
#26 0x0000000006107021 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x6107021)
#27 0x00000000060f9d6a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x60f9d6a)
#28 0x000000000413a5e8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x413a5e8)
#29 0x00000000043ac829 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x43ac829)
#30 0x000000000432bd9e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x432bd9e)
#31 0x000000000448de2e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0x448de2e)
#32 0x0000000000c16796 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0xc16796)
#33 0x0000000000c0dfaa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#34 0x0000000000c132f4 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0xc132f4)
#35 0x0000000000b0a354 main (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0xb0a354)
#36 0x00007fc89ca29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#37 0x00007fc89ca29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#38 0x0000000000c0da6e _start (/opt/compiler-explorer/clang-assertions-trunk-20240216/bin/clang-19+0xc0da6e)
clang++: error: unable to execute command: Aborted (core dumped)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWluT46aX_zTqFwqXjGRZfugHj92eTO0k0zXdyWafXAiOZNISaAF1e_Lpt0DyBdsziSfrf-VSE7ct4PA7v3PhgKDGiEoC3EeTd9FkeUc7u1H63pQMVxtcbKiUUN8Vin-5_wytVrxjViiJhERMNa2oQSPYtrXSoJGSiNVUVsjqTr5EyRxtrG1NlMwjsorIqlK8ULUdKV1FZPV7RFbbIi21ev8_OoqXUTzvP59Upxkgpji4oX1LFg__-p8kEZLVHQcUJQtjtZDV-lXAW5Q8HIuy0LQ1tb4XU9JYZCx3QpP58SCSIfO6H3qpCyojkkdkhqLpu74XQghpsJ2WfuzwNJouvza_kBZt95O8KsGRb74k2GOFbauRsdQKdhE2Ov4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