Skip to content

Support struct field type remapping via --with-type#751

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-with-type-field-remap-559
Jul 13, 2026
Merged

Support struct field type remapping via --with-type#751
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-with-type-field-remap-559

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Extends --with-type (-wt) to accept a field-qualified key (Struct.field=Type) so the emitted type of an individual struct field can be overridden.

#559 originally proposed --remap-type Something.type=SomethingType, but --remap-type/--remap-field shipped in #736 with a different meaning (name disambiguation). Instead this reuses --with-type, whose semantics are already "use this type for the given declaration," so int Something.type; can be emitted as SomethingType Something.type;.


Implementation reuses the existing WithType helper, which already performs the override and preserves the original Clang type as a [NativeTypeName] attribute — it was previously wired only for enum backing types. Field type emission already flows through the same GetRemappedTypeName(fieldDecl, …) path, so the hook is a FieldDecl sibling branch alongside the existing EnumDecl one.

  • WithType gains an optional matchStar = true parameter; the enum call site is byte-for-byte unchanged.
  • The * catch-all is intentionally not honored for fields — rewriting every struct field to a single type is meaningless, so the qualified Struct.field key must be explicit.
  • Behavior is full-type replacement (the value is the complete desired C# type); the original type is preserved as [NativeTypeName] unless equivalent; unrelated fields are unaffected. This composes with the existing macro/enum-backing/struct-marker uses of --with-type, which use distinct qualified names.

Added a focused root-level regression fixture (WithTypeFieldTest) using ValidateGeneratedCSharpLatestWindowsBindingsAsync, rather than adding to the 16-way matrix, to avoid golden churn. It asserts int typepublic SomethingType type; with [NativeTypeName("int")] and that a sibling int other; field is untouched. No existing goldens change.

Validation: dotnet build -c Release produces 0 warnings; dotnet test -c Release is green (3746 generator + 56 interop/3 skipped + 11 CLI-parser tests).

Fixes #559

Extend `--with-type` to accept a field-qualified key (`Struct.field=Type`) so
the emitted type of an individual struct field can be overridden, reusing the
existing `WithType` machinery. The original Clang type is preserved as a
`[NativeTypeName]` attribute and unrelated fields are unaffected. The `*`
catch-all is intentionally not honored for fields.

Fixes dotnet#559

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding tannergooding merged commit 4d2acea into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding tannergooding deleted the tannergooding-with-type-field-remap-559 branch July 13, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structure field type remapping

1 participant