Description
Summary
default_locale_cmp, and the keyed sort in assemble that mirrors it, collate a pair only when both strings stay inside the pinned collation alphabet. Any other pair falls back to UTF-16 code-unit order. Mixing the two orders isn't transitive, so when a sheet holds rules like the ones below:
assemblecan produce a sheet whose order depends on the input order, andslice::sort_bycan panic with "user-provided comparison function does not correctly implement a total order".
Repro
On main (5cbe2c5) with the pinned toolchain 1.99.1. Three rules at the same priority:
.xa::after{content:''}<.xb::after{content:""}by collation ('before")""<"Checking…"by UTF-16, because U+2026 is outside the alphabet"Checking…"<''by UTF-16
That's a cycle. Assembling the three rules in each of their 6 input orders gives 3 different sheets (input [a, c, b] gives c, a, b). 60 rules built the same way ("a<i>", "B<i>", "C↗<i>"), shuffled, panic in assemble at core/src/slice/sort/shared/smallsort.rs:854.
Tests that reproduce both, in crates/oj_stylex/tests/assembly.rs:
#[test]
fn rules_mixing_verified_and_unverified_text_sort_in_one_order() {
use oj_stylex::assemble::{assemble_with_comparator, default_locale_cmp};
let cfg = AssembleConfig::default();
let rules = [
plain_rule("xa", ".xa::after{content:''}", 8000.0),
plain_rule("xb", ".xb::after{content:\"\"}", 8000.0),
plain_rule("xc", ".xc::before{content:\"Checking\u{2026}\"}", 8000.0),
];
let mut sheets = std::collections::BTreeSet::new();
for order in [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]] {
let input: Vec<StylexRule> = order.iter().map(|&i| rules[i].clone()).collect();
let sheet = assemble(&input, &cfg).unwrap();
assert_eq!(Ok(sheet.clone()), assemble_with_comparator(&input, &cfg, default_locale_cmp));
sheets.insert(sheet);
}
assert_eq!(sheets.len(), 1, "the sheet depends on input order: {sheets:#?}");
}
#[test]
fn many_mixed_collation_rules_sort_without_panicking() {
let values = ["a", "B", "C\u{2197}"];
let mut rules: Vec<StylexRule> = (0..60)
.map(|i| {
let value = values[i % values.len()];
plain_rule(&format!("{value}{i}"), &format!(".x{i}{{content:\"{value}{i}\"}}"), 3000.0)
})
.collect();
let mut rng = Lcg(0x5eed_0f00_d00d_0001);
for _ in 0..64 {
for i in (1..rules.len()).rev() {
rules.swap(i, rng.below(i + 1));
}
std::panic::catch_unwind(|| assemble(&rules, &AssembleConfig::default()))
.expect("the sort panicked")
.unwrap();
}
}Options for a fix
- Extend the order consistently (draft StyleX: stylesheet assembly sorts rules in one order when text holds a character outside the collation table #773). A char outside the alphabet gets a primary weight by code point inside the same three-level comparison: C0 controls (U+0000–U+001F) before the alphabet, every other char after it.
- Strings made only of alphabet chars compare exactly as before.
- All 52
unverified_pairsintestdata/pins/collation.jsonkeep their order. - Outside those pairs the order isn't UTF-16's; DEL, for example, sorts after
é. - A simpler rule that puts every unverified char after the alphabet flips 18 of the 52 pins (C0 controls against space,
aandy, where ICU and UTF-16 both put the control first).
- Use real root collation (ICU4X
icu_collator). This matches JSlocaleCompareat the root locale for all text, not just the alphabet. StyleX's own Rust compiler takes this route.- Size cost, measured: a stripped, fat-LTO release binary that already links
icu_normalizer,icu_segmenterandicu_propertiesgrows from 382 KB to 1.56 MB (about +1.2 MB), with 2 more crates. Expect more if those ICU crates aren't already in the tree. - Locale: bare
localeComparefollows the host's default locale, so root collation is the deterministic choice, but it can still differ from a JS build on a tailored locale (for example Swedishö).
- Size cost, measured: a stripped, fat-LTO release binary that already links
- Use UTF-16 code-unit order everywhere. (my preference, StyleX: use UTF-16 code-unit order for compiler sorts #797) This is the simplest and fastest option, and total by construction. But it changes sheet order against JS StyleX for ordinary text:
"sorts before', andBsorts beforea. That breaks byte parity unless StyleX itself switches away fromlocaleCompare.
Personally, I like option 3 and would be happy to work with Stylex folks to push for it.
Activity
[-]oj_stylex: the stylesheet rule sort is not a total order when rule text mixes characters outside the collation alphabet[/-][+]Panic: the stylesheet rule sort is not a total order when rule text mixes characters outside the collation alphabet[/+][-]Panic: the stylesheet rule sort is not a total order when rule text mixes characters outside the collation alphabet[/-][+]Panic: the stylesheet rule sort is not a total order[/+][-]Panic: the stylesheet rule sort is not a total order[/-][+]Stylex Panic: stylesheet rule sort is not a total order[/+]StyleX: use UTF-16 code-unit order for compiler sorts
StyleX: use UTF-16 code-unit order for compiler sorts (#797)
hurrymaplelad commentedon Nov 30, 2026
Closed by #797 - switched to UTF-16 sorts.