Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

Swift: count arc4random, CFAbsoluteTimeGetCurrent and DispatchTime.now as clock and randomness (#1421)

jakalijey955644df1d27
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c68f9c31..7007fe32 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -287,6 +287,10 @@
- PowerShell: a parameter typed `[decimal]`, `[uint64]` or `[ulong]` keeps its function in
PowerShell, with the reasons C# gives for the same types, instead of bridging it as `f64` or
`i64`. `[uint32]`, `[double]` and `[single]` bridge as before (#1341 by @jakalijey).
+- Swift: `arc4random`, `arc4random_uniform`, `arc4random_buf`, `drand48`,
+ `CFAbsoluteTimeGetCurrent`, `DispatchTime.now` and `mach_absolute_time` count as clock and
+ randomness reads in `analyze` and `bridge`, as `Date()` and `.random` already did
+ (#1400 by @jakalijey).
- C: `clock_gettime`, `gettimeofday` and `timespec_get` count as clock reads in `analyze` and
`bridge`, as `time` and `clock` already did (#1319 by @jakalijey).
- `analyze` no longer calls a Python, Go, Java or other non-JS unit "JS-shaped" when every export
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 46e020b0..6f7c17fc 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -351,6 +351,13 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"UUID()",
".randomElement(",
".shuffled(",
+ "arc4random(",
+ "arc4random_uniform(",
+ "arc4random_buf(",
+ "drand48(",
+ "CFAbsoluteTimeGetCurrent(",
+ "DispatchTime.now(",
+ "mach_absolute_time(",
],
Lang::C => &[
"time(",
@@ -1577,6 +1584,14 @@ mod tests {
] {
assert_eq!(reason(Lang::Csharp, &[], code), None, "{code}");
}
+ assert!(
+ reason(
+ Lang::Swift,
+ &[],
+ "public func ticks() -> UInt64 {\n return DispatchTime.now().uptimeNanoseconds\n}\n"
+ )
+ .is_some()
+ );
// C's finer clocks (#1319).
for code in [
"long now_ns(void) {\n struct timespec ts;\n clock_gettime(CLOCK_MONOTONIC, &ts);\n return ts.tv_sec;\n}\n",
@@ -1678,6 +1693,18 @@ mod tests {
"let suits = [\"hearts\", \"spades\"]\npublic func shuffledSuits() -> [String] {\n return suits.shuffled()\n}\n",
Risk::ClockAndRandom,
),
+ (
+ Lang::Swift,
+ "Dice.swift",
+ "public func roll() -> Int {\n return Int(arc4random_uniform(6)) + 1\n}\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Swift,
+ "Stamp.swift",
+ "public func stamp() -> Double {\n return CFAbsoluteTimeGetCurrent()\n}\n",
+ Risk::ClockAndRandom,
+ ),
(
Lang::Ruby,
"deck.rb",