swc-project/swc · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

The legacy rgb()/hsl() compat pass converts hue angles to plain numbers, accepting deg/grad/rad/turn after lowercasing the unit. Any other dimension unit in the hue position (px, %, pt, or an unknown unit) reaches unreachable!() — the parser accepted the dimension but the lowering has no conversion for it.

Source

Thrown at crates/swc_css_compat/src/compiler/legacy_rgb_and_hsl.rs:56

            } else if is_hsl {
                function.value = function
                    .value
                    .drain(..)
                    .map(|n| {
                        if let Some(Angle {
                            span,
                            value: Number { value, .. },
                            unit,
                            ..
                        }) = n.as_hue().and_then(|hue| hue.as_angle())
                        {
                            let value = match &*unit.value.to_ascii_lowercase() {
                                "deg" => *value,
                                "grad" => value * 180.0 / 200.0,
                                "rad" => value * 180.0 / PI,
                                "turn" => value * 360.0,
                                _ => {
                                    unreachable!();
                                }
                            };

                            ComponentValue::Number(Box::new(Number {
                                span: *span,
                                value: value.round(),
                                raw: None,
                            }))
                        } else {
                            n
                        }
                    })
                    .collect();
            }

            if is_rgb || is_hsl {
                if let Some(alpha_value) = function
                    .value

View on GitHub (pinned to 5176682b65)

Solutions

  1. Fix the hue value: use a unitless number or one of deg/grad/rad/turn (e.g. `hsl(180deg, 50%, 50%)`)
  2. Pre-validate color functions (scan hsl(/hsla( hue units) before running compat passes
  3. Report the exact value to swc so the pass can emit a proper parse error instead of panicking

Example fix

/* before */
color: hsl(10px, 50%, 50%);

/* after */
color: hsl(10deg, 50%, 50%);
Defensive patterns

Strategy: validation

Validate before calling

// Reject legacy hsl()/hsla() whose hue has a non-angle unit
let bad_hue = regex::Regex::new(
    r#"(?i)hsla?\(\s*[+-]?[\d.]+(px|em|rem|ex|ch|vw|vh|vmin|vmax|cm|mm|in|pt|pc|q|fr|d|%)"#,
).unwrap();
if bad_hue.is_match(&css) {
    return Err("hsl() hue must be unitless or deg/grad/rad/turn".into());
}

Try / catch

match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| compile_css(&css))) {
    Ok(v) => v,
    Err(p) if panic_message(&p).contains("unreachable") => {
        // fallback: strip compat lowering for colors (raise target) and recompile
    }
    Err(p) => std::panic::resume_unwind(p),
}

Prevention

When it happens

Trigger: Compiling CSS through swc_css_compat's legacy rgb/hsl lowering when a legacy hsl() hue component has a unit other than deg/grad/rad/turn — e.g. `hsl(10px, 50%, 50%)`, `hsl(2rem, ...)`, or a typo like `hsl(360d ...)`.

Common situations: Malformed hand-written CSS; CSS generated by tools that emit unusual units in the hue slot; values copied from design tools; fuzzing corpora.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/51dd82827923e88d. Report an issue: GitHub.