swc-project/swc · error

failed to parse number as char

Error message

failed to parse number as char

What it means

In the JSX lexer's from_code helper (used by read_jsx_entity), the digits of a numeric JSX character reference (&#NNNN;) parsed as a number but char::from_u32 rejected the resulting code point — i.e. it falls in the surrogate range (0xD800-0xDFFF) or exceeds 0x10FFFF, so it is not a valid Unicode scalar. It fires while lexing a JSX entity containing such an invalid numeric escape.

Source

Thrown at crates/swc_ecma_lexer/src/common/lexer/mod.rs:998

                first = false;
                c.is_ident_start()
            } else {
                c.is_ident_part() || c == '-'
            }
        });

        Ok(Self::Token::jsx_name(slice, self))
    }

    fn read_jsx_entity(&mut self) -> LexResult<(char, String)> {
        debug_assert!(self.syntax().jsx());

        fn from_code(s: &str, radix: u32) -> LexResult<char> {
            // TODO(kdy1): unwrap -> Err
            let c = char::from_u32(
                u32::from_str_radix(s, radix).expect("failed to parse string as number"),
            )
            .expect("failed to parse number as char");

            Ok(c)
        }

        fn is_hex(s: &str) -> bool {
            s.chars().all(|c| c.is_ascii_hexdigit())
        }

        fn is_dec(s: &str) -> bool {
            s.chars().all(|c| c.is_ascii_digit())
        }

        let mut s = CompactString::default();

        debug_assert!(self.input().cur().is_some_and(|c| c == b'&'));
        self.bump();

        let start_pos = self.input().cur_pos();

View on GitHub (pinned to 5176682b65)

Solutions

  1. Use a valid scalar code point in the numeric entity (avoid surrogate ranges and values above 0x10FFFF)
  2. Escape the desired character as a named entity instead of a numeric one
  3. Sanitize unpaired-surrogate escapes in upstream tooling that generates JSX
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_ecma_lexer/src/common/lexer/mod.rs:998 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


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