Skip to content

Commit 3d20897

Browse files
authored
Merge pull request github#13910 from rdmarsh2/rdmarsh2/swift/for-await-cfg
Swift: CFG test for for-try-await
2 parents 90888e5 + 7053c62 commit 3d20897

File tree

4 files changed

+283
-0
lines changed

4 files changed

+283
-0
lines changed

swift/ql/test/library-tests/ast/PrintAst.expected

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,6 +3209,90 @@ cfg.swift:
32093209
# 519| getElement(6): [ReturnStmt] return ...
32103210
# 519| getResult(): [DeclRefExpr] x
32113211
# 519| getResult().getFullyConverted(): [LoadExpr] (Int) ...
3212+
# 522| [NamedFunction] testAsyncFor()
3213+
# 522| InterfaceType = () async -> ()
3214+
# 522| getBody(): [BraceStmt] { ... }
3215+
# 523| getVariable(0): [ConcreteVarDecl] stream
3216+
# 523| Type = AsyncStream<Int>
3217+
# 523| getElement(0): [PatternBindingDecl] var ... = ...
3218+
# 523| getInit(0): [CallExpr] call to AsyncStream<Element>.init(_:bufferingPolicy:_:)
3219+
# 523| getFunction(): [MethodLookupExpr] AsyncStream<Element>.init(_:bufferingPolicy:_:)
3220+
# 523| getBase(): [TypeExpr] AsyncStream<Int>.Type
3221+
# 523| getTypeRepr(): [TypeRepr] AsyncStream<Int>
3222+
# 523| getMethodRef(): [DeclRefExpr] AsyncStream<Element>.init(_:bufferingPolicy:_:)
3223+
# 523| getArgument(0): [Argument] : Int.Type
3224+
# 523| getExpr(): [TypeExpr] Int.Type
3225+
# 523| getTypeRepr(): [TypeRepr] Int
3226+
# 523| getExpr().getFullyConverted(): [DotSelfExpr] .self
3227+
# 523| getArgument(1): [Argument] bufferingPolicy: call to ...
3228+
# 523| getExpr(): [CallExpr] call to ...
3229+
# 523| getFunction(): [MethodLookupExpr] .bufferingNewest
3230+
# 523| getBase(): [TypeExpr] AsyncStream<Int>.Continuation.BufferingPolicy.Type
3231+
# 523| getTypeRepr(): [TypeRepr] AsyncStream<Int>.Continuation.BufferingPolicy
3232+
# 523| getMethodRef(): [DeclRefExpr] bufferingNewest
3233+
# 523| getArgument(0): [Argument] : 5
3234+
# 523| getExpr(): [IntegerLiteralExpr] 5
3235+
# 523| getArgument(2): [Argument] : { ... }
3236+
# 523| getExpr(): [ExplicitClosureExpr] { ... }
3237+
# 524| getParam(0): [ParamDecl] continuation
3238+
# 524| Type = AsyncStream<Int>.Continuation
3239+
# 523| getBody(): [BraceStmt] { ... }
3240+
# 525| getElement(0): [BraceStmt] { ... }
3241+
# 525| getElement(0): [CallExpr] call to detached(priority:operation:)
3242+
# 525| getFunction(): [MethodLookupExpr] .detached(priority:operation:)
3243+
# 525| getBase(): [TypeExpr] Task<(), Never>.Type
3244+
# 525| getTypeRepr(): [TypeRepr] Task<(), Never>
3245+
# 525| getMethodRef(): [DeclRefExpr] detached(priority:operation:)
3246+
# 525| getArgument(0): [Argument] priority: default priority
3247+
# 525| getExpr(): [DefaultArgumentExpr] default priority
3248+
# 525| getArgument(1): [Argument] operation: { ... }
3249+
# 525| getExpr(): [ExplicitClosureExpr] { ... }
3250+
# 525| getBody(): [BraceStmt] { ... }
3251+
# 526| getElement(0): [ForEachStmt] for ... in ... { ... }
3252+
# 526| getPattern(): [NamedPattern] i
3253+
# 526| getSequence(): [BinaryExpr] ... ....(_:_:) ...
3254+
# 526| getFunction(): [MethodLookupExpr] ....(_:_:)
3255+
# 526| getBase(): [TypeExpr] Int.Type
3256+
# 526| getTypeRepr(): [TypeRepr] Int
3257+
# 526| getMethodRef(): [DeclRefExpr] ...(_:_:)
3258+
# 526| getArgument(0): [Argument] : 1
3259+
# 526| getExpr(): [IntegerLiteralExpr] 1
3260+
# 526| getArgument(1): [Argument] : 100
3261+
# 526| getExpr(): [IntegerLiteralExpr] 100
3262+
# 526| getBody(): [BraceStmt] { ... }
3263+
# 527| getElement(0): [CallExpr] call to yield(_:)
3264+
# 527| getFunction(): [MethodLookupExpr] .yield(_:)
3265+
# 527| getBase(): [DeclRefExpr] continuation
3266+
# 527| getMethodRef(): [DeclRefExpr] yield(_:)
3267+
# 527| getArgument(0): [Argument] : i
3268+
# 527| getExpr(): [DeclRefExpr] i
3269+
# 529| getElement(1): [CallExpr] call to finish()
3270+
# 529| getFunction(): [MethodLookupExpr] .finish()
3271+
# 529| getBase(): [DeclRefExpr] continuation
3272+
# 529| getMethodRef(): [DeclRefExpr] finish()
3273+
# 527| getCapture(0): [CapturedDecl] continuation
3274+
# 525| getElement(1): [ReturnStmt] return
3275+
# 523| getPattern(0): [NamedPattern] stream
3276+
# 533| getElement(1): [ForEachStmt] for ... in ... { ... }
3277+
# 533| getPattern(): [NamedPattern] i
3278+
# 533| getSequence(): [DeclRefExpr] stream
3279+
# 533| getBody(): [BraceStmt] { ... }
3280+
# 534| getElement(0): [CallExpr] call to print(_:separator:terminator:)
3281+
# 534| getFunction(): [DeclRefExpr] print(_:separator:terminator:)
3282+
# 534| getArgument(0): [Argument] : [...]
3283+
# 534| getExpr(): [VarargExpansionExpr] [...]
3284+
# 534| getSubExpr(): [ArrayExpr] [...]
3285+
# 534| getElement(0): [DeclRefExpr] i
3286+
# 534| getElement(0).getFullyConverted(): [ErasureExpr] (Any) ...
3287+
# 534| getArgument(1): [Argument] separator: default separator
3288+
# 534| getExpr(): [DefaultArgumentExpr] default separator
3289+
# 534| getArgument(2): [Argument] terminator: default terminator
3290+
# 534| getExpr(): [DefaultArgumentExpr] default terminator
3291+
# 525| [NilLiteralExpr] nil
3292+
# 526| [ConcreteVarDecl] i
3293+
# 526| Type = Int
3294+
# 533| [ConcreteVarDecl] i
3295+
# 533| Type = Int
32123296
declarations.swift:
32133297
# 1| [StructDecl] Foo
32143298
# 2| getMember(0): [PatternBindingDecl] var ... = ...

swift/ql/test/library-tests/ast/cfg.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,19 @@ func testAvailable() -> Int {
518518

519519
return x
520520
}
521+
522+
func testAsyncFor () async {
523+
var stream = AsyncStream(Int.self, bufferingPolicy: .bufferingNewest(5), {
524+
continuation in
525+
Task.detached {
526+
for i in 1...100 {
527+
continuation.yield(i)
528+
}
529+
continuation.finish()
530+
}
531+
})
532+
533+
for try await i in stream {
534+
print(i)
535+
}
536+
}

swift/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5998,3 +5998,170 @@ cfg.swift:
59985998

59995999
# 519| x
60006000
#-----| -> (Int) ...
6001+
6002+
# 522| enter testAsyncFor()
6003+
#-----| -> testAsyncFor()
6004+
6005+
# 522| exit testAsyncFor()
6006+
6007+
# 522| exit testAsyncFor() (normal)
6008+
#-----| -> exit testAsyncFor()
6009+
6010+
# 522| testAsyncFor()
6011+
#-----| -> stream
6012+
6013+
# 523| var ... = ...
6014+
#-----| -> stream
6015+
6016+
# 523| stream
6017+
#-----| match -> AsyncStream<Element>.init(_:bufferingPolicy:_:)
6018+
6019+
# 523| AsyncStream<Element>.init(_:bufferingPolicy:_:)
6020+
#-----| -> AsyncStream<Int>.Type
6021+
6022+
# 523| AsyncStream<Int>.Type
6023+
#-----| -> Int.Type
6024+
6025+
# 523| call to AsyncStream<Element>.init(_:bufferingPolicy:_:)
6026+
#-----| -> var ... = ...
6027+
6028+
# 523| Int.Type
6029+
#-----| -> .self
6030+
6031+
# 523| .self
6032+
#-----| -> .bufferingNewest
6033+
6034+
# 523| AsyncStream<Int>.Continuation.BufferingPolicy.Type
6035+
#-----| -> 5
6036+
6037+
# 523| .bufferingNewest
6038+
#-----| -> AsyncStream<Int>.Continuation.BufferingPolicy.Type
6039+
6040+
# 523| call to ...
6041+
#-----| -> { ... }
6042+
6043+
# 523| 5
6044+
#-----| -> call to ...
6045+
6046+
# 523| enter { ... }
6047+
#-----| -> { ... }
6048+
6049+
# 523| exit { ... }
6050+
6051+
# 523| exit { ... } (normal)
6052+
#-----| -> exit { ... }
6053+
6054+
# 523| { ... }
6055+
#-----| -> continuation
6056+
6057+
# 523| { ... }
6058+
#-----| -> call to AsyncStream<Element>.init(_:bufferingPolicy:_:)
6059+
6060+
# 524| continuation
6061+
#-----| -> .detached(priority:operation:)
6062+
6063+
# 525| Task<(), Never>.Type
6064+
#-----| -> default priority
6065+
6066+
# 525| return
6067+
#-----| return -> exit { ... } (normal)
6068+
6069+
# 525| .detached(priority:operation:)
6070+
#-----| -> Task<(), Never>.Type
6071+
6072+
# 525| call to detached(priority:operation:)
6073+
#-----| -> return
6074+
6075+
# 525| default priority
6076+
#-----| -> { ... }
6077+
6078+
# 525| enter { ... }
6079+
#-----| -> { ... }
6080+
6081+
# 525| exit { ... }
6082+
6083+
# 525| exit { ... } (normal)
6084+
#-----| -> exit { ... }
6085+
6086+
# 525| { ... }
6087+
#-----| -> ....(_:_:)
6088+
6089+
# 525| { ... }
6090+
#-----| -> call to detached(priority:operation:)
6091+
6092+
# 526| for ... in ... { ... }
6093+
#-----| non-empty -> i
6094+
#-----| empty -> .finish()
6095+
6096+
# 526| i
6097+
#-----| match -> .yield(_:)
6098+
6099+
# 526| 1
6100+
#-----| -> 100
6101+
6102+
# 526| ... ....(_:_:) ...
6103+
#-----| -> for ... in ... { ... }
6104+
6105+
# 526| ....(_:_:)
6106+
#-----| -> Int.Type
6107+
6108+
# 526| Int.Type
6109+
#-----| -> 1
6110+
6111+
# 526| 100
6112+
#-----| -> ... ....(_:_:) ...
6113+
6114+
# 527| continuation
6115+
#-----| -> i
6116+
6117+
# 527| .yield(_:)
6118+
#-----| -> continuation
6119+
6120+
# 527| call to yield(_:)
6121+
#-----| -> for ... in ... { ... }
6122+
6123+
# 527| i
6124+
#-----| -> call to yield(_:)
6125+
6126+
# 529| continuation
6127+
#-----| -> call to finish()
6128+
6129+
# 529| .finish()
6130+
#-----| -> continuation
6131+
6132+
# 529| call to finish()
6133+
#-----| -> exit { ... } (normal)
6134+
6135+
# 533| for ... in ... { ... }
6136+
#-----| empty -> exit testAsyncFor() (normal)
6137+
#-----| non-empty -> i
6138+
6139+
# 533| i
6140+
#-----| match -> print(_:separator:terminator:)
6141+
6142+
# 533| stream
6143+
#-----| -> for ... in ... { ... }
6144+
6145+
# 534| print(_:separator:terminator:)
6146+
#-----| -> i
6147+
6148+
# 534| call to print(_:separator:terminator:)
6149+
#-----| -> for ... in ... { ... }
6150+
6151+
# 534| default separator
6152+
#-----| -> default terminator
6153+
6154+
# 534| default terminator
6155+
#-----| -> call to print(_:separator:terminator:)
6156+
6157+
# 534| (Any) ...
6158+
#-----| -> [...]
6159+
6160+
# 534| [...]
6161+
#-----| -> [...]
6162+
6163+
# 534| [...]
6164+
#-----| -> default separator
6165+
6166+
# 534| i
6167+
#-----| -> (Any) ...

swift/ql/test/library-tests/controlflow/graph/cfg.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,19 @@ func testAvailable() -> Int {
518518

519519
return x
520520
}
521+
522+
func testAsyncFor () async {
523+
var stream = AsyncStream(Int.self, bufferingPolicy: .bufferingNewest(5), {
524+
continuation in
525+
Task.detached {
526+
for i in 1...100 {
527+
continuation.yield(i)
528+
}
529+
continuation.finish()
530+
}
531+
})
532+
533+
for try await i in stream {
534+
print(i)
535+
}
536+
}

0 commit comments

Comments
 (0)