forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast-dump-json-no-crash.swift
486 lines (415 loc) · 9.82 KB
/
ast-dump-json-no-crash.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// This test is by no means exhaustive, but attempts to catch any places in the
// implementation of ASTDumper's JSON where it might do an unprotected call to
// some method on AST node that would cause an assertion due to some
// unsatisfied precondition. This is a good place to put regression tests if
// issues are discovered in the future.
//
// This file should not contain top-level code, since it contains a `@main`
// type. It should also not contain code that requires Obj-C; put that in
// ast-dump-json-objc-no-crash.swift instead.
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -target %target-swift-5.9-abi-triple -swift-version 6 -I %S/Inputs/dependencies -parse-as-library -dump-ast -dump-ast-format json %s -module-name main -o - >/dev/null
structE:Error{}
@main
structMainThing{
staticfunc main(){}
}
// Statements
func f1(){
return()
}
func f2(){
defer{print()}
iftrue{print()}else{print()}
guardtrueelse{fatalError()}
whiletrue{print()}
do{print()}
do{throwE()}catchlet e1 as E{ _ = e1 }catch{ _ = error }
repeat{print()}whiletrue
for_in[]wheretrue{}
forcase let.some(x)in[1,nil,3]where x %2==0{}
switchInt.random(in:0...10){
case0: fallthrough
case1...6:break
caselet xwhere x %2==0:break
default:break
}
label1:for_in[]{continue label1 }
label2:for_in[]{break label2 }
structS1:~Copyable {
deinit{}
consumingfunc f(){ discard self }
}
structS2{
init?(){returnnil}
}
}
// Declarations
protocolP1{
varv1:Int{getset}
varv2:Int{getset}
func f()
associatedtypeA1
associatedtypeA2=Int
associatedtypeA3:BinaryIntegerwhere A3:Codable
}
protocolP2:AnyObjectwhere Self:C1{}
publicfinalclassC1:P1{
publictypealiasA1=String
publictypealiasA3=Int32
publicvarv1:Int{
willSet {print()}
didSet {print()}
}
publicvarv2:Int{
get{10}
set{print()}
}
publicinit(){
self.v1 =0
self._v3 =0
}
publicfunc f(){}
publicvar_v3:Int
publicvarv3:Int{
@storageRestrictions(accesses: v1, initializes: _v3)
init { _v3 =10}
get{0}
set{print()}
}
}
openclassC2{
publicrequiredinit(){}
func f1(){}
classfunc f2(){}
staticfunc f3(){}
classvarv2:Int{0}
nonisolated(unsafe)staticvarv3:Int=0
staticvarv4:Int{0}
}
classC3:C2{
requiredinit(){ super.init()}
overridefunc f1(){}
overrideclassfunc f2(){ super.f2()}
}
actorAC1{
nonisolatedfunc f1(){}
}
enumE1{
indirectcase a(E1)
case b, c(Int), d(x:Int, y:String="")
}
indirectenumE2{case e1(E2), e2 }
structS1<A, B:BinaryInteger>where A:StringProtocol{
func f1()where A ==String, B ==Int{}
}
typealiasTA1<B>=S1<String,B> where B: FixedWidthInteger
@MainActorpublicstructMAS{}
@MainActorprotocolMAP{func f()}
structMASInferred:MAP{
func f(){}
}
@dynamicMemberLookup
structDynLook{
subscript(dynamicMember member:String)->String{return member }
func f(){
_ =self.hello
}
}
func z1(){
_ =DynLook().hello
}
@dynamicCallable
structDynCall{
func dynamicallyCall(withKeywordArguments args:KeyValuePairs<String,String>){}
func f(){
self(label:"value")
}
}
func z2(){
letdc=DynCall()
dc(label:"value")
}
func z3(){
vars1:S1<Substring,UInt>
varta1:TA1<Int32>
}
func f1()->someBinaryInteger{0}
func f2<T:BinaryInteger>(_ t:T){}
func f3(_ t:someBinaryInteger){}
func f4(_ values:Int...){}
func z4(){
f4(1,2,3)
}
func f5( _ value:@autoclosure()->Int){}
func z5(){
f5(10)
}
func f6(_ x:Int=10){}
func f7(_ x:inoutInt){ x =50}
func z7(){
varf7x=10; f7(&f7x)
}
func f8()asyncthrows->Int{0}
func f9()asyncthrows{
_ =tryawaitf8()
asyncletx=f8()
print(tryawait x)
}
func f10(isolation:anyActor= #isolation){}
structLazyHolder{
lazy varv:Int=10
}
structAccessors{
privatevar_x:Int
varx:Int{
_read{ yield _x }
_modify{ yield &_x }
}
}
structSynthEq:Equatable{
varx:Int
vary:String
}
structSynthHash:Hashable{
varx:Int
vary:String
}
structSynthCode:Codable{
varx:Int
vary:String
}
enumSynthComp:Comparable{
case a
case b(Int)
case c(Int,String)
}
import Swift
importstruct Swift.Int
@preconcurrencyimport Swift
publicimport Swift
import UserClangModule
@freestanding(expression)
macro Macro1<T:BinaryInteger>(t:T)->String= #externalMacro(module:"DummyModule", type:"DummyType")
@attached(member)
macro Macro2<T:BinaryInteger>(t:T)= #externalMacro(module:"DummyModule", type:"DummyType")
structToExtend<T>{}
extensionToExtend{
func f1(){}
}
extensionToExtendwhere T ==Int{
func f2(){}
}
extension[Int]{
func f3(){}
}
precedencegroupMooglePrecedence{
higherThan:AdditionPrecedence
associativity:left
assignment:false
}
infix operator ^*^: MooglePrecedence
@resultBuilder
structStringBuilder{
staticfunc buildBlock(_ parts:String...)->String{
parts.joined()
}
}
@StringBuilderfunc sb1()->String{
"hello"
""
"world"
}
func sb2(@StringBuilder _ body:()->String){print(body())}
func sb3(){
sb2{
"hello"
""
"world"
}
}
func nestedOpaques0()->someBinaryInteger{2}
func nestedOpaques1()->someFixedWidthInteger&SignedInteger{2}
func nestedOpaques2()->(someBinaryInteger,someSequence){(2,[])}
func nestedOpaques3()->(someBinaryInteger,someSequence<Double>){(2,[])}
func nestedOpaques4()->(someBinaryInteger)?{Bool.random()?2:nil}
func nestedOpaques5()->[someBinaryInteger]{[2]}
// Expressions
func zz1()throws{
let _:Unicode.Scalar="a"
let _:Character="a\u{0301}"
_ ="abc"
_ =true
_ =10
_ =0x10
_ =10.0
_ =0x10p4
_ ="a\(10)b"
_ = #file
_ = #line
_ =/hello(?<capture>\s+)world/
_ =Array<Int>(repeating:0, count:2)
letarray1=[1,2,3]
_ =array1[0]
letdict1=["a":1,"b":2,"c":3]
_ =dict1["a"]
lettup1=(1, b:2, c:3)
_ = tup1.0
_ = tup1.b
letj:Int=4
_ =(1+9)*-3/ j << 2
structCAF{
func callAsFunction(x:Int){}
}
letcaf=CAF()
caf(x:10)
structKP{
varx:KP2?
}
structKP2{
vary:Int
}
letkp=KP()
_ =kp[keyPath: \.x?.y]
_ =kp[keyPath: \KP.x?.y]
letcv=5
_ = copy cv
structNonCop:~Copyable {}
letnoncop=NonCop()
_ = consume noncop
func thrower()throws(E)->Int{throwE()}
_ =trythrower()
_ =try?thrower()
_ =try!thrower()
_ ={[kp, cv]in
print(kp, cv)
}
_ ={@MainActor@Sendable(x:Int)->Stringin
return"\(x)"
}
_ ={(x:Int)->Intin
trythrower()
}
_ ={(x:Int)->Stringin
tryawaitf9()
return"ok"
}
let _:(Int,Int)->Int={ $0 + $1 }
}
structPack<eachT>{
func f(_ t:repeateachT){
repeat g(each t)
}
func g<U>(_ t:U){}
}
func tuplify<eachT>(_ value:repeateachT)->(repeateachT){
return(repeateach value)
}
func example<eachT>(_ value:repeateachT){
letabstractTuple=tuplify(repeateach value)
repeat print(each abstractTuple)
}
func anySeq<T>(_ type:T.Type=T.self)->anySequence<T>{[]}
func anySeqUser(){
lets=anySeq(Int.self)
letiter= s.makeIterator()
}
func opaqueSeq<T>(_ type:T.Type=T.self)->someSequence<T>{[]}
func opaqueSeqUser(){
lets=opaqueSeq(Int.self)
letiter= s.makeIterator()
}
letx=10
func zz1b(){
_ =type(of: x)
}
structRebindInit{
varx:Int
init(){self=.init(x:0)}
init(x:Int){self.x = x }
}
@propertyWrapper
structPropWrap<T>{
varwrappedValue:T
varprojectedValue:PropWrap<T>{self}
init(wrappedValue:T){self.wrappedValue = wrappedValue }
}
structWrapperHolder{
@PropWrapvarw:Int=0
mutatingfunc f(){
self.w =0
self._w =PropWrap(wrappedValue:1)
_ =self.$w
}
}
func zz2(){
structS3{
varx:Int
}
lets3:S3?=nil
_ = s3?.x.bigEndian
_ = s3!.x.bigEndian
_ =5asInt
_ =5as?String
_ =5as!Double
lette1:Int
lette2:String
(te1, te2)=(1,"hello")
letan1:Any
an1 =5
lettrn=Bool.random()?10:20
}
protocolP9{
func f()->Self
}
func doSomething(_ p:anyP9){
p.f() // implicit opening
}
func zz3(){
letah:AnyHashable=10
}
// Patterns
enumForPattern{
case a
case b(value:Int, name:String)
case c(value:Int, name:String)
case d(Int)
case e(Bool)
}
func ffp(_ value:ForPattern){
switch value {
case.a:break
case.b(let v,let n):print(v, n); break
caselet.c(v, n):print(v, n); break
case.d(let x)where x >10:break
case.d(20):break
case.d:break
case.e(true):break
case.e(false):break
}
switch5{
case is Int:break
case _:break;
}
}
func ffpo(_ value:ForPattern?){
switch value {
case.a?:break
case.d(x as Int):break
default:break
}
}
// Various other constructs
@available(iOS, deprecated:14.0, renamed:"newThing()", message:"Use the new thing")
func oldThing(){}
@available(iOS, introduced:14.0)
func newThing(){}
func newThingClient(){
if #available(iOS 14.0,*){
newThing()
}else{
oldThing()
}
}
dynamicfunc toBeReplaced(arg:Int){}
@_dynamicReplacement(for:toBeReplaced(arg:))
func toReplaceWith(arg:Int){}