<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/113327>113327</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] Assertion `(NamedDC->InEnclosingNamespaceSetOf(SemaDC) || NewD->isInvalidDecl() || OldD->isInvalidDecl()) && "unexpected context for redeclaration"' failed.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend,
crash-on-invalid,
clang:frontend:fuzzer
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
yijan4845
</td>
</tr>
</table>
<pre>
**This testcase is generated by a fuzzer.**
Compiler Explorer: [https://godbolt.org/z/G5vWMeGEd](https://godbolt.org/z/G5vWMeGEd)
This invalid code will crash on Clang Assertion Trunk:
```cpp
extern "C" {
int template_id1_1 =
};
namespace A {
namespace B {
void g();
}
}
using namespace A::B;
void A::g();
```
It seems that this goes back to clang-7. This issue is quite similar to [60639](https://github.com/llvm/llvm-project/issues/60639) but triggered by `namespace`.
Stack dump:
```
clang++: /root/llvm-project/clang/lib/Sema/SemaDecl.cpp:3504: void adjustDeclContextForDeclaratorDecl(clang::DeclaratorDecl*, clang::DeclaratorDecl*): Assertion `(NamedDC->InEnclosingNamespaceSetOf(SemaDC) || NewD->isInvalidDecl() || OldD->isInvalidDecl()) && "unexpected context for redeclaration"' 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/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1. <source>:12:12: current parser token ';'
#0 0x0000000003bba5e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bba5e8)
#1 0x0000000003bb82ac llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bb82ac)
#2 0x0000000003b05918 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007e6a22042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007e6a220969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007e6a22042476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007e6a220287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007e6a2202871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007e6a22039e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x00000000069d6968 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x69d6968)
#10 0x0000000006a3a66a clang::Sema::MergeFunctionDecl(clang::FunctionDecl*, clang::NamedDecl*&, clang::Scope*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a3a66a)
#11 0x0000000006a42d90 clang::Sema::CheckFunctionDeclaration(clang::Scope*, clang::FunctionDecl*, clang::LookupResult&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a42d90)
#12 0x0000000006a48ce6 clang::Sema::ActOnFunctionDeclarator(clang::Scope*, clang::Declarator&, clang::DeclContext*, clang::TypeSourceInfo*, clang::LookupResult&, llvm::MutableArrayRef<clang::TemplateParameterList*>, bool&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a48ce6)
#13 0x0000000006a4df40 clang::Sema::HandleDeclarator(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef<clang::TemplateParameterList*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a4df40)
#14 0x0000000006a4ea80 clang::Sema::ActOnDeclarator(clang::Scope*, clang::Declarator&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a4ea80)
#15 0x00000000066af01e clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66af01e)
#16 0x00000000066bea99 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66bea99)
#17 0x000000000667eb0e clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667eb0e)
#18 0x000000000667f2ce clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667f2ce)
#19 0x0000000006686a63 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6686a63)
#20 0x00000000066e21f5 clang::Parser::ParseLinkage(clang::ParsingDeclSpec&, clang::DeclaratorContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66e21f5)
#21 0x000000000667ec42 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667ec42)
#22 0x000000000667f2ce clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667f2ce)
#23 0x0000000006686a63 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6686a63)
#24 0x000000000668794d clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x668794d)
#25 0x0000000006687df0 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6687df0)
#26 0x0000000006679e32 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6679e32)
#27 0x000000000452ff28 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452ff28)
#28 0x00000000047eb1e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47eb1e9)
#29 0x0000000004769cde clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4769cde)
#30 0x00000000048d08be clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48d08be)
#31 0x0000000000cde37f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcde37f)
#32 0x0000000000cd607a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#33 0x0000000004573c49 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#34 0x0000000003b05dc4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b05dc4)
#35 0x000000000457423f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#36 0x0000000004539b3d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4539b3d)
#37 0x000000000453ac2d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x453ac2d)
#38 0x0000000004542555 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4542555)
#39 0x0000000000cdb23f clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcdb23f)
#40 0x0000000000baa334 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbaa334)
#41 0x00007e6a22029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#42 0x00007e6a22029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#43 0x0000000000cd5b2e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcd5b2e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
ASM generation compiler returned: 134
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWsty27jSfhp6g6KKBO8LL2TZyu8pZ5KKXP9ZqkCySWFMARwAdOQ8_SmAlEXQsmLPWFnMmZQjUbg0vv660WgQIFLSmgFcOtGVE11fkE5tuLh8on8Q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