Skip to content

A class whose base is declared later in the file fails on load in Python (NameError), JavaScript (ReferenceError), C++ and C (unknown type) #498

Open

Description

@matt-edmondson

What's wrong

LanguageGeneratorBase.GenerateFileMembers writes a file's members in document order (Coder/Languages/LanguageGeneratorBase.cs:743-758):

protected virtual void GenerateFileMembers(SourceFile file, CodeBlocker code)
{
    ...
    foreach (AstNode member in file.Members)
    {
        ...
        GenerateInternal(member, code);
    }
}

C#, Rust and Go don't care what order types are declared in, so a document that is valid for them can put a derived type before its base. Python, JavaScript, C++ and C do care. Their generators write the declarations exactly as the document lists them, and none of them reorders or forward-declares.

Failure scenario

var file = new SourceFile("shapes");
var square = new ClassDeclaration("Square") { BaseType = "Shape" };
square.Members.Add(new FieldDeclaration("side", "int"));
var shape = new ClassDeclaration("Shape");
shape.Members.Add(new FieldDeclaration("id", "int"));
file.Members.Add(square);
file.Members.Add(shape);
Target Output Result
Python class Square(Shape): before class Shape: NameError: name 'Shape' is not defined on import
JavaScript class Square extends Shape { before class Shape { ReferenceError: Cannot access 'Shape' before initialization on load
C++ class Square : public Shape before class Shape error: expected class-name before '{' token
C Shape base; inside Square before typedef struct Shape error: unknown type name 'Shape'
C#, Rust, Go same order compile

All four failures were reproduced against the built Coder.dll at f15652d, using python3, node 22, g++ -std=c++20 and gcc -std=c11.

Why it matters

The AST is meant to be language-agnostic. The project already treats a Python import-time NameError from a base list as a generator bug: #64 covers a base that names the class being declared. A base declared later in the file is the same failure and the more common one. Neither the editor nor the API warns about it, so a document that generates clean C#, Rust and Go produces Python and JavaScript that crash on load.

(Open design question for maintainers: for C++20 designated initialisers, the generators deliberately leave ordering to the caller (CFamilyGenerator.cs:254). If declaration order is meant to be the document author's responsibility too, the alternative fix is to validate it and report a diagnostic instead of emitting code that fails on load.)

Suggested fix

Before writing a file's or namespace's members, order the type declarations so that each type comes after its BaseType and Interfaces when those are declared in the same scope. C and C++ also need any type held by value as a field to come first. Use a stable topological sort, so documents already in a valid order come out unchanged, and leave cycles in document order. The sort could be a protected helper used by the Python, JavaScript, C and C++ generators, or it could go in GenerateFileMembers and the namespace walk for every target. Add a test that loads the Python and JavaScript output, as PythonGeneratedSourceImportsTests already does, and one that compiles the C/C++ output.

Activity

  1. added
    bug
    on Nov 12, 2026
  2. matt-edmondson commented on Nov 13, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. A document that is valid for C#, Rust and Go produces Python and JavaScript that crash on load, and C and C++ that fail to compile. It only happens when a derived type comes before its base in the same file, and the workaround (reorder the document) is easy.
    • Area: LanguageGeneratorBase.GenerateFileMembers and the namespace member walk, which affect the Python, JavaScript, C and C++ generators
    • Suggested assignment: The Coder generator maintainer. A design decision is needed first, between a stable topological sort of type declarations and a validation diagnostic, as the issue notes. The sort is the friendlier option and keeps already-valid documents unchanged.
    • Duplicates / related: No duplicate found. It is related to Python writes a base list naming the class being declared, which raises NameError on import #64 (a base list that names the class being declared, Python NameError) and to A constructor initialiser naming the base class only reaches the base constructor in C++ and JavaScript; Python, C#, Go, Rust and C treat it as a field #169 (base constructor initialisers). The C/C++ by-value field ordering point overlaps with the existing designated-initialiser ordering stance in CFamilyGenerator.cs:254.
    • In progress: No open PR matches.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions