Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7106] 4.1 Regression: Compiler crashes when generating KeyPath in conditional NSObject extension of generic container type #49654

Closed
tcldr opened this issue Mar 2, 2018 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself key paths Feature: key paths (both native and Objective-C) regression swift 4.1

Comments

@tcldr
Copy link

tcldr commented Mar 2, 2018

Previous ID SR-7106
Radar rdar://problem/38070998
Original Reporter @tcldr
Type Bug
Status Resolved
Resolution Done
Environment

Apple Swift version 4.1-dev (LLVM 1c8b50929b, Clang 420ae40df6, Swift 08ee342)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, KeyPaths
Assignee @tcldr
Priority Medium

md5: 40f18707bde5a0758b0973b22f8d6fde

is duplicated by:

  • SR-7203 SILGen Assertion failed in emitNativeToCBridgedValue

Issue Description:

Following up on SR-7105, I've noticed a compiler crash in 4.1 when generating a KeyPath for a contained concrete type when that concrete type is an NSObject.

Easier to express in code:

import Foundation

struct Wrapper <Base> {
    let base: Base
}

extension Wrapper where Base: NSObject {
    var valueKeyPath: KeyPath <Base, Int> {
        return \.hashValue
    }
}

This compiles and works correctly in 4.0.3 but not in 4.1-dev:

xcrun --toolchain org.swift.4120180301a swiftc -o keypathbug KeyPathBug4.1.swift                                                                                                                                                            KeyPathBug 
Assertion failed: (bridgedType->isEqual(SGF.getASTContext().getAnyObjectType())), function emitNativeToCBridgedNonoptionalValue, file /Users/buildnode/jenkins/workspace/oss-swift-4.1-package-osx/swift/lib/SILGen/SILGenBridging.cpp, line 666.
0  swift                    0x000000010edcbbe8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010edcc2f6 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff60ca5f5a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2671091904
4  libsystem_c.dylib        0x00007fff60ad0312 abort + 127
5  libsystem_c.dylib        0x00007fff60a98368 basename_r + 0
6  swift                    0x000000010bf6051e emitNativeToCBridgedValue(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 6734
7  swift                    0x000000010bf5eab4 swift::Lowering::SILGenFunction::emitNativeToBridgedValue(swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 116
8  swift                    0x000000010bf7f84c swift::Lowering::Conversion::emit(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) const + 188
9  swift                    0x000000010bf807a6 swift::Lowering::ConvertingInitialization::copyOrInitValueInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, bool) + 198
10 swift                    0x000000010bf2aad2 void copyOrInitValuesInto<((anonymous namespace)::ImplodeKind)1>(swift::Lowering::Initialization*, llvm::ArrayRef<swift::Lowering::ManagedValue>&, swift::CanType, swift::SILLocation, swift::Lowering::SILGenFunction&) + 658
11 swift                    0x000000010bf2a7f0 swift::Lowering::RValue::forwardInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::Initialization*) && + 256
12 swift                    0x000000010bf20464 swift::Lowering::ArgumentSource::getAsSingleValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 148
13 swift                    0x000000010bf7f405 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 549
14 swift                    0x000000010bf20811 swift::Lowering::ArgumentSource::getConverted(swift::Lowering::SILGenFunction&, swift::Lowering::Conversion const&, swift::Lowering::SGFContext) && + 129
15 swift                    0x000000010bf571c8 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3384
16 swift                    0x000000010bf55a34 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus) && + 452
17 swift                    0x000000010bf552c3 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 1971
18 swift                    0x000000010bf42670 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3120
19 swift                    0x000000010bf4a608 swift::Lowering::SILGenFunction::emitGetAccessor(swift::SILLocation, swift::SILDeclRef, llvm::ArrayRef<swift::Substitution>, swift::Lowering::ArgumentSource&&, bool, bool, swift::Lowering::RValue&&, swift::Lowering::SGFContext) + 1736
20 swift                    0x000000010bf96f32 swift::Lowering::SILGenFunction::emitRValueForStorageLoad(swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, bool, swift::AbstractStorageDecl*, swift::Lowering::RValue, llvm::ArrayRef<swift::Substitution>, swift::AccessSemantics, swift::Type, swift::Lowering::SGFContext, bool) + 898
21 swift                    0x000000010bfb0c7e getOrCreateKeyPathGetter(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::AbstractStorageDecl*, llvm::ArrayRef<swift::Substitution>, swift::AccessStrategy, swift::GenericEnvironment*, llvm::ArrayRef<swift::KeyPathPatternComponent::Index>, swift::CanType, swift::CanType) + 2830
22 swift                    0x000000010bfaa3da (anonymous namespace)::RValueEmitter::visitKeyPathExpr(swift::KeyPathExpr*, swift::Lowering::SGFContext) + 2154
23 swift                    0x000000010bf9ef87 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 135
24 swift                    0x000000010bf949c3 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 35
25 swift                    0x000000010c002214 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 468
26 swift                    0x000000010bffff2a swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 7114
27 swift                    0x000000010c00051e swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 8638
28 swift                    0x000000010bffe355 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
29 swift                    0x000000010bfba618 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 424
30 swift                    0x000000010bf3c2f6 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_1::operator()(swift::SILFunction*) const + 262
31 swift                    0x000000010bf345ed swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 621
32 swift                    0x000000010c00af80 swift::ASTVisitor<SILGenExtension, void, void, void, void, void, void>::visit(swift::Decl*) + 416
33 swift                    0x000000010c00922b SILGenExtension::emitExtension(swift::ExtensionDecl*) + 59
34 swift                    0x000000010c0091e5 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 21
35 swift                    0x000000010bf3917b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 907
36 swift                    0x000000010bf39f38 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 360
37 swift                    0x000000010bf3a524 swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&, llvm::Optional<unsigned int>) + 100
38 swift                    0x000000010b884b0c performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13228
39 swift                    0x000000010b880780 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3376
40 swift                    0x000000010b840033 main + 3075
41 libdyld.dylib            0x00007fff60a24115 start + 1
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-01-a.xctoolchain/usr/bin/swift -frontend -c -primary-file KeyPathBug4.1.swift -target x86_64-apple-darwin17.4.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name keypathbug -o /var/folders/20/skxxz6xs12z6t9z90cw56yn40000gn/T/KeyPathBug4.1-e2c3b0.o 
1.  While emitting SIL for getter for valueKeyPath at KeyPathBug4.1.swift:9:9
2.  While silgen emitFunction SIL function "@_T010keypathbug7WrapperVAASo8NSObjectCRbzlE12valueKeyPaths0eF0CyxSiGvg".
 for getter for valueKeyPath at KeyPathBug4.1.swift:9:9
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
@belkadan
Copy link
Contributor

belkadan commented Mar 2, 2018

@swift-ci create

@jckarter
Copy link
Member

As far as I can tell, the assert "always" failed, but this managed to work incidentally with assertions disabled. The crash reproduces for me with a debug compiler but not with the compiler shipped with the Xcode betas.

@tcldr
Copy link
Author

tcldr commented Mar 16, 2018

Yeah, I think it's an issue that has been introduced relatively recently.

I found it whilst checking another issue that had been reported as resolved (SR-7064) was working as expected via the nightly build - which it was.

As the fix for SR-7064 doesn't exist in beta 4, the issue must have surfaced since then.

@jckarter
Copy link
Member

Sorry I wasn't clear. Xcode ships the Swift compiler with assertions disabled, but snapshots are built with assertions enabled. Your test case trips the assertion when assertions are enabled, but appears to compile successfully when assertions are disabled. The code with the assertion hasn't changed since 4.0, so the problem was likely to have been latent in previous versions of the compiler and undiscovered until you tried your codebase with an asserts-enabled snapshot.

@tcldr
Copy link
Author

tcldr commented Mar 16, 2018

Got it, makes sense. Thanks for clarifying!

@jckarter
Copy link
Member

This should fix it on the master branch: #15314

@jckarter
Copy link
Member

Merged to master, so this should hopefully be cleared up in future master branch snapshots.

@tcldr
Copy link
Author

tcldr commented Mar 19, 2018

Thanks, that's great news! I wonder if it solves SR-7105? To my eyes, they seemed very similar.

@jckarter
Copy link
Member

Yeah, that looks like it's very likely to be the same problem. I'll check.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself key paths Feature: key paths (both native and Objective-C) regression swift 4.1
Projects
None yet
Development

No branches or pull requests

4 participants