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-8243] Assertion failed: (archetype->requiresClass() && "don't know what to do") #50775

Open
lilyball mannequin opened this issue Jul 13, 2018 · 13 comments
Open

[SR-8243] Assertion failed: (archetype->requiresClass() && "don't know what to do") #50775

lilyball mannequin opened this issue Jul 13, 2018 · 13 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Jul 13, 2018

Previous ID SR-8243
Radar None
Original Reporter @lilyball
Type Bug
Environment

Xcode 10.0 (10L201y)
Swift 4.2 Snapshot 2018-07-11 (a)
Apple Swift version 4.2-dev (LLVM da1c9a3ae4, Clang a0e59b03e6, Swift 104c96a)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @slavapestov
Priority Medium

md5: a7390e0f869f33ea3d0d0d423849ddb6

Issue Description:

The current Swift 4.2 toolchain crashes on my code with the following stack trace:

Assertion failed: (archetype->requiresClass() && "don't know what to do"), function operator(), file /Users/buildnode/jenkins/workspace/oss-swift-4.2-package-osx/swift/lib/IRGen/GenReflection.cpp, line 733.
0  swift                    0x0000000108fa8938 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000108fa7b77 llvm::sys::RunSignalHandlers() + 39
2  swift                    0x0000000108fa8fb2 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff4fdc1f5a _sigtramp + 26
4  swift                    0x000000010a34bb90 cmark_strbuf__initbuf + 116939
5  libsystem_c.dylib        0x00007fff4fb5f1ae abort + 127
6  libsystem_c.dylib        0x00007fff4fb271ac basename_r + 0
7  swift                    0x00000001058ca4ed swift::Type llvm::function_ref<swift::Type (swift::Type)>::callback_fn<CaptureDescriptorBuilder::getCaptureTypes()::'lambda'(swift::Type)>(long, swift::Type) + 173
8  swift                    0x0000000106a4ac59 llvm::Optional<swift::Type> llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>::callback_fn<swift::Type::transform(llvm::function_ref<swift::Type (swift::Type)>) const::$_15>(long, swift::TypeBase*) + 25
9  swift                    0x0000000106a427bd swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 237
10 swift                    0x0000000106a429e0 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 784
11 swift                    0x0000000106a433e0 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 3344
12 swift                    0x0000000106a433e0 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 3344
13 swift                    0x0000000106a36308 swift::Type::transform(llvm::function_ref<swift::Type (swift::Type)>) const + 40
14 swift                    0x00000001058ca018 CaptureDescriptorBuilder::getCaptureTypes() + 200
15 swift                    0x00000001058c9d50 CaptureDescriptorBuilder::layout() + 32
16 swift                    0x00000001058c8483 ReflectionMetadataBuilder::emit(llvm::Optional<swift::irgen::LinkEntity>, char const*) + 35
17 swift                    0x00000001058c6d70 CaptureDescriptorBuilder::emit() + 208
18 swift                    0x00000001058c6b0b swift::irgen::IRGenModule::getAddrOfCaptureDescriptor(swift::SILFunction&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::HeapLayout const&) + 363
19 swift                    0x0000000105876ee0 swift::irgen::emitFunctionPartialApplication(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::FunctionPointer const&, llvm::Value*, swift::irgen::Explosion&, llvm::ArrayRef<swift::SILParameterInfo>, llvm::ArrayRef<swift::Substitution>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::Explosion&, bool) + 2880
20 swift                    0x000000010592d713 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 49043
21 swift                    0x000000010591da86 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8070
22 swift                    0x000000010582e1cb swift::irgen::IRGenerator::emitLazyDefinitions() + 1051
23 swift                    0x00000001058f9106 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**, unsigned int) + 1382
24 swift                    0x00000001058f964e swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, unsigned int, llvm::GlobalVariable**) + 94
25 swift                    0x0000000105791430 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 15536
26 swift                    0x000000010578c762 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2946
27 swift                    0x000000010574741e main + 1134
28 libdyld.dylib            0x00007fff4fab3015 start + 1

This occurs when emitting the IR SIL function @$S9TwitchKit7ObjcBox33_DA7F7CEE4B414290E144F07A4CB20784LLCyxGIegg_ADyq_GIegg_Ieggg_Iegg_AEIeyBy_AFIeyBy_IeyByy_IeyBy_Rld__CRld_0_Cr0_0_lTR.

That symbol looks a bit odd, I don't know why there are so many layers of function there. The ObjcBox class just looks like

/// A wrapper to provide type-safe round-tripping through ObjC generics.
private class ObjcBox<T> {
    let value: T
    
    init(_ value: T) {
        self.value = value
    }
}

and it's just used as a wrapper for arbitrary Swift types so they can be passed to Obj-C generics. Usage looks like

let resultMapper = TWResultMapper<ObjcBox<Value>, ObjcBox<Error>, ObjcValue, ObjcError>(flatMapBlock: { (resultSwitchBlock) in
    var result: TWResult<ObjcBox<Value>, ObjcBox<Error>>?
    resultSwitchBlock({ (objcValue: ObjcValue) in
        result = .success(withValue: ObjcBox(objcValue._bridgedToSwift()))
    }, { (objcError: ObjcError) in
        result = .failure(withFailure: ObjcBox(objcError._bridgedToSwift()))
    })
    return result!
})

This code compiles just fine with Xcode 9.4.

@belkadan
Copy link
Contributor

cc @rjmccall, aschwaighofer@apple.com (JIRA User), @slavapestov. Anything we can do without the full project?

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Jul 13, 2018

The full project for this is the Twitch iOS app. I'm skeptical that I could get approval to give that to anyone outside the company. Let me know if there's any information I can pull from the project though.

@aschwaighofer
Copy link
Member

I think we are going to need more code than shown.

The failing function is an re-abstraction thunk from native Swift closures to blocks.

reabstraction thunk helper <A, B><A1, B1 where A1: AnyObject, B1: AnyObject> from 
@escaping @callee_guaranteed (
  @guaranteed @escaping @callee_guaranteed (
      @guaranteed @escaping @callee_guaranteed (@guaranteed TwitchKit.(ObjcBox in _DA7F7CEE4B414290E144F07A4CB20784)<A>) -> (), 
      @guaranteed @escaping @callee_guaranteed (@guaranteed TwitchKit.(ObjcBox in _DA7F7CEE4B414290E144F07A4CB20784)<B>) -> ()
  ) -> ()
) -> ()
to
@escaping @callee_unowned @convention(block) (
  @unowned @escaping @callee_unowned @convention(block) (
    @unowned @escaping @callee_unowned @convention(block) (@unowned TwitchKit.(ObjcBox in _DA7F7CEE4B414290E144F07A4CB20784)<A>) -> (),
    @unowned @escaping @callee_unowned @convention(block) (@unowned TwitchKit.(ObjcBox in _DA7F7CEE4B414290E144F07A4CB20784)<B>) -> ()
  ) -> ()
) -> ()

This would be similar to the closure shown above but AFAICT the innermost closures would look something like:
{ (box: ObjectBox<A>) in }

@slavapestov
Copy link
Member

I'll take a look. As a workaround, you can disable reflection metadata emission.

@slavapestov
Copy link
Member

I think I see what the problem is.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Jul 14, 2018

Ok I think the error comes from this particular bit of code:

    private init<ObjcValue, ObjcError>(_bridging future: TWFuture<ObjcValue, ObjcError>, resultMapper: TWResultMapper<ObjcBox<Value>, ObjcBox<Error>, ObjcValue, ObjcError>) {
        self.init(on: .immediate, { (resolver) in
            let completion = resultMapper.flatMapFuture(future, onCancel: {
                resolver.cancel()
            }).onComplete({ (resultSwitchBlock) in
                resultSwitchBlock({ (value) in
                    resolver.fulfill(with: value.value)
                }, { (error) in
                    resolver.reject(with: error.value)
                })
            })
            resolver.onRequestCancel(on: .main, { (_) in
                completion.cancel()
            })
        })
    }

based on the fact that resultSwitchBox in that code has the type (((ObjcBox<Value>) -> Void, (ObjcBox<Error>) -> Void) -> Void), and so I believe the thunk itself corresponds to the block passed to onComplete.

@slavapestov
Copy link
Member

Does ObjectBox conform to _ObjectiveCBridgeable?

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Jul 16, 2018

@slavapestov No it does not. The declaration of ObjcBox in the ticket description is the complete declaration, with no extensions.

@slavapestov
Copy link
Member

I finally found a way to reproduce the issue.

#import <Foundation/Foundation.h>

@interface MyClass<T> : NSObject

typedef void (^_Nonnull MyCallback)(_Nonnull T);
typedef void (^_Nonnull MyCallback2)(_Nonnull MyCallback);

- (void)doSomething:(MyCallback2) arg;
@end

///

class ObjcBox<T> {
  let value: T
  
  init(_ value: T) {
    self.value = value
  }
}

func g<T>(callback: (@escaping (@escaping (ObjcBox<T>) -> Void) -> Void)) {
  MyClass<ObjcBox<T>>().doSomething(callback)
}

@slavapestov
Copy link
Member

I have a very similar problem internally as rdar://22470076 (yes, that's from 2015).

@JaviSoto
Copy link
Contributor

For some reason we're not hitting this crash anymore with the version of the compiler in Xcode Beta 5.

@slavapestov
Copy link
Member

Kevin's original submission indicated he was testing a developer toolchain, which has asserts enabled. I'm not seeing a compile-time crash in a release build either.

@JaviSoto
Copy link
Contributor

Ah, that makes sense. We were using a developer toolchain because of other crashes that were fixed in the nightlies but not yet in the swiftc bundled in Xcode.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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 crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

5 participants