Skip to content

Commit 6b611a6

Browse files
committed
[+0-all-args] Add a test that is a minimum implementation for for-each to be codegen through SILGen without the stdlib.
There was not an example in tree and I am going to use a version of this for my guaranteed normal argument tests. Just chopping this off from a larger commit. rdar://34222540
1 parent cd293d6 commit 6b611a6

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

test/SILGen/minimum_foreach.swift

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %target-swift-frontend -module-name Swift -parse-stdlib -parse-as-library -emit-silgen -enable-sil-ownership %s
2+
3+
// This files contains a minimal implementation for Swift to emit foreach loops
4+
// for a type. It acts partially as a guide for users and since it is in the
5+
// form of a test, it ensures that we will always be able to get this test
6+
// through the type checker.
7+
8+
precedencegroup AssignmentPrecedence { assignment: true }
9+
10+
public protocol ExpressibleByNilLiteral {
11+
init(nilLiteral: ())
12+
}
13+
14+
protocol IteratorProtocol {
15+
associatedtype Element
16+
mutating func next() -> Element?
17+
}
18+
19+
protocol Sequence {
20+
associatedtype Element
21+
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
22+
23+
func makeIterator() -> Iterator
24+
}
25+
26+
enum Optional<T> {
27+
case none
28+
case some(T)
29+
}
30+
31+
func _diagnoseUnexpectedNilOptional(_filenameStart: Builtin.RawPointer,
32+
_filenameLength: Builtin.Word,
33+
_filenameIsASCII: Builtin.Int1,
34+
_line: Builtin.Word) {
35+
// This would usually contain an assert, but we don't need one since we are
36+
// just emitting SILGen.
37+
}
38+
39+
extension Optional : ExpressibleByNilLiteral {
40+
public init(nilLiteral: ()) {
41+
self = .none
42+
}
43+
}
44+
45+
class FakeCollection<T> {
46+
}
47+
48+
struct FakeCollectionIterator<T> {
49+
weak var collection: FakeCollection<T>?
50+
51+
init(_ newCollection: FakeCollection<T>) {
52+
collection = newCollection
53+
}
54+
}
55+
56+
extension FakeCollectionIterator : IteratorProtocol {
57+
public typealias Element = T
58+
public mutating func next() -> Element? {
59+
return .none
60+
}
61+
}
62+
63+
extension FakeCollection : Sequence {
64+
public typealias Element = T
65+
public typealias Iterator = FakeCollectionIterator<T>
66+
public func makeIterator() -> FakeCollectionIterator<T> {
67+
return FakeCollectionIterator(self)
68+
}
69+
}
70+
71+
func useT<T>(_ t: T) {}
72+
73+
func iterateFakeCollection<T>(_ x: FakeCollection<T>) {
74+
for y in x {
75+
useT(y)
76+
}
77+
}

0 commit comments

Comments
 (0)