swiftlang / swift

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

[SR-11950] SIL parser crash: Assertion failed: (genericConformance->getKind() != ProtocolConformanceKind::Specialized) #54370

Open dan-zheng opened 4 years ago

dan-zheng commented 4 years ago
Previous ID SR-11950
Radar rdar://problem/58435665
Original Reporter @dan-zheng
Type Bug
Environment Apple Swift version 5.2-dev (Swift bec1279414) Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, SILParser | |Assignee | None | |Priority | Medium | md5: 2ec147fe81ad8177c7500ed0a7f8d2c3

Issue Description:

SIL parser crash for SILGen round-trip.

protocol Protocol {
  associatedtype Assoc: Protocol
}

struct Struct<T> {}
extension Struct: Protocol where T == Float {
  struct Assoc: Protocol {
    typealias Assoc = Self
  }
}
$ swiftc -emit-silgen sr-11950.swift | sil-opt
Assertion failed: (genericConformance->getKind() != ProtocolConformanceKind::Specialized), function SpecializedProtocolConformance, file swift-build/swift/lib/AST/ProtocolConformance.cpp, line 977.
Stack dump:
0.  Program arguments: ./build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/bin/sil-opt
1.  With parser at source location: <stdin>:54:3
0  sil-opt                  0x000000010b324465 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  sil-opt                  0x000000010b3233d8 llvm::sys::RunSignalHandlers() + 248
2  sil-opt                  0x000000010b324a66 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007fff728e4b5d _sigtramp + 29
4  sil-opt                  0x000000010d7e4456 cmark_strbuf__initbuf + 143081
5  libsystem_c.dylib        0x00007fff7279e6a6 abort + 127
6  libsystem_c.dylib        0x00007fff7276720d basename_r + 0
7  sil-opt                  0x000000010b8673f3 swift::SpecializedProtocolConformance::SpecializedProtocolConformance(swift::Type, swift::ProtocolConformance*, swift::SubstitutionMap) (.cold.1) + 35
8  sil-opt                  0x00000001086745b0 swift::SpecializedProtocolConformance::computeConditionalRequirements() const + 0
9  sil-opt                  0x000000010849fd07 swift::ASTContext::getSpecializedConformance(swift::Type, swift::ProtocolConformance*, swift::SubstitutionMap) + 407
10 sil-opt                  0x0000000107ece97e (anonymous namespace)::SILParser::parseProtocolConformance(swift::ProtocolDecl*&, swift::GenericEnvironment*&, (anonymous namespace)::ConformanceContext, swift::ProtocolDecl*) + 1838
11 sil-opt                  0x0000000107ed00ed parseSILVTableEntry(swift::Parser&, swift::SILModule&, swift::ProtocolDecl*, swift::GenericEnvironment*, (anonymous namespace)::SILParser&, bool, std::__1::vector<swift::SILWitnessTable::Entry, std::__1::allocator<swift::SILWitnessTable::Entry> >&, std::__1::vector<swift::SILWitnessTable::ConditionalConformance, std::__1::allocator<swift::SILWitnessTable::ConditionalConformance> >&) + 5565
12 sil-opt                  0x0000000107ece027 swift::SILParserTUState::parseSILWitnessTable(swift::Parser&) + 1095
13 sil-opt                  0x00000001083bce37 swift::Parser::parseTopLevel() + 855
...

Output from swiftc -emit-silgen:

$ swiftc -emit-silgen sr-11950.swift
sil_stage raw

import Builtin
import Swift
import SwiftShims

protocol Protocol {
  associatedtype Assoc : Protocol
}

struct Struct<T> {
  init()
}

extension Struct : Protocol where T == Float {
  struct Assoc : Protocol {
    typealias Assoc = Struct<T>.Assoc
    init()
  }
}

// main
sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
  %2 = integer_literal $Builtin.Int32, 0          // user: %3
  %3 = struct $Int32 (%2 : $Builtin.Int32)        // user: %4
  return %3 : $Int32                              // id: %4
} // end sil function 'main'

// Struct.init()
sil hidden [ossa] @$s4main6StructVACyxGycfC : $@convention(method) <T> (@thin Struct<T>.Type) -> Struct<T> {
bb0(%0 : $@thin Struct<T>.Type):
  %1 = alloc_box $<τ_0_0> { var Struct<τ_0_0> } <T>, var, name "self" // user: %2
  %2 = mark_uninitialized [rootself] %1 : $<τ_0_0> { var Struct<τ_0_0> } <T> // users: %5, %3
  %3 = project_box %2 : $<τ_0_0> { var Struct<τ_0_0> } <T>, 0 // user: %4
  %4 = load [trivial] %3 : $*Struct<T>            // user: %6
  destroy_value %2 : $<τ_0_0> { var Struct<τ_0_0> } <T> // id: %5
  return %4 : $Struct<T>                          // id: %6
} // end sil function '$s4main6StructVACyxGycfC'

// Struct<A>.Assoc.init()
sil hidden [ossa] @$s4main6StructVAASfRszlE5AssocVAEySf_GycfC : $@convention(method) (@thin Struct<Float>.Assoc.Type) -> Struct<Float>.Assoc {
bb0(%0 : $@thin Struct<Float>.Assoc.Type):
  %1 = alloc_box ${ var Struct<Float>.Assoc }, var, name "self" // user: %2
  %2 = mark_uninitialized [rootself] %1 : ${ var Struct<Float>.Assoc } // users: %5, %3
  %3 = project_box %2 : ${ var Struct<Float>.Assoc }, 0 // user: %4
  %4 = load [trivial] %3 : $*Struct<Float>.Assoc  // user: %6
  destroy_value %2 : ${ var Struct<Float>.Assoc } // id: %5
  return %4 : $Struct<Float>.Assoc                // id: %6
} // end sil function '$s4main6StructVAASfRszlE5AssocVAEySf_GycfC'

sil_witness_table hidden <T where T == Float> Struct<T>.Assoc: Protocol module main {
  associated_type_protocol (Assoc: Protocol): Struct<Float>.Assoc: specialize <Float> (<T where T == Float> Struct<T>.Assoc: Protocol module main)
  associated_type Assoc: Struct<Float>.Assoc
}

sil_witness_table hidden <T where T == Float> Struct<T>: Protocol module main {
  associated_type_protocol (Assoc: Protocol): Struct<Float>.Assoc: specialize <Float> (<T where T == Float> Struct<T>.Assoc: Protocol module main)
  associated_type Assoc: Struct<T>.Assoc
}
beccadax commented 4 years ago

@swift-ci create