Skip to content

Commit b41ec37

Browse files
committed
Swift: Remove the code related to constructor capture (and the related TODO). This cannot happen in Swift.
1 parent 65e13aa commit b41ec37

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,30 @@ private module CaptureInput implements VariableCapture::InputSig {
942942
}
943943

944944
class Callable extends S::Callable {
945-
predicate isConstructor() { this instanceof S::Initializer }
945+
predicate isConstructor() {
946+
// A class declaration cannot capture a variable in Swift. Consider this hypothetical example:
947+
// ```
948+
// protocol Interface { }
949+
// func foo() -> Interface {
950+
// let y = 42
951+
// class Impl : Interface {
952+
// let x : Int
953+
// init() {
954+
// x = y
955+
// }
956+
// }
957+
// let object = Impl()
958+
// return object
959+
// }
960+
// ```
961+
// The Swift compiler will reject this with an error message such as
962+
// ```
963+
// error: class declaration cannot close over value 'y' defined in outer scope
964+
// x = y
965+
// ^
966+
// ```
967+
none()
968+
}
946969
}
947970
}
948971

@@ -964,8 +987,6 @@ private CaptureFlow::ClosureNode asClosureNode(Node n) {
964987
or
965988
result.(CaptureFlow::ThisParameterNode).getCallable() = n.(ClosureSelfParameterNode).getClosure()
966989
or
967-
result.(CaptureFlow::MallocNode).getClosureExpr() = n.getCfgNode().getNode().asAstNode() // TODO: figure out why the java version had PostUpdateNode logic here
968-
or
969990
exists(CaptureInput::VariableWrite write |
970991
result.(CaptureFlow::VariableWriteSourceNode).getVariableWrite() = write and
971992
n.asExpr() = write.getSource()

0 commit comments

Comments
 (0)