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-7610] Should-be-benign change breaks standard library #50152

Open
dabrahams opened this issue May 5, 2018 · 7 comments
Open

[SR-7610] Should-be-benign change breaks standard library #50152

dabrahams opened this issue May 5, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@dabrahams
Copy link
Collaborator

Previous ID SR-7610
Radar rdar://problem/40036642
Original Reporter @dabrahams
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 2ba8b11b9c16831c05b7859b4a0d577f

Issue Description:

Reproduced with github/master at 154e8c3; this bug blocks progress on unifying Sequence and Collection. Applying [this change| dabrahams@f5c999e] should not cause any code to break, but instead:

/Users/dave/src/s/swift/stdlib/public/core/Dictionary.swift:1308:17: error: type 'Dictionary<Key, Value>.Values' does not conform to protocol 'MutableCollection'
  public struct Values
                ^
/Users/dave/src/s/swift/stdlib/public/core/MutableCollection.swift:98:3: note: protocol requires subscript with type '(Dictionary<Key, Value>.Values.Index) -> Dictionary<Key, Value>.Values.Element'; do you want to add a stub?
  subscript(position: Index) -> Element { get set }
  ^
/Users/dave/src/s/swift/stdlib/public/core/StringUnicodeScalarView.swift:618:10: warning: unavailable subscript 'subscript' was used to satisfy a requirement of protocol 'RangeReplaceableCollection'
  public subscript(r: Range<Index>) -> String.UnicodeScalarView.SubSequence {
         ^
/Users/dave/src/s/swift/stdlib/public/core/RangeReplaceableCollection.swift:369:3: note: requirement 'subscript' declared here
  subscript(bounds: Range<Index>) -> SubSequence { get }
  ^
@belkadan
Copy link
Contributor

belkadan commented May 7, 2018

@swift-ci create

@huonw
Copy link
Mannequin

huonw mannequin commented May 15, 2018

(Unfortunately it looks like {panel:title=...}...{panel} is significantly less clear than the plain old {code:none}...{code} form, since JIRA was trying to interpret the {}s inside the former.)

@huonw
Copy link
Mannequin

huonw mannequin commented May 15, 2018

My first suspicion is that this is an associated-type-inference issue, where this change is perturbing the exact decisions made about Dictionary.Values.Index, somehow.

However, various strategies for helping the compiler, like adding {{public typealias Index = DictionaryIndex<Key, Value>}} to Dictionary.Values (and/or converting all the Index references in it to an explicit DictionaryIndex<Key, Value>), result in an assertion failure:

Assertion failed: (entry.is<const TypeInfo*>() && "getting TypeInfo recursively!"), function getCompleteTypeInfo, file /Users/huon/projects/swift/swift/lib/IRGen/GenType.cpp, line 1367.
0  swift                    0x0000000109ffc628 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000109ffcd36 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff77917f5a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2288943296
4  libsystem_c.dylib        0x00007fff776b51ae abort + 127
5  libsystem_c.dylib        0x00007fff7767d1ac basename_r + 0
6  swift                    0x00000001069bacfe swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 78
7  swift                    0x00000001069af1f2 swift::irgen::TypeConverter::convertTupleType(swift::TupleType*) + 466
8  swift                    0x00000001069bbc5a swift::irgen::TypeConverter::convertType(swift::CanType) + 714
9  swift                    0x00000001069bb433 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 659
10 swift                    0x00000001069bacd5 swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 37
11 swift                    0x00000001069193c2 swift::irgen::EnumImplStrategy::get(swift::irgen::TypeConverter&, swift::SILType, swift::EnumDecl*) + 594
12 swift                    0x000000010691a67b swift::irgen::TypeConverter::convertEnumType(swift::TypeBase*, swift::CanType, swift::EnumDecl*) + 203
13 swift                    0x00000001069bc495 swift::irgen::TypeConverter::convertAnyNominalType(swift::CanType, swift::NominalTypeDecl*) + 165
14 swift                    0x00000001069bba1d swift::irgen::TypeConverter::convertType(swift::CanType) + 141
15 swift                    0x00000001069bb433 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 659
16 swift                    0x00000001069bacd5 swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 37
17 swift                    0x00000001069a4242 swift::irgen::TypeConverter::convertStructType(swift::TypeBase*, swift::CanType, swift::StructDecl*) + 4082
18 swift                    0x00000001069bc4b8 swift::irgen::TypeConverter::convertAnyNominalType(swift::CanType, swift::NominalTypeDecl*) + 200
19 swift                    0x00000001069bba1d swift::irgen::TypeConverter::convertType(swift::CanType) + 141
20 swift                    0x00000001069bb433 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 659
21 swift                    0x00000001069bacd5 swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 37
22 swift                    0x00000001068b8c93 (anonymous namespace)::AllocStackHoisting::run() + 419
23 swift                    0x00000001071af853 swift::SILPassManager::runPassOnFunction(unsigned int, swift::SILFunction*) + 1475
24 swift                    0x00000001071b07a3 swift::SILPassManager::runFunctionPasses(unsigned int, unsigned int) + 1315
25 swift                    0x00000001071b1464 swift::SILPassManager::execute() + 660
26 swift                    0x00000001069d00eb swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 187
27 swift                    0x00000001069cfb1f runIRGenPreparePasses(swift::SILModule&, swift::irgen::IRGenModule&) + 159
28 swift                    0x00000001069ce0cc 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) + 972
29 swift                    0x00000001069ccc9c swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**) + 844
30 swift                    0x000000010686c801 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 15521
31 swift                    0x0000000106867b56 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3318
32 swift                    0x00000001068244fd main + 2317
33 libdyld.dylib            0x00007fff77609015 start + 1
Stack dump:
0.  Program arguments: /Users/huon/projects/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift -frontend -c -filelist /var/folders/3k/4q7m7bkn2k750rpr0jm8s9fr0000gn/T/sources-14ece9 -supplementary-output-file-map /var/folders/3k/4q7m7bkn2k750rpr0jm8s9fr0000gn/T/supplementaryOutputs-0ea42b -disable-objc-attr-requires-foundation-module -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Older_Xcodes/Xcode_2018-04-17_11-14.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -I /Users/huon/projects/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/./lib/swift/macosx/x86_64 -F /Applications/Older_Xcodes/Xcode_2018-04-17_11-14.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/../../../Developer/Library/Frameworks -warn-swift3-objc-inference-complete -g -module-cache-path /Users/huon/projects/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/./module-cache -module-link-name swiftCore -nostdimport -parse-stdlib -resource-dir /Users/huon/projects/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/./lib/swift -swift-version 3 -O -D INTERNAL_CHECKS_ENABLED -D SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS -enable-resilience -group-info-path /Users/huon/projects/swift/swift/stdlib/public/core/GroupInfo.json -verify-syntax-tree -enable-sil-ownership -Xllvm -sil-inline-generics -Xllvm -sil-partial-specialization -Xcc -D__SWIFT_CURRENT_DYLIB=swiftCore -parse-as-library -module-name Swift -o /Users/huon/projects/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/stdlib/public/core/macosx/x86_64/Swift.o 
1.  Contents of /var/folders/3k/4q7m7bkn2k750rpr0jm8s9fr0000gn/T/sources-14ece9:
---
...
---
2.  While running pass #&#8203;3248 SILFunctionTransform "AllocStackHoisting" on SILFunction "@$Ss10DictionaryV4KeysV11descriptionSSvg".
 for getter for description at /Users/huon/projects/swift/swift/stdlib/public/core/Dictionary.swift:1296:16
3.  While converting type 'IndexingIterator<Dictionary<τ_0_0, τ_0_1>.Keys>' (declared at [/Users/huon/projects/swift/swift/stdlib/public/core/Collection.swift:85:8 - line:108:1] RangeText="struct IndexingIterator<Elements : Collection> {
  @usableFromInline
  internal let _elements: Elements
  @usableFromInline
  internal var _position: Elements.Index

  @inlinable
  @inline(__always)
  /// Creates an iterator over the given collection.
  public /// @testable
  init(_elements: Elements) {
    self._elements = _elements
    self._position = _elements.startIndex
  }

  @inlinable
  @inline(__always)
  /// Creates an iterator over the given collection.
  public /// @testable
  init(_elements: Elements, _position: Elements.Index) {
    self._elements = _elements
    self._position = _position
  }
}")
4.  While converting type 'DefaultIndex<Dictionary<τ_0_0, τ_0_1>.Keys>' (declared at [/Users/huon/projects/swift/swift/stdlib/public/core/Collection.swift:1803:8 - line:1875:1] RangeText="enum DefaultIndex<Base: Swift.Sequence> : Comparable {

  init() { self = .end }

  init(_ s: Base) {
    var i = s.makeIterator()
    if let e = i.next() {
      self = .element(0, e, i)
    }
    else {
      self = .end
    }
  }

  /// An index of an element in the collection.
  ///
  /// The associated values are:
  /// - The zero-based position in the collection, for `Comparable` purposes.
  /// - The element itself, so that it only needs to be computed once.
  /// - The state, immediately after generating the element at this index.
  case element(Int, Base.Element, Base.Iterator)
  
  /// An index representing the end of the collection.
  case end
  
  public static func ==(lhs: DefaultIndex, rhs: DefaultIndex) -> Bool {
    switch (lhs, rhs) {
      case let (.element(l, _, _), .element(r, _, _)): return l == r
      case (.end, .end): return true
      default: return false
    }
  }
  
  public static func < (lhs: DefaultIndex, rhs: DefaultIndex) -> Bool {
    switch (lhs, rhs) {
      case let (.element(l, _, _), .element(r, _, _)): return l < r
      case (.element, .end): return true
      default: return false
    }
  }

  fileprivate mutating func stepForward() {
    switch self {
      case .element(let pos, _, var iterator):
        if let e = iterator.next() {
          self = .element(pos + 1, e, iterator)
        } else {
          self = .end
        }
      case .end:
        fatalError("Can't advance past end")
    }
  }

  public func distance(to other: DefaultIndex) -> Int {
    switch (self, other) {
      case (.end, .end): return 0
      case (.element(let l, _, _), .element(let r, _, _)): return r - l
      default: break
    }
    var i = self
    var n = 0
    while i != .end { i.stepForward(); n += 1 }
    return n
  }

  fileprivate var element: Base.Element {
    guard case .element(_, let e, _) = self else {
      fatalError("Can't subscript at end")
    }
    return e
  }
}")
5.  While converting type '(Int, τ_0_0, IndexingIterator<Dictionary<τ_0_0, τ_0_1>.Keys>)'

@huonw
Copy link
Mannequin

huonw mannequin commented May 15, 2018

Yep, the original problem seems to be associated type inference:

(lldb) p Conformance->dump()
(normal_conformance type=Dictionary<Key, Value>.Values protocol=MutableCollection
  (assoc_type req=Element type=Value)
  (assoc_type req=Index type=DefaultIndex<Dictionary<Key, Value>.Values>)
  (assoc_type req=SubSequence type=Slice<Dictionary<Key, Value>.Values>))

There's only one DefaultIndex and that's the new one.

The work-around crashing seems worth a separate bug.

@dabrahams
Copy link
Collaborator Author

Apparently this change works around the problem now.

@dabrahams
Copy link
Collaborator Author

Ah, not entirely. We get the same crash later building the SIMD module.

{code:none}

Assertion failed: (entry.is<const TypeInfo*>() && "getting TypeInfo recursively!"), function getCompleteTypeInfo, file /Users/dave/src/s/swift/lib/IRGen/GenType.cpp, line 1367.
0 swift 0x0000000108b8ef78 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 swift 0x0000000108b8df77 llvm::sys::RunSignalHandlers() + 39
2 swift 0x0000000108b8f5f2 SignalHandler(int) + 258
3 libsystem_platform.dylib 0x00007fff547adf5a _sigtramp + 26
4 libdyld.dylib 0x00007fff5449f149 dyldGlobalLockRelease() + 0
5 libsystem_c.dylib 0x00007fff5454b1ae abort + 127
6 libsystem_c.dylib 0x00007fff545131ac basename_r + 0
7 swift 0x00000001052e5ded swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 141
8 swift 0x00000001052cf1da swift::irgen::TypeConverter::convertStructType(swift::TypeBase*, swift::CanType, swift::StructDecl*) + 4090
9 swift 0x00000001052e74c8 swift::irgen::TypeConverter::convertAnyNominalType(swift::CanType, swift::NominalTypeDecl*) + 200
10 swift 0x00000001052e6a40 swift::irgen::TypeConverter::convertType(swift::CanType) + 144
11 swift 0x00000001052e63d5 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 629
12 swift 0x00000001052e5d8e swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 46
13 swift 0x00000001052da210 swift::irgen::TypeConverter::convertTupleType(swift::TupleType*) + 480
14 swift 0x00000001052e6c7d swift::irgen::TypeConverter::convertType(swift::CanType) + 717
15 swift 0x00000001052e63d5 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 629
16 swift 0x00000001052e5d8e swift::irgen::IRGenModule::getTypeInfo(swift::SILType) + 46
17 swift 0x0000000105242f3a swift::irgen::EnumImplStrategy::get(swift::irgen::TypeConverter&, swift::SILType, swift::EnumDecl*) + 586
18 swift 0x00000001052441db swift::irgen::TypeConverter::convertEnumType(swift::TypeBase*, swift::CanType, swift::EnumDecl*) + 203
19 swift 0x00000001052e74a5 swift::irgen::TypeConverter::convertAnyNominalType(swift::CanType, swift::NominalTypeDecl*) + 165
20 swift 0x00000001052e6a40 swift::irgen::TypeConverter::convertType(swift::CanType) + 144
21 swift 0x00000001052e63d5 swift::irgen::TypeConverter::getTypeEntry(swift::CanType) + 629
22 swift 0x00000001052e5cf0 swift::irgen::IRGenModule::getTypeInfoForLowered(swift::CanType) + 16
23 swift 0x000000010533d42d isLargeLoadableType(swift::GenericEnvironment*, swift::SILType, swift::irgen::IRGenModule&) + 157
24 swift 0x000000010533c7f6 LargeSILTypeMapper::getNewSILType(swift::GenericEnvironment*, swift::SILType, swift::irgen::IRGenModule&) + 550
25 swift 0x000000010533e962 (anonymous namespace)::LoadableByAddress::run() + 1954
26 swift 0x0000000105b1ceea swift::SILPassManager::runModulePass(unsigned int) + 346
27 swift 0x0000000105b1d847 swift::SILPassManager::execute() + 775
28 swift 0x00000001052fb2fb swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 187
29 swift 0x00000001052fad2f runIRGenPreparePasses(swift::SILModule&, swift::irgen::IRGenModule&) + 159
30 swift 0x00000001052f92dc 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) + 972
31 swift 0x00000001052f7eac swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::1::unique_ptr<swift::SILModule, std::1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::1::basic_string<char, std::1::char_traits<char>, std::
_1::allocator<char> > >, llvm::GlobalVariable**) + 844
32 swift 0x000000010519380f performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 15455
33 swift 0x000000010518eba6 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3318
34 swift 0x000000010514b80d main + 2317
35 libdyld.dylib 0x00007fff5449f015 start + 1
36 libdyld.dylib 0x0000000000000028 start + 2880835604

 

@dabrahams
Copy link
Collaborator Author

This is related to SR-7605

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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
Projects
None yet
Development

No branches or pull requests

2 participants