You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-263 Implement a general solution to prevent deadlocks when generic types rely on their own metadata recursively
relates to:
SR-263 Implement a general solution to prevent deadlocks when generic types rely on their own metadata recursively
Issue Description:
consider the following code:
enum _TrieNode<Element : Equatable> {
case leaf(Array<Element>)
case node(Element, Array<_TrieNode<Element>>)
mutating func insert<I>(_ iterator: inout I) -> _TrieNode<Element>? where I : IteratorProtocol, I.Element == Element {
if let element = iterator.next() {
switch self {
case .leaf(let elements):
let filtered = elements.filter { $0 != element }
var items = [Element]()
while let item = iterator.next() {
items.append(item)
}
self = .node(element, [.leaf(items)])
return .leaf(filtered)
case .node(let nodeElement, let children_):
var children = children_
for idx in 0..<children.count {
if children[idx].matches(element) {
if let additional = children[idx].insert(&iterator) {
children.append(additional)
}
break
}
}
self = .node(nodeElement, children)
return nil
}
}
return nil
}
func matches(_ element: Element) -> Bool {
switch self {
case .leaf(let elements):
return elements.contains(element)
case .node(let nodeElement, _):
return nodeElement == element
}
}
}
struct Trie<Element : Equatable> {
var nodes = [_TrieNode<Element>]()
mutating func insert<C>(_ elements: C) where C : Sequence, C.Iterator.Element == Element {
var iter = elements.makeIterator()
if let element = iter.next() {
for idx in 0..<nodes.count {
if nodes[idx].matches(element) {
if let additional = nodes[idx].insert(&iter) {
nodes.append(additional)
}
return
}
}
}
}
}
var trie = Trie<String.Element>()
trie.insert("hello")
trie.insert("hello world")
Since this code compiles I would expect it would execute with no failures. Unfortunately it seems to hit a runtime failure?
Either these types of metadata dependencies should not be created or it should be compile failure. Additionally this debugging information is not very actionable to help avoid this type of cyclical graph.
The text was updated successfully, but these errors were encountered:
I'm not sure if this is possible in the fully general case (because generics can result in the instantiation of other generics by calling functions). @rjmccall would know more.
@phausler This was actually mostly fixed in 4.2, with remaining work for class metadata now in swift-5.0-branch. We now support cyclic metadata, there's no need to diagnose anything 🙂
Additional Detail from JIRA
md5: b457abbc31a13836352c0eba2d4a2ccc
duplicates:
relates to:
Issue Description:
consider the following code:
Since this code compiles I would expect it would execute with no failures. Unfortunately it seems to hit a runtime failure?
Either these types of metadata dependencies should not be created or it should be compile failure. Additionally this debugging information is not very actionable to help avoid this type of cyclical graph.
The text was updated successfully, but these errors were encountered: