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.

C#: count Environment.TickCount and Stopwatch timestamps as clock reads (#1415)

jakalijey97fed0fdc3da
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4f3ae032..2f610b01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -267,6 +267,9 @@
- 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).
+- C#: `Environment.TickCount`, `Environment.TickCount64`, `Stopwatch.GetTimestamp()`,
+ `Stopwatch.StartNew()` and `Stopwatch.GetElapsedTime()` count as reading the clock, also written
+ with `System.Diagnostics.` in full (#1399 by @jakalijey).
- C: `secure_getenv`, `gethostname`, `getlogin`, `getlogin_r`, `getpid`, `getuid` and `uname`
count as reading the machine or the process, as `getenv` and `getcwd` already did, in `analyze`
and `bridge` (#1406 by @jakalijey).
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 644df3d2..27abc33c 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -285,6 +285,11 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"DateTimeOffset.UtcNow",
"Random",
"Guid.NewGuid",
+ // Also `Environment.TickCount64`.
+ "Environment.TickCount",
+ "Stopwatch.GetTimestamp(",
+ "Stopwatch.StartNew(",
+ "Stopwatch.GetElapsedTime(",
],
// Only these static members of `[datetime]` read the clock; `IsLeapYear`, `DaysInMonth`
// and `Parse` compute from their arguments.
@@ -654,11 +659,14 @@ fn find_all<'a>(hay: &'a str, needle: &'a str) -> impl Iterator<Item = usize> +
})
}
+const SYSTEM_NAMESPACES: &[&str] = &["System.IO.", "System.Diagnostics.", "System."];
+
/// Whether `before` ends with a .NET `System` namespace written in full (`System.`,
-/// `System.IO.`, `global::System.`): `System.DateTime.Now` is `DateTime.Now`, while `x.Now` and
-/// `obj.Random` after any other name are members of something else.
+/// `System.IO.`, `System.Diagnostics.`, `global::System.`): `System.DateTime.Now` is
+/// `DateTime.Now`, while `x.Now` and `obj.Random` after any other name are members of something
+/// else.
fn after_system_namespace(before: &str) -> bool {
- ["System.IO.", "System."].iter().any(|ns| {
+ SYSTEM_NAMESPACES.iter().any(|ns| {
before.strip_suffix(ns).is_some_and(|head| {
head.strip_suffix("global::").is_some()
|| !head
@@ -1503,6 +1511,12 @@ mod tests {
)
.is_some()
);
+ for code in [
+ "public static long Uptime() => Environment.TickCount64;",
+ "public static long Stamp() => System.Diagnostics.Stopwatch.GetTimestamp();",
+ ] {
+ assert!(reason(Lang::Csharp, &[], code).is_some(), "{code}");
+ }
// A member of something else that is not the `System` namespace is not the clock.
for code in [
"public static long Ticks() => clock.DateTime.Now.Ticks;",