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.

Kotlin: keep a function that reads the default locale, as Java does (#1423)

jakalijey08f5689096c7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7007fe32..85297c62 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -287,6 +287,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).
+- Kotlin: a function that calls `Locale.getDefault`, `TimeZone.getDefault`, `ZoneId.systemDefault`
+ or `Charset.defaultCharset` stays in Kotlin with the reason Java gives, and `analyze` flags those
+ calls as state outside the arguments in Java and Kotlin (#1409 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
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 6f7c17fc..d06b0fae 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -176,6 +176,10 @@ fn environment(lang: Lang) -> &'static [&'static str] {
"System.getProperty(",
"System.getProperties(",
"InetAddress.getLocalHost(",
+ "Locale.getDefault(",
+ "TimeZone.getDefault(",
+ "ZoneId.systemDefault(",
+ "Charset.defaultCharset(",
],
Lang::Ruby => &[
"ENV[",
@@ -1729,6 +1733,18 @@ mod tests {
"fun anySuit(): String = listOf(\"hearts\", \"spades\").random()\n",
Risk::ClockAndRandom,
),
+ (
+ Lang::Kotlin,
+ "When.kt",
+ "fun shout(s: String): String = s.uppercase(Locale.getDefault())\n",
+ Risk::SharedState,
+ ),
+ (
+ Lang::Java,
+ "When.java",
+ "class When { static String language() { return Locale.getDefault().getLanguage(); } }",
+ Risk::SharedState,
+ ),
(
Lang::Java,
"Deck.java",
diff --git a/src/lang/java.rs b/src/lang/java.rs
index af6c2a64..fd992f18 100644
--- a/src/lang/java.rs
+++ b/src/lang/java.rs
@@ -716,12 +716,6 @@ fn reads_environment(calls: &[String]) -> Option<String> {
"OffsetDateTime.now",
"Clock.systemUTC",
];
- const LOCALE: [&str; 4] = [
- "Locale.getDefault",
- "TimeZone.getDefault",
- "ZoneId.systemDefault",
- "Charset.defaultCharset",
- ];
let hit = |set: &[&str]| {
calls
.iter()
@@ -731,9 +725,7 @@ fn reads_environment(calls: &[String]) -> Option<String> {
if let Some(call) = hit(&CLOCK) {
return Some(format!("reads the clock (`{call}`)"));
}
- hit(&LOCALE)
- .map(|call| format!("reads the default locale (`{call}`)"))
- .or_else(|| super::reads_singleton(calls))
+ super::reads_default_locale(calls).or_else(|| super::reads_singleton(calls))
}
/// Whether a field declaration is `static` and not `final`.
diff --git a/src/lang/kotlin.rs b/src/lang/kotlin.rs
index da49c7b3..3058f2e4 100644
--- a/src/lang/kotlin.rs
+++ b/src/lang/kotlin.rs
@@ -1121,8 +1121,10 @@ fn collect(
blocker = Some(super::OVERLOADED.into());
}
let calls = calls(function, source);
+ // A port would read the machine it runs on, as in Java (issue #1409).
if blocker.is_none() {
- blocker = super::reads_singleton(&calls);
+ blocker =
+ super::reads_default_locale(&calls).or_else(|| super::reads_singleton(&calls));
}
if blocker.is_none() {
blocker = state.touched_by(function, source, owner_of(container, source).as_deref());
@@ -1488,6 +1490,43 @@ mod tests {
);
}
+ #[test]
+ fn a_function_that_reads_the_default_locale_stays_in_kotlin() {
+ // Issue #1409: the same calls Java keeps, on the same JVM.
+ let source = "import java.time.ZoneId\n\
+ import java.util.Locale\n\
+ \n\
+ fun zone(): String = ZoneId.systemDefault().id\n\
+ \n\
+ fun shout(s: String): String = s.uppercase(Locale.getDefault())\n\
+ \n\
+ fun utf(): String = java.nio.charset.Charset.defaultCharset().name()\n\
+ \n\
+ fun plain(s: String): String = s.uppercase(Locale.ROOT)\n";
+ let fns = exports("src/main/kotlin/When.kt", source);
+ let reason = |name: &str| {
+ fns.iter()
+ .find(|f| f.name == name)
+ .and_then(|f| f.blocker.clone())
+ };
+ assert_eq!(
+ reason("zone").as_deref(),
+ Some("reads the default locale (`ZoneId.systemDefault`)"),
+ "{fns:?}"
+ );
+ assert_eq!(
+ reason("shout").as_deref(),
+ Some("reads the default locale (`Locale.getDefault`)"),
+ "{fns:?}"
+ );
+ assert_eq!(
+ reason("utf").as_deref(),
+ Some("reads the default locale (`java.nio.charset.Charset.defaultCharset`)"),
+ "{fns:?}"
+ );
+ assert_eq!(reason("plain"), None, "a fixed locale is not the machine's");
+ }
+
#[test]
fn a_primitive_array_is_named_with_the_reason_the_wire_gives() {
// int, long, double and boolean element arrays are what the Java wire converts.
diff --git a/src/lang/mod.rs b/src/lang/mod.rs
index 70d18cb7..9bb8948f 100644
--- a/src/lang/mod.rs
+++ b/src/lang/mod.rs
@@ -72,6 +72,32 @@ pub fn is_singleton_call(call: &str) -> bool {
accessor && (class || (owner.is_empty() && name == "getInstance"))
}
+/// The JVM calls that read the machine's default locale, time zone or charset.
+const DEFAULT_LOCALE: [&str; 4] = [
+ "Locale.getDefault",
+ "TimeZone.getDefault",
+ "ZoneId.systemDefault",
+ "Charset.defaultCharset",
+];
+
+/// Why a Java or Kotlin export that reads the machine's default locale, time zone or charset
+/// stays in the original: case folding, number formatting and the first day of the week follow
+/// the machine the port runs on, not the one the original ran on. `calls` are named as in
+/// `ExportedFn::calls`, so `java.util.Locale.getDefault` matches too.
+pub fn reads_default_locale(calls: &[String]) -> Option<String> {
+ calls
+ .iter()
+ .find(|call| {
+ DEFAULT_LOCALE.iter().any(|name| {
+ call.as_str() == *name
+ || call
+ .strip_suffix(name)
+ .is_some_and(|head| head.ends_with('.'))
+ })
+ })
+ .map(|call| format!("reads the default locale (`{call}`)"))
+}
+
/// Why a function that calls a singleton's static accessor stays in the original.
pub fn reads_singleton(calls: &[String]) -> Option<String> {
calls