Unify duplicated bool type transforms into a shared helper#711
Merged
tannergooding merged 1 commit intoJul 13, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
bool/bool*type-transform logic used when emitting P/Invoke-facing signatures was copy-pasted across three sites inPInvokeGenerator.VisitDecl.cs. The plainbool->byteremap, the compat-modebool*->byte*remap, and thenativeTypeNamefallback 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:
Sites unified (all in
PInvokeGenerator.VisitDecl.cs):VisitFieldDeclVisitFunctionDeclvirtual / no-body return handlingThe anonymous-record site previously discarded
nativeTypeNameviaout _; it now captures it so it can pass theref. That field emitsNativeTypeName = nullregardless, so the discarded value stays byte-identical.This is behavior-preserving; generated output is unchanged.
Deferred intentionally: the four
bool-only sites inPInvokeGenerator.TypeResolution.csare left as-is. They only dobool->byteand deliberately omit thebool*transform; two of them builddelegate*signatures, so folding them into the full helper would change fnptr/template codegen. The!= 0write inVisitDeclis 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).