swc-project/swc · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

The @custom-media compat pass rewrites custom media queries. When a media condition 'without or' collapses to empty, it unwraps its single MediaInParens child and re-wraps it; the guard fires when the preceding is-match says the first element is a MediaInParens but popping yields something else — an internal invariant break in the transform rather than a user error.

Source

Thrown at crates/swc_css_compat/src/compiler/custom_media.rs:321

                                            ),
                                        }));
                                }
                            }
                            MediaConditionType::WithoutOr(media_condition) => {
                                let mut media_condition = self
                                    .media_condition_without_or_to_media_condition(media_condition);

                                if new_media_condition.conditions.is_empty() {
                                    let media_in_parens = if matches!(
                                        media_condition.conditions.first(),
                                        Some(MediaConditionAllType::MediaInParens(_))
                                    ) {
                                        match media_condition.conditions.pop() {
                                            Some(MediaConditionAllType::MediaInParens(inner)) => {
                                                inner
                                            }
                                            _ => {
                                                unreachable!();
                                            }
                                        }
                                    } else {
                                        MediaInParens::MediaCondition(media_condition)
                                    };

                                    new_media_condition.conditions.push(
                                        MediaConditionAllType::MediaInParens(media_in_parens),
                                    );
                                } else {
                                    new_media_condition
                                        .conditions
                                        .push(MediaConditionAllType::Or(MediaOr {
                                            span: DUMMY_SP,
                                            keyword: None,
                                            condition: MediaInParens::MediaCondition(
                                                media_condition,
                                            ),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Expand the custom media manually: replace `@custom-media --x (min-width: 600px)` + `@media (--x)` with `@media (min-width: 600px)` and rebuild
  2. Disable the custom-media compat feature (raise the browser target) so the pass does not run
  3. Update swc_css_compat to the latest release and, if reproducible, attach the failing query to a swc issue

Example fix

/* before */
@custom-media --wide (min-width: 600px);
@media (--wide) { .x { color: red } }

/* after */
@media (min-width: 600px) { .x { color: red } }
Defensive patterns

Strategy: fallback

Validate before calling

// Detect @custom-media rules before running compat passes
fn uses_custom_media(css: &str) -> bool {
    css.contains("@custom-media")
}

if uses_custom_media(&css) {
    // expand or strip them before swc_css_compat
}

Try / catch

let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    compile_css(&css, compat_target)
}));
match out {
    Ok(v) => v,
    Err(p) if panic_message(&p).contains("unreachable") => {
        // fallback: retry with the custom-media compat feature disabled / target raised
    }
    Err(p) => std::panic::resume_unwind(p),
}

Prevention

When it happens

Trigger: Running swc_css_compat's custom-media lowering (browser-compat presets) on stylesheets whose `@custom-media` definitions reduce to a single condition — e.g. `@custom-media --x (min-width: 600px)` used as `@media (--x)` — particularly with negations or nested condition combinations.

Common situations: Rust pipelines mimicking postcss-preset-env with @custom-media at-rules; design-token breakpoint systems defined via custom media; upgrades of swc_css_* changing which branch executes.

Related errors


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