swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.36k forks source link

[SR-15940] Assert crash in TypeCheckStmt.cpp #58201

Open swift-ci opened 2 years ago

swift-ci commented 2 years ago
Previous ID SR-15940
Radar None
Original Reporter ellis (JIRA User)
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: c0318b50fadbb361c90032f67b6a66fb

Issue Description: Swift crashes when compiling the following source.

@propertyWrapper
public class Bar<Value> {
  public init(wrappedValue: @autoclosure () -> Value) {}
  public var wrappedValue: Value {}
}

class Foo {
  @Bar var a: Bool?
}

Here is the backtrace

error: compile command failed due to signal 6 (use -v to see invocation)
test.swift:4:35: error: computed property must have accessors specified
public var wrappedValue: Value {}
^
swift-frontend: swift/lib/Sema/TypeCheckStmt.cpp:86: virtual std::pair<bool, Expr *> (anonymous namespace)::ContextualizeClosures::walkToExprPre(swift::Expr *): Assertion `CE->getDiscriminator() == AutoClosureExpr::InvalidDiscriminator' failed.
Please submit a bug report (<https://swift.org/contributing/#reporting-bugs)> and include the project and the crash backtrace.
Stack dump:
0. Program arguments: swift-frontend -frontend -c -primary-file test.swift -target arm64-apple-ios11.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -color-diagnostics -new-driver-path /swift-driver -resource-dir swift -module-name EmojiPickerLayout -target-sdk-version 15.2 -o /tmp/TemporaryDirectory.qXz2A9/EmojiPickerLayout-1.o
1. Swift version 5.6-dev (LLVM a5fb80477a008fd, Swift c2abee2cb7c2471)
2. Compiling with the current language version
3. While evaluating request TypeCheckSourceFileRequest(source_file "test.swift")
4. While type-checking 'Foo' (at test.swift:7:1)
5. While type-checking declaration 0x8841d80 (in module 'EmojiPickerLayout')
#0 0x0000000007d136f3 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) llvm-project/llvm/lib/Support/Unix/Signals.inc:565:13
#1 0x0000000007d11a50 llvm::sys::RunSignalHandlers() llvm-project/llvm/lib/Support/Signals.cpp:98:18
#2 0x0000000007d13a7f SignalHandler(int) llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
#3 0x00007f72a1d3f130 __restore_rt (/usr/local/fbcode/platform009/lib/libpthread.so.0+0x15130)
#4 0x00007f72a17d2530 raise /home/engshare/third-party2/glibc/2.30/src/glibc-2.30/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
#5 0x00007f72a17b951d abort /home/engshare/third-party2/glibc/2.30/src/glibc-2.30/stdlib/abort.c:81:7
#6 0x00007f72a17c94c3 __assert_fail_base /home/engshare/third-party2/glibc/2.30/src/glibc-2.30/assert/assert.c:89:7
#7 0x00007f72a17c9533 (/usr/local/fbcode/platform009/lib/libc.so.6+0x35533)
#8 0x00000000040db1cf swift::AbstractClosureExpr::setDiscriminator(unsigned int) swift/include/swift/AST/Expr.h:3610:5
#9 0x00000000040db1cf (anonymous namespace)::ContextualizeClosures::walkToExprPre(swift::Expr*) swift/lib/Sema/TypeCheckStmt.cpp:87:13
#10 0x00000000043404af (anonymous namespace)::Traversal::doIt(swift::Expr*) swift/lib/AST/ASTWalker.cpp:1197:14
#11 0x00000000043404af (anonymous namespace)::Traversal::doIt(swift::ArgumentList*) swift/lib/AST/ASTWalker.cpp:1360:17
#12 0x00000000043432cd (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) swift/lib/AST/ASTWalker.cpp:856:15
#13 0x000000000433f891 (anonymous namespace)::Traversal::SetParentRAII::~SetParentRAII() swift/lib/AST/ASTWalker.cpp:93:21
#14 0x000000000433f891 (anonymous namespace)::Traversal::visit(swift::Expr*) swift/lib/AST/ASTWalker.cpp:100:3
#15 0x000000000433f891 (anonymous namespace)::Traversal::doIt(swift::Expr*) swift/lib/AST/ASTWalker.cpp:1201:9
#16 0x000000000433f891 swift::Expr::walk(swift::ASTWalker&) swift/lib/AST/ASTWalker.cpp:1891:28
#17 0x00000000040d64ab swift::TypeChecker::contextualizeInitializer(swift::Initializer*, swift::Expr*) swift/lib/Sema/TypeCheckStmt.cpp:202:1
#18 0x0000000004024715 (anonymous namespace)::DeclChecker::visitPatternBindingDecl(swift::PatternBindingDecl*) swift/lib/Sema/TypeCheckDeclPrimary.cpp:2088:13
#19 0x0000000004024715 swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) swift/include/swift/AST/DeclNodes.def:185:1
#20 0x0000000004024715 (anonymous namespace)::DeclChecker::visit(swift::Decl*) swift/lib/Sema/TypeCheckDeclPrimary.cpp:1697:31
#21 0x000000000402a1ac (anonymous namespace)::DeclChecker::visitClassDecl(swift::ClassDecl*) swift/lib/Sema/TypeCheckDeclPrimary.cpp:2478:23
#22 0x0000000004023aeb swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) swift/include/swift/AST/DeclNodes.def:0:9
#23 0x0000000004023aeb (anonymous namespace)::DeclChecker::visit(swift::Decl*) swift/lib/Sema/TypeCheckDeclPrimary.cpp:1697:31
#24 0x000000000402383e swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) swift/lib/Sema/TypeCheckDeclPrimary.cpp:3186:1
#25 0x000000000410e267 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const swift/lib/Sema/TypeChecker.cpp:336:17
#26 0x0000000004110258 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) swift/include/swift/AST/Evaluator.h:362:14
#27 0x000000000410ff72 llvm::Expected<std::tuple<> >::operator bool() llvm-project/llvm/include/llvm/Support/Error.h:564:17
#28 0x000000000410ff72 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) swift/include/swift/AST/Evaluator.h:393:10
#29 0x000000000410e0c4 llvm::Expected<std::tuple<> >::takeError() llvm-project/llvm/include/llvm/Support/Error.h:592:15
#30 0x000000000410e0c4 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) swift/include/swift/AST/Evaluator.h:106:25
#31 0x000000000410e06e swift::performTypeChecking(swift::SourceFile&) swift/lib/Sema/TypeChecker.cpp:286:3
#32 0x000000000321ab3a swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<void (swift::SourceFile&)>) swift/lib/Frontend/Frontend.cpp:1138:19
#33 0x000000000321aa08 _*gnu_cxx::normal_iterator<unsigned int const*, std::vector<unsigned int, std::allocator<unsigned int> > >::*_normal_iterator(unsigned int const* const&) /mnt/gvfs/third-party2/libgcc/9ec50d4571ff7613c2a4dc750831a6d1cb86995f/9.x/platform009/9202ce7/include/c++/9.x/bits/stl_iterator.h:808:20
#34 0x000000000321aa08 std::vector<unsigned int, std::allocator<unsigned int> >::begin() const /mnt/gvfs/third-party2/libgcc/9ec50d4571ff7613c2a4dc750831a6d1cb86995f/9.x/platform009/9202ce7/include/c++/9.x/bits/stl_vector.h:818:16
#35 0x000000000321aa08 std::vector<unsigned int, std::allocator<unsigned int> >::empty() const /mnt/gvfs/third-party2/libgcc/9ec50d4571ff7613c2a4dc750831a6d1cb86995f/9.x/platform009/9202ce7/include/c++/9.x/bits/stl_vector.h:1005:16
#36 0x000000000321aa08 llvm::SetVector<unsigned int, std::vector<unsigned int, std::allocator<unsigned int> >, llvm::DenseSet<unsigned int, llvm::DenseMapInfo<unsigned int> > >::empty() const llvm-project/llvm/include/llvm/ADT/SetVector.h:73:20
#37 0x000000000321aa08 swift::CompilerInstance::isWholeModuleCompilation() const swift/include/swift/Frontend/Frontend.h:454:67
#38 0x000000000321aa08 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<void (swift::SourceFile&)>) swift/lib/Frontend/Frontend.cpp:1129:7
#39 0x000000000321aa08 swift::CompilerInstance::finishTypeChecking() swift/lib/Frontend/Frontend.cpp:1145:3
#40 0x000000000321aa08 swift::CompilerInstance::performSema() swift/lib/Frontend/Frontend.cpp:1070:3
#41 0x000000000308123b withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>) swift/lib/FrontendTool/FrontendTool.cpp:1108:7
#42 0x000000000307838d performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*) swift/lib/FrontendTool/FrontendTool.cpp:0:12
#43 0x000000000307838d performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) swift/lib/FrontendTool/FrontendTool.cpp:1306:19
#44 0x00000000030778f6 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) swift/lib/FrontendTool/FrontendTool.cpp:2080:46
#45 0x0000000003045f83 swift::mainEntry(int, char const**) swift/lib/DriverTool/driver.cpp:405:12
#46 0x00007f72a17b9dc5 __libc_start_main /home/engshare/third-party2/glibc/2.30/src/glibc-2.30/csu/../csu/libc-start.c:308:16
#47 0x000000000304590a _start /home/engshare/third-party2/glibc/2.30/src/glibc-2.30/csu/../sysdeps/x86_64/start.S:122:0
swift-ci commented 2 years ago

Comment by Ellis Hoag (JIRA)

I believe a workaround is to explicitly set the optional variable to nil.

{ @Bar var a: Bool? = nil }
slavapestov commented 2 years ago

@swift-ci create

AnthonyLatsis commented 1 year ago

Apparently, all we did was move the crash from Sema to SILGen. This can be observed by providing wrappedValue with a dummy get {} getter to humor Sema.

tumtumtum commented 2 months ago

I had a play and you can work around this bug by making the closure both @autoclosure and @escaping.