swiftlang / swift

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

[SR-4243] Compiler Crash with AnyClass and perform(_:with:) #46826

Open saagarjha opened 7 years ago

saagarjha commented 7 years ago
Previous ID SR-4243
Radar None
Original Reporter @saagarjha
Type Bug
Environment macOS Sierra 10.12.4 Beta (16E189a) Xcode Version 8.3 beta 4 (8W143q)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | None | |Priority | Medium | md5: 37d8a6239a77331b2d10808c89154088

Issue Description:

I'm getting a crash with this:

let foo: AnyClass? = NSClassFromString("foo")
foo?.perform(Selector("bar"), with: nil)

Here's a crash log:

0  swift                    0x00000001115a9ef7 PrintStackTraceSignalHandler(void*) + 39
1  swift                    0x00000001115a93a6 SignalHandler(int) + 646
2  libsystem_platform.dylib 0x00007fffe2224b3a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff518a2400 _sigtramp + 1869076704
4  swift                    0x000000010f45e0b6 swift::TypeBase::getContextSubstitutions(swift::DeclContext const*) + 1158
5  swift                    0x000000010f45e392 swift::TypeBase::getMemberSubstitutions(swift::ValueDecl const*) + 146
6  swift                    0x000000010f45e735 swift::TypeBase::getTypeOfMember(swift::ModuleDecl*, swift::ValueDecl const*, swift::LazyResolver*, swift::Type) + 53
7  swift                    0x000000010f1fdfed (anonymous namespace)::CalleeCandidateInfo::CalleeCandidateInfo(swift::Type, llvm::ArrayRef<swift::constraints::OverloadChoice>, bool, swift::constraints::ConstraintSystem*, bool) + 1181
8  swift                    0x000000010f2031b1 (anonymous namespace)::FailureDiagnosis::visitApplyExpr(swift::ApplyExpr*) + 4433
9  swift                    0x000000010f1e4d51 swift::ASTVisitor<(anonymous namespace)::FailureDiagnosis, bool, void, void, void, void, void>::visit(swift::Expr*) + 337
10 swift                    0x000000010f1df116 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 70
11 swift                    0x000000010f20e8bc swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6236
12 swift                    0x000000010f2c2ff5 swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 6229
13 swift                    0x000000010f2c5c28 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 776
14 swift                    0x000000010f1ecf39 (anonymous namespace)::FailureDiagnosis::typeCheckChildIndependently(swift::Expr*, swift::Type, swift::ContextualTypePurpose, swift::OptionSet<TCCFlags, unsigned int>, swift::ExprTypeCheckListener*, bool) + 1993
15 swift                    0x000000010f20d03a std::__1::__function::__func<(anonymous namespace)::FailureDiagnosis::visitExpr(swift::Expr*)::$_13, std::__1::allocator<(anonymous namespace)::FailureDiagnosis::visitExpr(swift::Expr*)::$_13>, swift::Expr* (swift::Expr*)>::operator()(swift::Expr*&&) + 58
16 swift                    0x000000010f413156 swift::Expr::forEachImmediateChildExpr(std::__1::function<swift::Expr* (swift::Expr*)> const&)::ChildWalker::walkToExprPre(swift::Expr*) + 38
17 swift                    0x000000010f3dc0d2 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Expr*) + 4082
18 swift                    0x000000010f1e6e94 swift::ASTVisitor<(anonymous namespace)::FailureDiagnosis, bool, void, void, void, void, void>::visit(swift::Expr*) + 8852
19 swift                    0x000000010f1df116 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 70
20 swift                    0x000000010f20e8bc swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6236
21 swift                    0x000000010f2c2ff5 swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 6229
22 swift                    0x000000010f2c5c28 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 776
23 swift                    0x000000010f33d04a swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 554
24 swift                    0x000000010f341ac9 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 185
25 swift                    0x000000010f353937 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) + 1687
26 swift                    0x000000010ef84d4f swift::CompilerInstance::performSema() + 4975
27 swift                    0x000000010e3c358f swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 15807
28 swift                    0x000000010e3795cc main + 9052
29 libdyld.dylib            0x00007fffe2015235 start + 1
30 libdyld.dylib            0x000000000000000f start + 503229915
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file SwiftCompilerCrash.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name SwiftCompilerCrash -o /var/folders/x3/wncmj8p52t732rrxyz6h424m0000gn/T/SwiftCompilerCrash-690d13.o
1.  While type-checking expression at [SwiftCompilerCrash.swift:28:1 - line:28:40] RangeText="foo?.perform(Selector("bar"), with: nil)"
2.  While type-checking expression at [SwiftCompilerCrash.swift:28:1 - line:28:40] RangeText="foo?.perform(Selector("bar"), with: nil)"
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)
belkadan commented 7 years ago

Workaround:

import Foundation
let foo = NSClassFromString("foo") as? NSObject.Type
foo?.perform(Selector("bar"), with: nil)