xai-org/x-algorithm · error · SemanticCheckFailure

type cannot defined in root scope: %s

Error message

type cannot defined in root scope: %s

What it means

defineType(ThriftType, alias) rejects type-alias definitions made while only the root scope exists (scopeList.size() <= 1). Types can only be imported/aliased inside nested scopes such as function or module bodies.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/compiler/CompilerContext.java:292

      if (scope.isVariableDefined(variableText)) {
        Parameter parameter = scope.getVariable(variableText);
        return new Variable(variableText, parameter);
      }
    }
    throw new SemanticCheckFailure(String.format("variable %s not defined", variableText));
  }

  TypeContext getTypeContext() throws SemanticCheckFailure {
    if (scopeList.isEmpty()) {
      throw new SemanticCheckFailure("There is no type context");
    }

    return scopeList.getFirst();
  }

  void defineType(ThriftType thriftType, String alias) throws SemanticCheckFailure {
    if (scopeList.size() <= 1) {
      throw new SemanticCheckFailure(
          String.format("type cannot defined in root scope: %s", alias));
    }

    CompilerScope scope = currentScope();
    scope.defineType(thriftType, alias);
  }

  void defineType(ThriftStructType scroogeType, String alias) throws SemanticCheckFailure {
    if (scopeList.size() <= 1) {
      throw new SemanticCheckFailure(
          String.format("type cannot defined in root scope: %s", alias));
    }

    CompilerScope scope = currentScope();
    scope.defineType(scroogeType, alias);
  }

  TestScope addTestScope() {

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Move the import/type-alias inside a scoped construct (function/module body) where it is allowed
  2. Verify the DSL grammar/semantics version for where imports are permitted
  3. If writing a custom transform, emit the import after a scope has been pushed

Example fix

// before
import com.foo.BarType as T  // at root scope
// after
fn f() { import com.foo.BarType as T; ... }
Defensive patterns

Strategy: try-catch

Try / catch

catch (SemanticCheckFailure e) { /* move the import inside a scoped body */ }

Prevention

When it happens

Trigger: An 'import' or 'import as' statement appearing at the root/top level of a compilation unit, where only the root scope is active, triggering toImport/toImportAs to call defineType at root.

Common situations: Moving imports to the top of a file in the wrong construct; DSL changes that relaxed where imports may appear; custom transforms emitting imports outside scoped bodies.

Related errors


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