Repository navigation
Description
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
matt-edmondson commentedon Nov 13, 2026
Triage
LanguageGeneratorBase.GenerateFileMembersand the namespace member walk, which affect the Python, JavaScript, C and C++ generatorsNameError) 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 inCFamilyGenerator.cs:254.Generated by Claude Code