swc-project/swc · error · TypeError

Super expression must either be null or a function

Error message

Super expression must either be null or a function

What it means

The #[proc_macro_derive(Encode, attributes(encoding))] entry point in swc's ast_node crate parses its input with syn::parse::<syn::DeriveInput>(input).expect("failed to parse input as DeriveInput") before delegating to encoding::encode::expand. It is a defensive invariant on an input rustc guarantees to be a valid item, so a panic ('proc-macro panicked' + this message) indicates the token stream reaching the macro was not a parseable derive target.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_inherits.rs:12

// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.

use super::{HelperDef, HelperName};

pub const DEF: HelperDef = HelperDef {
    name: HelperName::inherits,
    local: "_inherits",
    import_path: "@swc/helpers/_/_inherits",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
    }

    subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });

    if (superClass) _set_prototype_of(subClass, superClass);
}
"#,
    #[cfg(feature = "inline-helpers")]
    deps: super::HelperBitmap::from_bits(0x00000000000020000020000000000000),
};

#[cfg(feature = "inline-helpers")]
pub fn stmts() -> &'static [swc_ecma_ast::Stmt] {
    static STMTS: once_cell::sync::Lazy<Vec<swc_ecma_ast::Stmt>> =
        once_cell::sync::Lazy::new(|| super::super::parse(DEF.source, DEF.import_path));
    &STMTS
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Verify the annotated item compiles as plain Rust (a struct or enum with valid syntax) before adding the derive back.
  2. Run cargo expand to inspect what tokens actually reach the derive when the item is macro-produced.
  3. Check the swc/ast_node crate version for known proc-macro panics and upgrade.
  4. File a minimal reproduction upstream if valid input still triggers it.
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Applying #[derive(Encode)] to an item whose token stream is malformed or not an item (macro-generated edge cases), or calling the proc macro function directly with arbitrary tokens. The sibling Decode and Spanned entry points carry the identical guard.

Common situations: Practically unreachable through normal #[derive] usage; seen only with build-time code generation that synthesizes derive calls, custom test harnesses for proc macros, or version/toolchain mismatches that alter token stream shapes.

Related errors


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