Skip to content

Stylex Panic: stylesheet rule sort is not a total order #795

Done

Description

@hurrymaplelad

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:

  • assemble can produce a sheet whose order depends on the input order, and
  • slice::sort_by can 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

  1. 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_pairs in testdata/pins/collation.json keep 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, a and y, where ICU and UTF-16 both put the control first).
  2. Use real root collation (ICU4X icu_collator). This matches JS localeCompare at 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_segmenter and icu_properties grows 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 localeCompare follows 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 ö).
  3. 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 ', and B sorts before a. That breaks byte parity unless StyleX itself switches away from localeCompare.

Personally, I like option 3 and would be happy to work with Stylex folks to push for it.

Activity

  1. changed the title [-]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[/+] on Nov 23, 2026
  2. changed the title [-]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[/+] on Nov 23, 2026
  3. changed the title [-]Panic: the stylesheet rule sort is not a total order[/-] [+]Stylex Panic: stylesheet rule sort is not a total order[/+] on Nov 23, 2026
  4. hurrymaplelad commented on Nov 30, 2026

    @hurrymaplelad
    ContributorAuthor

    Closed by #797 - switched to UTF-16 sorts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions