swiftlang / swift

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

[SR-9533] Compiler crash: same name associatedtype in conditional conformance #51984

Closed kateinoigakukun closed 2 years ago

kateinoigakukun commented 5 years ago
Previous ID SR-9533
Radar None
Original Reporter @kateinoigakukun
Type Bug
Environment DEVELOPMENT-SNAPSHOT-2018-12-17-a
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | @slavapestov | |Priority | Medium | md5: a92d370a0edc9ed1c579e9affae8f3d7

Issue Description:

protocol P1 {
    associatedtype X
}

protocol P2 {
    associatedtype X
}

struct Box<T> {}

extension Box: P1 where T: P1 {
    typealias X = T.X
}

extension Box: P2 where T: P2 {
}

func XType<T>(_ type: T.Type) -> T.X.Type where T: P2 {
    return T.X.self
}

struct A: P2, P1 {
    typealias X = Int
}

This code doesn't crash. But adding below code results segment fault.

let _ = XType(Box<A>.self)
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file /Users/kateinoigakukun/projects/swift-source/llvm/include/llvm/Support/Casting.h, line 106.
Stack dump:
0.      Program arguments: /Users/kateinoigakukun/projects/swift-source/build/Xcode-DebugAssert/swift-macosx-x86_64/Debug/bin/swift -frontend -interpret cond_crash.swift -enable-objc-interop -sdk /Applications/Xcode-10.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name cond_crash
0  swift                    0x000000010f48938c llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 60
1  swift                    0x000000010f489929 PrintStackTraceSignalHandler(void*) + 25
2  swift                    0x000000010f4860fe llvm::sys::RunSignalHandlers() + 990
3  swift                    0x000000010f48a735 SignalHandler(int) + 485
4  libsystem_platform.dylib 0x00007fff55557f5a _sigtramp + 26
5  libsystem_platform.dylib 0x00007ffee95c9ac0 _sigtramp + 2483493760
6  libsystem_c.dylib        0x00007fff552f51ae abort + 127
7  libsystem_c.dylib        0x00007fff552bd1ac basename_r + 0
8  swift                    0x0000000109299449 llvm::isa_impl_cl<swift::SugarType, swift::TypeBase const*>::doit(swift::TypeBase const*) + 89
9  swift                    0x00000001092993d8 llvm::isa_impl_wrap<swift::SugarType, swift::TypeBase const*, swift::TypeBase const*>::doit(swift::TypeBase const* const&) + 24
10 swift                    0x00000001092993b2 llvm::isa_impl_wrap<swift::SugarType, swift::TypeBase* const, swift::TypeBase const*>::doit(swift::TypeBase* const&) + 34
11 swift                    0x0000000109299305 bool llvm::isa<swift::SugarType, swift::TypeBase*>(swift::TypeBase* const&) + 21
12 swift                    0x0000000109299254 swift::TypeBase::getDesugaredType() + 36
13 swift                    0x00000001092a4135 swift::AnyFunctionType* swift::TypeBase::castTo<swift::AnyFunctionType>() + 21
14 swift                    0x00000001096c9d17 swift::GenericFunctionType::substGenericArgs(swift::SubstitutionMap) + 119
15 swift                    0x0000000107cb3188 (anonymous namespace)::Callee::getSubstFormalInterfaceType(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SubstitutionMap) + 72
16 swift                    0x0000000107cc2f3c (anonymous namespace)::Callee::Callee(swift::Lowering::SILGenFunction&, swift::SILDeclRef, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SubstitutionMap, swift::SILLocation, bool) + 188
17 swift                    0x0000000107cc2e6d (anonymous namespace)::Callee::Callee(swift::Lowering::SILGenFunction&, swift::SILDeclRef, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SubstitutionMap, swift::SILLocation, bool) + 157
18 swift                    0x0000000107c9533d (anonymous namespace)::Callee::forDirect(swift::Lowering::SILGenFunction&, swift::SILDeclRef, swift::SubstitutionMap, swift::SILLocation, bool) + 413
19 swift                    0x0000000107cafbb4 (anonymous namespace)::SILGenApply::visitDeclRefExpr(swift::DeclRefExpr*) + 1668
20 swift                    0x0000000107cae93c swift::ASTVisitor<(anonymous namespace)::SILGenApply, void, void, void, void, void, void>::visit(swift::Expr*) + 332
21 swift                    0x0000000107cb5a19 (anonymous namespace)::SILGenApply::visitApplyExpr(swift::ApplyExpr*) + 553
22 swift                    0x0000000107cb05c8 swift::ASTVisitor<(anonymous namespace)::SILGenApply, void, void, void, void, void, void>::visitCallExpr(swift::CallExpr*) + 40
23 swift                    0x0000000107caed36 swift::ASTVisitor<(anonymous namespace)::SILGenApply, void, void, void, void, void, void>::visit(swift::Expr*) + 1350
24 swift                    0x0000000107cae478 (anonymous namespace)::SILGenApply::decompose(swift::Expr*) + 40
25 swift                    0x0000000107c940f1 (anonymous namespace)::CallEmission::forApplyExpr(swift::Lowering::SILGenFunction&, swift::Expr*) + 113
26 swift                    0x0000000107c9401c swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 92
27 swift                    0x0000000107d6c5d2 (anonymous namespace)::RValueEmitter::visitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 66
28 swift                    0x0000000107d5897a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visitCallExpr(swift::CallExpr*, swift::Lowering::SGFContext) + 74
29 swift                    0x0000000107d4399b swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 3851
30 swift                    0x0000000107d30f74 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 196
31 swift                    0x0000000107d30cee swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 350
32 swift                    0x0000000107d0e48b swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 555
33 swift                    0x0000000107d0e77f swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 159
34 swift                    0x0000000107c697be swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 1534
35 swift                    0x0000000107c6911b swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 811
36 swift                    0x0000000107c6af2e swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 1390
37 swift                    0x0000000107c6a62d swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 989
38 swift                    0x0000000107c6b723 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*) + 1123
39 swift                    0x0000000107c6c276 swift::performSILGeneration(swift::ModuleDecl*, swift::SILOptions&) + 38
40 swift                    0x00000001067277d4 generateSILModules(swift::CompilerInvocation&, swift::CompilerInstance&) + 1604
41 swift                    0x000000010671c027 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1879
42 swift                    0x0000000106719ac7 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5063
43 swift                    0x000000010664f6e5 run_driver(llvm::StringRef, llvm::ArrayRef<char const*>) + 917
44 swift                    0x000000010664def2 main + 3730
45 libdyld.dylib            0x00007fff55249015 start + 1
belkadan commented 5 years ago

Typealiases in constrained extensions aren't actually constrained today, which means this is allowed as a conformance of P2 when it shouldn't be. (The compiler shouldn't crash, of course.) cc @DougGregor, @slavapestov

ChristopherRogers commented 5 years ago

You can also get the compiler to crash with the Swift 5 compiler if you add a constraint on the associated type. (Curiously enough, Swift 4.2 doesn't crash here.) (Since it's so similar I just decided to post it here instead of create a new issue.)

protocol AssociatedTypeProto {}
class AssociatedTypeProtoClass: AssociatedTypeProto {}

protocol Proto1 {
  associatedtype SameNameAssociatedType: AssociatedTypeProto
}

protocol Proto2 {
  associatedtype SameNameAssociatedType: AssociatedTypeProto
}

struct Box<T> {}

extension Box: Proto1 where T: Proto1 {
  typealias SameNameAssociatedType = T.SameNameAssociatedType
//  typealias SameNameAssociatedType = AssociatedTypeProtoClass // OK
}

extension Box: Proto2 where T: Proto2 {}

This gets you this output on master with assertions enabled:

Invalid conformance substitution
UNREACHABLE executed at /Users/me/Code/swift-source/swift/lib/AST/ProtocolConformance.cpp:130!
Stack dump:
0.  Program arguments: /Users/me/Code/swift-source/build/Ninja-RelWithDebInfoAssert+swift-DebugAssert/swift-macosx-x86_64/bin/swift -frontend -c -primary-file bug.swift -target x86_64-apple-darwin18.2.0 -enable-objc-interop -color-diagnostics -module-name bug -o /var/folders/0j/fm70ht1j0_sg60z_jd0hlmbw0000gp/T/bug-75cb1f.o 
1.  While emitting witness table for protocol conformance to 'Proto2' (at bug.swift:8:1) for type 'Box<T>' (declared at [bug.swift:12:1 - line:12:16] RangeText="struct Box<T> {")
0  swift                    0x000000010bce4d88 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010bce4005 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010bce5392 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff6f0adb3d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000000046 _sigtramp + 2431984934
5  libsystem_c.dylib        0x00007fff6ef6b1c9 abort + 127
6  swift                    0x000000010bc7f5c4 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 532
7  swift                    0x0000000109339689 swift::ProtocolConformanceRef::subst(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolDecl*)>) const + 825
8  swift                    0x000000010933933d swift::ProtocolConformanceRef::subst(swift::Type, swift::SubstitutionMap) const + 141
9  swift                    0x000000010933f99e swift::SpecializedProtocolConformance::getAssociatedConformance(swift::Type, swift::ProtocolDecl*, swift::LazyResolver*) const + 206
10 swift                    0x000000010933f563 swift::ProtocolConformance::getAssociatedConformance(swift::Type, swift::ProtocolDecl*, swift::LazyResolver*) const + 211
11 swift                    0x0000000106d011bd (anonymous namespace)::WitnessTableBuilder::addAssociatedConformance(swift::AssociatedConformance) + 301
12 swift                    0x0000000106d0094d swift::SILWitnessVisitor<(anonymous namespace)::WitnessTableBuilder>::visitProtocolDecl(swift::ProtocolDecl*) + 829
13 swift                    0x0000000106cee91d (anonymous namespace)::WitnessTableBuilder::build() + 61
14 swift                    0x0000000106cee49b swift::irgen::IRGenModule::emitSILWitnessTable(swift::SILWitnessTable*) + 299
15 swift                    0x0000000106becc75 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1925
16 swift                    0x0000000106d8559d 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**) + 2349
17 swift                    0x0000000106d8632f 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&, llvm::GlobalVariable**) + 655
18 swift                    0x0000000106a3006d generateIR(swift::IRGenOptions&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >&, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >) + 781
19 swift                    0x0000000106a0851e performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 6222
20 swift                    0x00000001069f9beb performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 2683
21 swift                    0x00000001069f7367 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5063
22 swift                    0x0000000106913675 run_driver(llvm::StringRef, llvm::ArrayRef<char const*>) + 917
23 swift                    0x0000000106911b42 main + 3730
24 libdyld.dylib            0x00007fff6eec2ed9 start + 1
25 libdyld.dylib            0x000000000000000d start + 2433995061
<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)
slavapestov commented 2 years ago

I remember fixing this a while back:

q.swift:15:1: error: type 'Box<T>' does not conform to protocol 'P2'
extension Box: P2 where T: P2 {
^
q.swift:6:20: note: protocol requires nested type 'X'; do you want to add it?
    associatedtype X
                   ^