Skip to content

Unify duplicated bool type transforms into a shared helper#711

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-refactor-unify-bool-transform
Jul 13, 2026
Merged

Unify duplicated bool type transforms into a shared helper#711
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-refactor-unify-bool-transform

Conversation

@tannergooding

Copy link
Copy Markdown
Member

The bool/bool* type-transform logic used when emitting P/Invoke-facing signatures was copy-pasted across three sites in PInvokeGenerator.VisitDecl.cs. The plain bool -> byte remap, the compat-mode bool* -> byte* remap, and the nativeTypeName fallback were each duplicated, so keeping the sites in sync was manual and drift-prone.

This factors that logic into a single private helper and calls it from each site:

private void TransformBoolType(ref string typeName, ref string nativeTypeName)

Sites unified (all in PInvokeGenerator.VisitDecl.cs):

  • VisitFieldDecl
  • VisitFunctionDecl virtual / no-body return handling
  • the anonymous-record field emit path

The anonymous-record site previously discarded nativeTypeName via out _; it now captures it so it can pass the ref. That field emits NativeTypeName = null regardless, so the discarded value stays byte-identical.

This is behavior-preserving; generated output is unchanged.

Deferred intentionally: the four bool-only sites in PInvokeGenerator.TypeResolution.cs are left as-is. They only do bool -> byte and deliberately omit the bool* transform; two of them build delegate* signatures, so folding them into the full helper would change fnptr/template codegen. The != 0 write in VisitDecl is a value expression rather than a type transform, so it is out of scope.

Validation: Release build with 0 warnings / 0 errors, and all 3708 golden-file generator tests pass (byte-identical output).

The identical bool/bool* type-transform logic used when generating fields, anonymous record fields, and virtual/imported function returns was copy-pasted across three sites in PInvokeGenerator.VisitDecl.cs, risking per-site drift.

Factor it into a single private TransformBoolType(ref string typeName, ref string nativeTypeName) helper covering the plain bool and compat-mode bool* forms plus the nativeTypeName fallback, and call it from each site. Behavior-preserving -- generated output is unchanged.

The bool-only sites in PInvokeGenerator.TypeResolution.cs are intentionally left as-is since they don't perform the bool* transform and folding it in could change fnptr/template codegen.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit d2a9f51 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-refactor-unify-bool-transform branch July 13, 2026 02:31
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.

1 participant