JetBrains/intellij-community · error · IncorrectOperationException

Can't bind to non-labeled statement

Error message

Can't bind to non-labeled statement

What it means

PsiLabelReference is the reference from a break/continue statement to its label. bindToElement re-points a reference at a target element, but a label reference can only be bound to a PsiLabeledStatement; anything else throws IncorrectOperationException("Can't bind to non-labeled statement").

Source

Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/PsiLabelReference.java:58

      }
    }
    return null;
  }

  @Override
  public @NotNull String getCanonicalText() {
    return getElement().getText();
  }

  @Override
  public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
    myIdentifier = (PsiIdentifier)PsiImplUtil.setName(myIdentifier, newElementName);
    return myIdentifier;
  }

  @Override
  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    if (!(element instanceof PsiLabeledStatement)) throw new IncorrectOperationException("Can't bind to non-labeled statement");
    return handleElementRename(((PsiLabeledStatement)element).getName());
  }

  @Override
  public boolean isReferenceTo(@NotNull PsiElement element) {
    return resolve() == element;
  }

  @Override
  public boolean isSoft() {
    return false;
  }
}

View on GitHub (pinned to be881553f2)

Solutions

  1. Check the element type first: only call bindToElement when element instanceof PsiLabeledStatement.
  2. If you want to change the label text, call handleElementRename(newLabelName) instead, which renames the identifier.
  3. For generic reference processing, prefer checking reference.isReferenceTo(target) or use the ResolveResult target kinds before binding.

Example fix

// before
labelRef.bindToElement(target); // target is e.g. PsiMethod -> throws

// after
if (target instanceof PsiLabeledStatement) {
  labelRef.bindToElement(target);
} else {
  labelRef.handleElementRename(target.getName()); // just rename label text
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(target instanceof PsiLabeledStatement)) return; // cannot bind label ref

Type guard

static boolean canBindLabelRef(PsiElement e) { return e instanceof PsiLabeledStatement; }

Try / catch

try { labelRef.bindToElement(t); } catch (IncorrectOperationException e) { /* fall back to handleElementRename */ }

Prevention

When it happens

Trigger: Calling bindToElement on the reference returned by PsiBreakStatement.getLabelReference() (or continue) with an element that is not a PsiLabeledStatement — e.g. a PsiMethod, PsiClass, or PsiField resolved from elsewhere.

Common situations: Refactoring plugins that rebind references after moving targets; 'fix reference' intentions that resolve to the wrong element; automated migration tools that call bindToElement uniformly on all references.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/c5be5075d32607a2. Report an issue: GitHub.