Skip to content

Rust: a specialisation writes its nested types twice, the second copy inside impl Trait for Type, so the output fails to compile (E0449) #370

Open

Description

@matt-edmondson

What's wrong

RustGenerator.GenerateClassDeclaration first writes every nested type (ClassDeclaration, EnumDeclaration, UsingAlias) beside the type, at Coder/Languages/RustGenerator.cs:505-509. For a specialisation it then calls GenerateSpecialisation (:610-623), which runs WriteMembers(classDecl.Members, code) over all members. WriteMembers (StandardLanguageGenerator.cs:1006) does no filtering, so every nested type is written a second time, this time inside impl Trait for Type { … }.

  • A UsingAlias inside a specialisation is the C++ template<> struct Describe<RigidBody> { using Field = float; }; idiom. It comes out as a module-level pub type Field = f32; and then a second pub type Field = f32; inside the impl. rustc rejects the second with E0449 "visibility qualifiers are not permitted here". Two specialisations that alias the same name also collide at module scope (E0428).
  • A nested struct is written a second time inside the impl, which rustc rejects with "struct is not supported in traits or impls".

I reproduced both compiler errors with rustc on hand-written output of the same shape.

Repro

var describe = new ClassDeclaration("Describe") { SpecialisationArguments = { new TypeReference("RigidBody") } };
describe.Members.Add(new UsingAlias("Field", "float"));
new RustGenerator().Generate(describe);
// pub type Field = f32;
// impl Describe for RigidBody {
//     pub type Field = f32;   // E0449
// }

Suggested fix / acceptance criteria

  • Make GenerateSpecialisation skip the members that IsTypeDeclaration already hoisted, for example with WriteMembers(classDecl.Members.Where(m => !IsTypeDeclaration(m)), code). The alternative is to write a UsingAlias inside a specialisation once, as an associated type (type Field = f32;, with no pub), and not hoist it. The first option is the smaller change.
  • Add a case to RustGeneratedSourceCompilesTests that compiles a specialisation holding a UsingAlias, and another that compiles a specialisation holding a nested class.

Activity

  1. added
    bug
    on Oct 30, 2026
  2. matt-edmondson commented on Oct 30, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    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