xai-org/x-algorithm · error · SemanticCheckFailure

variable %s already exists in the same scope

Error message

variable %s already exists in the same scope

What it means

CompilerScope.defineVariable rejects binding a variable whose name already exists in the same scope, preventing shadowing-within-a-scope and ambiguous binder names.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/compiler/CompilerScope.java:81

            ThriftType.EQUALITY_CHECK
    );
    checkAndMergeMap(
            this.thriftStructTypes,
            that.thriftStructTypes,
            "thrift struct type",
            ThriftStructType.EQUALITY_CHECK
    );
    checkAndMergeMap(
            this.compilerUnits,
            that.compilerUnits,
            "compiler unit",
            CompilerUnit.EQUALITY_CHECK
    );
  }

  void defineVariable(Parameter parameter) throws SemanticCheckFailure {
    if (variables.containsKey(parameter.getParameterName())) {
      throw new SemanticCheckFailure(
          String.format("variable %s already exists in the same scope",
              parameter.getParameterName()));
    }
    variables.put(parameter.getParameterName(), parameter);
  }

  boolean isVariableDefined(String variableText) {
    return variables.containsKey(variableText);
  }

  Parameter getVariable(String variableText) {
    return variables.get(variableText);
  }

  void defineType(ThriftType thriftType, String alias) {
    thriftTypes.put(alias, thriftType);
    for (CompilerUnit c : CompilerUnit.of(alias, thriftType)) {
      compilerUnits.put(c.funcName(), c);

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Rename the inner binder to a unique name
  2. Rename the colliding parameter or outer binder
  3. In generated code, mangle binder names to guarantee uniqueness

Example fix

// before
forEach(xs, forEach(xs, xs))
// after
forEach(xs, forEach(ys, ys))
Defensive patterns

Strategy: validation

Validate before calling

if (scope.isVariableDefined(binderName)) throw new IllegalArgumentException("binder already defined: " + binderName);

Try / catch

catch (SemanticCheckFailure e) { /* rename the duplicate binder */ }

Prevention

When it happens

Trigger: A lambda/binder construct (toAny, toFlatMap, toForEach, toFilter, toFoldl, toFoldl1) binding a variable name that is already defined in the current scope — e.g. nested 'forEach(x, ... forEach(x, ...))' in the same scope, or a binder colliding with a parameter.

Common situations: Nested comprehensions reusing the same binder name; renaming a binder to collide with an existing parameter; generated code that reuses binder names.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/e5d8019249fd763e. Report an issue: GitHub.