Skip to content

Generated C# doesn't compile when a class is named System (CS0426 on System.Numerics/Collections), or when a member shares its class's name (CS0542) #232

Description

@matt-edmondson

What's wrong

Two naming cases pass kschema validate cleanly, yet the C# the generator emits for them doesn't compile.

a) Framework types aren't global::-qualified

MapType and the initialisers write System.Numerics.Vector3, System.Collections.Generic.List<…>, System.DateTime and so on (CSharpCodeGenerator.cs:639-651, :722-725). Inside namespace Game;, a schema class named System (a very common ECS name) makes System resolve to Game.System:

"classes": [
  { "name": "System",    "members": [ { "name": "Priority", "type": { "TypeName": "Int" } } ] },
  { "name": "Transform", "members": [
      { "name": "Position", "type": { "TypeName": "Vector3" } },
      { "name": "Tags", "type": { "TypeName": "Array", "elementType": { "TypeName": "String" }, "container": "vector" } } ] }
],
"codeGenerators": [ { "name": "CS", "language": "csharp", "namespace": "Game", "outputPath": "out" } ]

This fails with CS0426: The type name 'Numerics' does not exist in the type 'System', and the same for Collections. By the same mechanism, a configured namespace such as Acme.System, or a class named ktsu, breaks the ktsu.Schema.Runtime.* references.

b) Member named after its enclosing type

GenerateClass never compares a member's name with the class name. Class Label with member Label: String emits public string Label { get; set; } inside public class Label, which fails with CS0542: member names cannot be the same as their enclosing type. The same applies to a semantic type named Value (the generator adds a Value property to it) and to an interface function named after its interface.

Why it matters

These are ordinary names. The schema validates cleanly, and the failure only appears when the consumer builds, pointing at generated code.

Suggested fix / acceptance criteria

  • Prefix every framework and runtime type the C# generator emits with global::, e.g. global::System.Numerics.Vector3 and global::ktsu.Schema.Runtime.….
  • For (b), there is no C# escape, so report the collision in Validate() (an Error, or a C#-generator-specific check before generation) with a message naming the class and member.
  • Add tests that generate and compile: a schema with a class named System that uses vector and list members, a namespace ending in .System, and a member/class name clash that is refused with a diagnostic.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions