Skip to content

Declare AOT compatibility and fix Native AOT crash in enum fallback#378

Merged
shibayan merged 2 commits into
masterfrom
feature/aot-compatibility
Jul 5, 2026
Merged

Declare AOT compatibility and fix Native AOT crash in enum fallback#378
shibayan merged 2 commits into
masterfrom
feature/aot-compatibility

Conversation

@shibayan

@shibayan shibayan commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Summary

Makes Sharprompt trim- and Native AOT-compatible, and fixes a real Native AOT crash found during verification.

  • <IsAotCompatible>true</IsAotCompatible> on the library — enables the trim/AOT analyzers at build time and marks the NuGet package as AOT-compatible for consumers.
  • Native AOT crash fix: FieldInfo.MetadataToken throws InvalidOperationException on Native AOT, crashing the enum reflection fallback (Prompt.Select<TEnum> for unregistered enums). The fallback now sorts by the underlying constant value instead — deterministic on every runtime and matching Enum.GetValues semantics.
  • IL2090 suppressed in the enum fallback with justification: the trimmer always preserves fields of enum types it keeps (verified empirically with TrimMode=full).
  • IL2026/IL2077 suppressed for TypeDescriptor.GetConverter in TypeHelper<T>: intrinsic converters cover the types typically used with prompts (string, numerics, bool, Guid, DateTime, enums); custom TypeConverter attributes on user-defined types remain the application's responsibility to preserve.

Fixes #248

Verification

Scenario Result
dotnet build with AOT analyzers 0 IL warnings
PublishTrimmed + TrimMode=full (win-x64) 0 IL warnings; generated path + enum fallback + TypeDescriptor conversions all work at runtime
PublishAot (win-x64, ILC 8.0.28) 0 IL warnings; native binary runs, generated path + enum fallback verified (previously crashed with InvalidOperationException on MetadataToken)
Unit tests 325 passed

🤖 Generated with Claude Code

Mark the library IsAotCompatible and resolve all trim/AOT analyzer
warnings:

- FieldInfo.MetadataToken is unavailable on Native AOT and crashed the
  enum reflection fallback with InvalidOperationException. Sort by the
  underlying constant value instead, which is deterministic on every
  runtime and matches Enum.GetValues semantics.
- Suppress IL2090 in the enum fallback: the trimmer always preserves
  fields of enum types it keeps (verified with TrimMode=full).
- Suppress IL2026/IL2077 for TypeDescriptor.GetConverter: intrinsic
  converters cover the types typically used with prompts; custom
  TypeConverter attributes on user types remain the app's responsibility.

Verified with PublishTrimmed (TrimMode=full) and PublishAot on win-x64:
zero IL warnings, and both the source-generated metadata path and the
reflection fallback work at runtime.

Fixes #248

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 5, 2026 12:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Sharprompt to explicitly declare Native AOT compatibility and addresses a Native AOT runtime crash in the enum reflection fallback path used by Prompt.Select<TEnum>/Prompt.MultiSelect<TEnum> when enum metadata isn’t pre-registered.

Changes:

  • Declares the library as AOT-compatible via <IsAotCompatible>true</IsAotCompatible>.
  • Adds targeted trimming suppressions around TypeDescriptor.GetConverter usage in TypeHelper<T>.
  • Reworks enum fallback metadata creation to avoid FieldInfo.MetadataToken (unsupported on Native AOT) by sorting based on enum values.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Sharprompt/Sharprompt.csproj Marks the package as AOT-compatible to enable analyzers and communicate support to consumers.
src/Sharprompt/Internal/TypeHelper.cs Suppresses trimming warnings for TypeDescriptor converter usage with documented justification.
src/Sharprompt/EnumMetadataRegistry.cs Updates enum fallback ordering to avoid Native AOT MetadataToken usage and remain deterministic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Sharprompt/EnumMetadataRegistry.cs Outdated
Sort by ordinal field name before deduplicating aliased values so the
representative does not depend on GetFields ordering, and always map
values through the representative's field name because Enum.ToString
picks an implementation-defined alias.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shibayan shibayan self-assigned this Jul 5, 2026
@shibayan shibayan added the enhancement New feature or request label Jul 5, 2026
@shibayan shibayan merged commit fbf9b71 into master Jul 5, 2026
5 checks passed
@shibayan shibayan deleted the feature/aot-compatibility branch July 5, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Native AOT

2 participants