llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

Assertion failure compiling template aliases with enums #27799

Open llvmbot opened 8 years ago

llvmbot commented 8 years ago
Bugzilla Link 27425
Version trunk
OS All
Attachments Patch with fix
Reporter LLVM Bugzilla Contributor
CC @Quuxplusone,@DougGregor,@zygoloid

Extended Description

With the following testcase:

//t.cpp
template <class T> using A = enum B : T {};

clang hits an assertion failure:

clang -std=c++11 t.cpp
clang-3.9: llvm/tools/clang/include/clang/AST/TypeNodes.def:98: clang::TypeInfo clang::ASTContext::getTypeInfoImpl(const clang::Type*) const: Assertion `!T->isDependentType() && "should not see dependent types here"' failed.
0  clang-3.9 0x0000000013436c60 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 68
1  clang-3.9 0x0000000013437050
2  clang-3.9 0x000000001343516c llvm::sys::RunSignalHandlers() + 180
3  clang-3.9 0x0000000013436514
4            0x00003fff87790478 __kernel_sigtramp_rt64 + 0
5  libc.so.6 0x00003fff871e0890 abort + 640
6  libc.so.6 0x00003fff871d4724
7  libc.so.6 0x00003fff871d4814 __assert_fail + 100
8  clang-3.9 0x00000000163ed090 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const + 880
9  clang-3.9 0x00000000163ecc98 clang::ASTContext::getTypeInfo(clang::Type const*) const + 192
10 clang-3.9 0x0000000013766850
11 clang-3.9 0x00000000137668b4
12 clang-3.9 0x000000001640ae60 clang::ASTContext::getIntWidth(clang::QualType) const + 160
13 clang-3.9 0x000000001550a2f0 clang::Sema::ActOnEnumBody(clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, clang::Decl*, llvm::ArrayRef<clang::Decl*>, clang::Scope*, clang::AttributeList*) + 1184
14 clang-3.9 0x0000000014ff2ee8 clang::Parser::ParseEnumBody(clang::SourceLocation, clang::Decl*) + 2444
15 clang-3.9 0x0000000014ff241c clang::Parser::ParseEnumSpecifier(clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext) + 5408
16 clang-3.9 0x0000000014fefbf0 clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) + 11816
17 clang-3.9 0x0000000014feb4b0 clang::Parser::ParseSpecifierQualifierList(clang::DeclSpec&, clang::AccessSpecifier, clang::Parser::DeclSpecContext) + 112
18 clang-3.9 0x0000000014fdf350 clang::Parser::ParseTypeName(clang::SourceRange*, clang::Declarator::TheContext, clang::AccessSpecifier, clang::Decl**, clang::ParsedAttributes*) + 224
19 clang-3.9 0x000000001500be38 clang::Parser::ParseUsingDeclaration(unsigned int, clang::Parser::ParsedTemplateInfo const&, clang::SourceLocation, clang::SourceLocation&, clang::AccessSpecifier, clang::Decl**) + 2364
20 clang-3.9 0x000000001500b164 clang::Parser::ParseUsingDirectiveOrDeclaration(unsigned int, clang::Parser::ParsedTemplateInfo const&, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&, clang::Decl**) + 544
21 clang-3.9 0x000000001508770c clang::Parser::ParseSingleDeclarationAfterTemplate(unsigned int, clang::Parser::ParsedTemplateInfo const&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::AccessSpecifier, clang::AttributeList*) + 448
22 clang-3.9 0x00000000150874e0 clang::Parser::ParseTemplateDeclarationOrSpecialization(unsigned int, clang::SourceLocation&, clang::AccessSpecifier, clang::AttributeList*) + 1112
23 clang-3.9 0x0000000015087048 clang::Parser::ParseDeclarationStartingWithTemplate(unsigned int, clang::SourceLocation&, clang::AccessSpecifier, clang::AttributeList*) + 284
24 clang-3.9 0x0000000014fe8a40 clang::Parser::ParseDeclaration(unsigned int, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&) + 508
25 clang-3.9 0x0000000014fc4afc clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) + 3272
26 clang-3.9 0x0000000014fc3dd8 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 812
27 clang-3.9 0x0000000014fbecd8 clang::ParseAST(clang::Sema&, bool, bool) + 464
28 clang-3.9 0x0000000013dd3dd4 clang::ASTFrontendAction::ExecuteAction() + 468
29 clang-3.9 0x000000001430521c clang::CodeGenAction::ExecuteAction() + 1720
30 clang-3.9 0x0000000013dd36a0 clang::FrontendAction::Execute() + 188
31 clang-3.9 0x0000000013d66f2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1304
32 clang-3.9 0x0000000013f51904 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1692
33 clang-3.9 0x000000001129adc0 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1156
34 clang-3.9 0x000000001128a68c
35 clang-3.9 0x000000001128b028 main + 1844
36 libc.so.6 0x00003fff871c4580
37 libc.so.6 0x00003fff871c4774 __libc_start_main + 196

clang version 3.9.0 (trunk 266784) (llvm/trunk 266782)

This failure was introduced in r222906 - Create a new 'flag_enum' attribute, specifically the change in Sema::ActOnEnumBody:

@@ -13586,10 +13629,8 @@ void Sema::ActOnEnumBody(SourceLocation
       BestPromotionType = Context.getPromotedIntegerType(BestType);
     else
       BestPromotionType = BestType;
-    // We don't need to set BestWidth, because BestType is going to be the type
-    // of the enumerators, but we do anyway because otherwise some compilers
-    // warn that it might be used uninitialized.
-    BestWidth = CharWidth;
+
+    BestWidth = Context.getIntWidth(BestType);
   }

I have attached a patch with a potential fix for this issue

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 8 years ago

OK, so the real problem is that this code is simply not supposed to be reachable for an enumeration defined in a dependent context. The check at the start of ActOnEnumBody is supposed to bail out in this case, but EnumDecl::isDependentType is unable to cope with the (ill-formed) case of an enum defined within an alias template.

Ideally, we should fix DeclContext::isDependentContext to -- somehow -- correctly detect the case where a context is dependent because it's nested within a variable template.

Alternatively, we could fix this by detecting the problematic case sooner (perhaps in the parser).

llvmbot commented 8 years ago

With the attached patch the code is rejected at a later point in GetDeclSpecTypeForDeclarator:

clang -std=c++11 t.cpp
/home/rcraik/defects/127660/t.cpp:1:35: error: 'B' cannot be defined in a type alias template
template <class T> using A = enum B : T {};
                                  ^
1 error generated.

I did not send the patch to cfe-commits because, as you say, it is not a general solution; I just attached it as a starting point. Perhaps I should have worded it better in the initial description.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 8 years ago

And for your future reference, patches should be posted to cfe-commits@ rather than bugzilla.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 8 years ago

The right fix would be to just reject this:

  template <class T> using A = enum B : T {};

... because ([dcl.typedef]/2) "The defining-type-specifier-seq of the defining-type-id shall not define a class or enumeration if the alias-declaration is the declaration of a template-declaration."

(Also, the attached patch doesn't fix the assert in full generality -- the enum-base might be some other kind of dependent type, not necessarily just a template parameter type.)

Endilll commented 1 year ago

Still crashing as of post-16 trunk: https://godbolt.org/z/dMbco848E

clang++: /root/build/tools/clang/include/clang/AST/TypeNodes.inc:73:
clang::TypeInfo clang::ASTContext::getTypeInfoImpl(const clang::Type*) const:
Assertion `!T->isDependentType() && "should not see dependent types here"' 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 -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.  <source>:1:43: current parser token ';'
 #0 0x000055f2aea9ac1a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a9ac1a)
 #1 0x000055f2aea9884c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a9884c)
 #2 0x000055f2ae9e7020 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007fd217c36420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007fd21770300b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #5 0x00007fd2176e2859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #6 0x00007fd2176e2729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #7 0x00007fd2176f3fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #8 0x000055f2b1b06446 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b06446)
 #9 0x000055f2b1b051b2 clang::ASTContext::getTypeInfo(clang::Type const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b051b2)
#10 0x000055f2b1b16b83 clang::ASTContext::getIntWidth(clang::QualType) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b16b83)
#11 0x000055f2b110c535 clang::Sema::ActOnEnumBody(clang::SourceLocation, clang::SourceRange, clang::Decl*, llvm::ArrayRef<clang::Decl*>, clang::Scope*, clang::ParsedAttributesView const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x610c535)
#12 0x000055f2b0e63ae8 clang::Parser::ParseEnumBody(clang::SourceLocation, clang::Decl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e63ae8)
#13 0x000055f2b0e68b92 clang::Parser::ParseEnumSpecifier(clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e68b92)
#14 0x000055f2b0e6a354 clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*, clang::ImplicitTypenameContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e6a354)
#15 0x000055f2b0e6d2a4 clang::Parser::ParseSpecifierQualifierList(clang::DeclSpec&, clang::ImplicitTypenameContext, clang::AccessSpecifier, clang::Parser::DeclSpecContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e6d2a4)
#16 0x000055f2b0e6d7a1 clang::Parser::ParseTypeName(clang::SourceRange*, clang::DeclaratorContext, clang::AccessSpecifier, clang::Decl**, clang::ParsedAttributes*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e6d7a1)
#17 0x000055f2b0e79ef2 clang::Parser::ParseAliasDeclarationAfterDeclarator(clang::Parser::ParsedTemplateInfo const&, clang::SourceLocation, clang::Parser::UsingDeclarator&, clang::SourceLocation&, clang::AccessSpecifier, clang::ParsedAttributes&, clang::Decl**) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e79ef2)
#18 0x000055f2b0e82241 clang::Parser::ParseUsingDeclaration(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo const&, clang::SourceLocation, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e82241)
#19 0x000055f2b0e82c25 clang::Parser::ParseUsingDirectiveOrDeclaration(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo const&, clang::SourceLocation&, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e82c25)
#20 0x000055f2b0f1196f clang::Parser::ParseSingleDeclarationAfterTemplate(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo const&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f1196f)
#21 0x000055f2b0f176f3 clang::Parser::ParseTemplateDeclarationOrSpecialization(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f176f3)
#22 0x000055f2b0f17a96 clang::Parser::ParseDeclarationStartingWithTemplate(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f17a96)
#23 0x000055f2b0e737f4 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e737f4)
#24 0x000055f2b0e439c0 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e439c0)
#25 0x000055f2b0e45176 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e45176)
#26 0x000055f2b0e4549f clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e4549f)
#27 0x000055f2b0e3a1f3 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e3a1f3)
#28 0x000055f2afb67560 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b67560)
#29 0x000055f2af4892e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44892e9)
#30 0x000055f2af41dc42 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x441dc42)
#31 0x000055f2af560c9f clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4560c9f)
#32 0x000055f2ac222cc3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1222cc3)
#33 0x000055f2ac21ade8 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#34 0x000055f2af2912f9 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
#35 0x000055f2ae9e74a5 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x39e74a5)
#36 0x000055f2af293eee clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4293eee)
#37 0x000055f2af26060b clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x426060b)
#38 0x000055f2af2611e3 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+++0x42611e3)
#39 0x000055f2af269fd6 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4269fd6)
#40 0x000055f2ac220a8e clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1220a8e)
#41 0x000055f2ac151cb1 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1151cb1)
#42 0x00007fd2176e4083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#43 0x000055f2ac21a4da _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x121a4da)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend