Issue 202705
Summary C++: Assertion failed on static template when invalidly assigned via undefined function or postfix of recursive reference
Labels new issue
Assignees
Reporter lmaltsis
    ```cpp
template <class data> class foo
{
 public:
	static const int a;
	static const int b;
	static const int c;
};

template <class data> const int foo<data>::a = undefined();
template <class data> const int foo<data>::b = a;
template <class data> const int foo<data>::c = b;

typedef foo<int> fooInt;

int main( void )
{
    fooInt *f;

    f = new fooInt();

    if (f->c != 0)
      return 1;
}
```
I've done my best trying to reduce this one but it remains complicated and invalid code.
https://godbolt.org/z/1WP44KPeG 
https://github.com/llvm/llvm-project/commit/e34dc2960ce94be069ac2a973c943b61ef9b3b23
(requires assertions to be enabled)

Alternatively:
```cpp
template <class data> class foo
{
    public:
	static const int a;
	static const int b;
	static const int c;
};

template <class data> const int foo<data>::a = a++;
template <class data> const int foo<data>::b = a;
template <class data> const int foo<data>::c = b;

typedef foo<int> fooInt;

int main( void )
{
    fooInt *f;

    f = new fooInt();

    if (f->c != 0)
 return 1;
}
```

```sh
$ clang /tmp/bug.cpp
<source>:9:48: error: use of undeclared identifier 'undefined'
    9 | template <class data> const int foo<data>::a = undefined();
      | ^~~~~~~~~
clang++: /root/llvm-project/llvm/tools/clang/lib/AST/ExprConstant.cpp:3451: bool evaluateVarDeclInit({anonymous}::EvalInfo&, const clang::Expr*, const clang::VarDecl*, {anonymous}::CallStackFrame*, unsigned int, clang::APValue*&): Assertion `!VD->mightBeUsableInConstantExpressions(Info.Ctx)' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.	<source>:21:18: current parser token ')'
2.	<source>:16:1: parsing function body 'main'
3.	<source>:16:1: in compound statement ('{}')
 #0 0x000000000437c168 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x437c168)
 #1 0x0000000004378fa4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4378fa4)
 #2 0x00000000043795c4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43795c4)
 #3 0x00000000042b4af8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x000070dc79245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x000070dc7929eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
 #6 0x000070dc7924527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
 #7 0x000070dc792288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
 #8 0x000070dc7922881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
 #9 0x000070dc7923b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x0000000007fb4758 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7fb4758)
#11 0x0000000007fb5ed7 findCompleteObject((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::AccessKinds, (anonymous namespace)::LValue const&, clang::QualType) ExprConstant.cpp:0:0
#12 0x0000000007fd2415 handleLValueToRValueConversion((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::QualType, (anonymous namespace)::LValue const&, clang::APValue&, bool) ExprConstant.cpp:0:0
#13 0x0000000008057d38 (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#14 0x0000000007fd93b1 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::IntExprEvaluator, bool>::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#15 0x0000000008003b1f Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
#16 0x00000000080048b2 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&) ExprConstant.cpp:0:0
#17 0x0000000008008e3c clang::Expr::isCXX11ConstantExpr(clang::ASTContext const&, clang::APValue*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8008e3c)
#18 0x00000000080092b0 EvaluateCPlusPlus11IntegralConstantExpr(clang::ASTContext const&, clang::Expr const*, llvm::APSInt*) ExprConstant.cpp:0:0
#19 0x000000000800d2f1 clang::Expr::getIntegerConstantExpr(clang::ASTContext const&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x800d2f1)
#20 0x0000000006ea248c AnalyzeImplicitConversions(clang::Sema&, clang::Expr*, clang::SourceLocation, bool) (.constprop.0) SemaChecking.cpp:0:0
#21 0x0000000006ea35f5 clang::Sema::CheckCompletedExpr(clang::Expr*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ea35f5)
#22 0x000000000727c1c7 clang::Sema::ActOnFinishFullExpr(clang::Expr*, clang::SourceLocation, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x727c1c7)
#23 0x00000000071bd6e0 clang::Sema::ActOnCondition(clang::Scope*, clang::SourceLocation, clang::Expr*, clang::Sema::ConditionKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71bd6e0)
#24 0x0000000006cceaf2 clang::Parser::ParseCXXCondition(clang::ActionResult<clang::Stmt*, true>*, clang::SourceLocation, clang::Sema::ConditionKind, bool, clang::Parser::ForRangeInfo*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cceaf2)
#25 0x0000000006d39edd clang::Parser::ParseParenExprOrCondition(clang::ActionResult<clang::Stmt*, true>*, clang::Sema::ConditionResult&, clang::SourceLocation, clang::Sema::ConditionKind, clang::SourceLocation&, clang::SourceLocation&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d39edd)
#26 0x0000000006d3ed66 clang::Parser::ParseIfStatement(clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d3ed66)
#27 0x0000000006d3c823 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d3c823)
#28 0x0000000006d3d749 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d3d749)
#29 0x0000000006d45f6e clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d45f6e)
#30 0x0000000006d4678a clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d4678a)
#31 0x0000000006c4a603 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c4a603)
#32 0x0000000006c81913 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+++0x6c81913)
#33 0x0000000006c3db4b clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c3db4b)
#34 0x0000000006c3e34f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c3e34f)
#35 0x0000000006c45e11 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c45e11)
#36 0x0000000006c46d45 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c46d45)
#37 0x0000000006c27bfa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c27bfa)
#38 0x0000000004e6ab58 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e6ab58)
#39 0x00000000051a1688 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51a1688)
#40 0x000000000511e8cd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x511e8cd)
#41 0x00000000052a179d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x52a179d)
#42 0x0000000000e214ff cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xe214ff)
#43 0x0000000000e17eba ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#44 0x0000000000e1803d int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#45 0x0000000004edd539 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
#46 0x00000000042b4f34 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42b4f34)
#47 0x0000000004eddb86 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
#48 0x0000000004e9a062 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e9a062)
#49 0x0000000004e9b00e 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+++0x4e9b00e)
#50 0x0000000004ea3215 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ea3215)
#51 0x0000000000e1d8ec clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xe1d8ec)
#52 0x0000000000cc173a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcc173a)
#53 0x000070dc7922a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#54 0x000070dc7922a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#55 0x0000000000e17955 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xe17955)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to