swc-project/swc · error · swc_ecma_parser::error::Error

Label {label} is already declared

Error message

Label {label} is already declared

What it means

Parser error from parse_labelled_stmt: the new label's symbol matches one already present in parser.state().labels, i.e. the same label name is declared again in the current label scope (duplicate or shadowing label within nested labelled statements).

Source

Thrown at crates/swc_ecma_parser/src/parser/stmt.rs:868

        expect!(self, Token::RParen);

        // We *may* eat semicolon.
        let _ = self.eat_general_semi();

        let span = self.span(start);

        Ok(DoWhileStmt { span, test, body }.into())
    }

    fn parse_labelled_stmt(&mut self, l: Ident) -> PResult<Stmt> {
        self.do_inside_of_context(Context::IsBreakAllowed, |p| {
            p.do_outside_of_context(Context::AllowUsingDecl, |p| {
                let start = l.span.lo();

                let mut errors = Vec::new();
                for lb in &p.state().labels {
                    if l.sym == *lb {
                        errors.push(Error::new(
                            l.span,
                            SyntaxError::DuplicateLabel(l.sym.clone()),
                        ));
                    }
                }
                p.state_mut().labels.push(l.sym.clone());

                let body = Box::new(if p.input().is(Token::Function) {
                    let f = p.parse_fn_decl(Vec::new())?;
                    if let Decl::Fn(FnDecl { function, .. }) = &f {
                        if p.ctx().contains(Context::Strict) {
                            p.emit_err(function.span, SyntaxError::LabelledFunctionInStrict)
                        }
                        if function.is_generator || function.is_async {
                            p.emit_err(function.span, SyntaxError::LabelledGeneratorOrAsync)
                        }
                    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Use a distinct label name
  2. Drop the duplicate labelled statement
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_ecma_parser/src/parser/stmt.rs:868 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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