-
Notifications
You must be signed in to change notification settings - Fork 832
Fix IL2091 trimming warning in FSharp.Core LazyExtensions.Create #19114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: T-Gro <[email protected]>
…ments Co-authored-by: T-Gro <[email protected]>
Co-authored-by: T-Gro <[email protected]>
|
@copilot System.Exception : Eval or Execution has failed (expected to succeed): System.Exception: List is ["Before sum"; "Before try"; "Inside finally"; "Before try"; "Inside finally"; |
Co-authored-by: T-Gro <[email protected]>
…er investigation Co-authored-by: T-Gro <[email protected]>
... I've reverted the changes in 378bde0. The manual replacement of |
T-Gro
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the IL2091 trimming warning in FSharp.Core for LazyExtensions.Create method.
CRITICAL: Follow the exact CI build sequence to ensure the test project uses the freshly built FSharp.Core.
====================================================================================
STEP 1 - ADD REPRODUCTION TEST CASES
Location: tests/AheadOfTime/Trimming/Program.fs
Add the following two test cases from issue #17355 at the end of the file (before the main entry point):
let testLazySimple () =
let f23 () = let z = lazy (12345) in z.Force()
check "lazy_simple" 12345 (f23())
let testSeqWithTryFinally () =
let result =
seq {
try
yield 1
yield 2
with
| ex -> ()
} |> Seq.toList
check "seq_try_with" [1; 2] result
Then add calls to these test functions in the main entry point at the end of Program.fs where other tests are called.
====================================================================================
STEP 2 - REPRODUCE THE WARNING FOLLOWING CI SEQUENCE
Follow the exact sequence from azure-pipelines-PR.yml (Build_And_Test_AOT_Windows job):
On Windows:
.\Build.cmd -pack -c Release
.\tests\AheadOfTime\check.ps1
On Linux/Mac:
./Build.sh -pack -c Release
./tests/AheadOfTime/check.ps1
The check.ps1 script will automatically run:
- Equality\check.ps1
- Trimming\check.ps1
VERIFY the IL2091 warning appears in the build output:
"IL2091: Microsoft.FSharp.Control.LazyExtensions.Create(FSharpFunc2<Unit,!!0>): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'System.Lazy1'."
====================================================================================
STEP 3 - APPLY THE FIX
Location: src/FSharp.Core/prim-types.fs (around line 7159)
Method: LazyExtensions.Create<'T>
Find the LazyExtensions.Create method definition and add the DynamicallyAccessedMembers attribute to the generic type parameter 'T':
Change from:
static member Create<'T>(creator: unit -> 'T) = ...
Change to:
static member Create<[<DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)>] 'T>(creator: unit -> 'T) = ...
Note: The DynamicallyAccessedMembersAttribute is already defined in the file for older .NET versions (search for "type internal DynamicallyAccessedMembersAttribute").
====================================================================================
STEP 4 - VERIFICATION FOLLOWING CI SEQUENCE
After applying the fix, rebuild and test using the same CI sequence:
On Windows:
.\Build.cmd -pack -c Release
.\tests\AheadOfTime\check.ps1
On Linux/Mac:
./Build.sh -pack -c Release
./tests/AheadOfTime/check.ps1
VERIFY:
- The IL2091 warning is no longer present in the build output
- All tests pass successfully including testLazySimple() and testSeqWithTryFinally()
- No new trimming warnings are introduced
- Build logs are available in: ./artifacts/log/Release/AheadOfTime/Trimming/
Test Projects (automatically built by check.ps1):
- tests/AheadOfTime/Trimming/SelfContained_Trimming_Test/SelfContained_Trimming_Test.fsproj
- tests/AheadOfTime/Trimming/StaticLinkedFSharpCore_Trimming_Test/StaticLinkedFSharpCore_Trimming_Test.fsproj
- tests/AheadOfTime/Trimming/FSharpMetadataResource_Trimming_Test/FSharpMetadataResource_Trimming_Test.fsproj
All have:
- PublishTrimmed=true
- TreatWarningsAsErrors=true
- Target net9.0
====================================================================================
CONTEXT
The warning occurs because System.Lazy requires DynamicallyAccessedMemberTypes.PublicParameterlessConstructor on its generic type parameter. When F#'s LazyExtensions.Create (used by the 'lazy' keyword) creates a Lazy, the trimmer cannot guarantee that the parameterless constructor won't be trimmed away unless the same annotation is propagated.
The fix annotates the generic parameter 'T' to declare it requires a public parameterless constructor, satisfying the trimmer's flow analysis.
Reference issue: #17355
====================================================================================
ADDITIONAL NOTES
- The CI pipeline (azure-pipelines-PR.yml) already has a Build_And_Test_AOT_Windows job that runs these exact steps
- The build sequence ensures the test projects use the freshly built compiler and FSharp.Core package
- The Build.cmd -pack step creates NuGet packages in artifacts/packages/Release/Shipping/
- The test projects reference these packages via eng/Versions.props and FSharpCorePreviewPackageVersionValue
…fix IL2091 trimming warning Co-authored-by: T-Gro <[email protected]>
Implemented in 2ba6ed9. Added |
❗ Release notes required
Warning No PR link found in some release notes, please consider adding it.
|
…se AzDo ##vso integration Co-authored-by: T-Gro <[email protected]>
|
@copilot : Looks good, please add release notes to https://github.com/dotnet/fsharp/tree/main/docs/release-notes/.FSharp.Core/10.0.200.md |
Co-authored-by: T-Gro <[email protected]>
Added release notes in 3740fd7. Created |
Description
Fix IL2091 trimming warning in FSharp.Core for
LazyExtensions.Createmethod by addingDynamicallyAccessedMembersattribute to the generic type parameter.The IL2091 warning occurs because
System.Lazy<'T>has a trimming constraint requiringPublicParameterlessConstructoron its type parameter. By adding theDynamicallyAccessedMembersattribute to the generic type parameter'Tin the type augmentation, we inform the trimmer that the constraint is satisfied.Changes Made
[<DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)>]attribute to the type parameter'Tin the type augmentation:type Lazy<[<DynamicallyAccessedMembers(...)>]'T> withtestLazySimpleandtestSeqWithTryFinally)##vso[task.logissue]integration to report errors to the correct caller lineTest Instructions
./build.sh --pack -c Release(Linux) or.\Build.cmd -pack -c Release(Windows)./tests/AheadOfTime/check.ps1Fixes #17356
Original prompt
This pull request was created as a result of the following prompt from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.