Skip to content

Diagnose or hoist NativePtr.stackalloc where localloc is invalid IL - #20491

Merged
T-Gro merged 36 commits into
mainfrom
fix/issue-20295
Sep 21, 2026
Merged

T-Gro merged 36 commits into
mainfrom
fix/issue-20295

Conversation

@T-Gro

@T-Gro T-Gro commented Sep 8, 2026

Copy link
Copy Markdown
Member

Fixes #20295

  • NativePtr.stackalloc inside the with handler, filter or finally block of a try (directly or via an inlinable lambda) now reports error FS3916 instead of emitting a localloc that the runtime rejects with InvalidProgramException at method load.
  • NativePtr.stackalloc passed as a chained base-constructor argument no longer produces an assembly that throws InvalidProgramException; the base-constructor arguments are hoisted into locals before the uninitialized this is pushed.

Copilot and others added 7 commits September 8, 2026 11:41
NativePtr.stackalloc emits the 'localloc' IL instruction, which the JIT rejects inside an exception-handling region, causing InvalidProgramException at method load. Detect this during PostInferenceChecks and emit compile-time error FS3916 when stackalloc is applied inside a 'with' handler or 'finally' block. A withinHandler env flag is set on the handler/finally bodies and reset at closure and method boundaries so stackalloc in a lambda or object-expression method defined in a handler stays legal. Part of issue #20295 (Case 1).

Co-authored-by: Copilot <[email protected]>
…FS3916)

An immediately-applied lambda in a 'try' handler is inlined into the handler's IL region by the optimizer, so its 'localloc' still lands inside the exception-handling region and throws InvalidProgramException under --optimize+. The CheckLambdas 'withinHandler = false' reset was based on the false premise that every lambda becomes a separate method; remove it so lambda bodies inside a handler are conservatively checked. Genuine method boundaries (object-expression/interface methods) keep their reset via CheckMethod and remain legal (verified by compileExeAndRun). Part of issue #20295 (Case 1).

Co-authored-by: Copilot <[email protected]>
… Case 2)

NativePtr.stackalloc used as a chained base-constructor argument emitted 'localloc' while an uninitialized 'this' was pending on the evaluation stack, which the JIT rejects (InvalidProgramException at method load). The uninitialized 'this' cannot be spilled to a local, so the existing spill mechanism could not clear the stack.

When a base/self-ctor argument may emit 'localloc', evaluate the arguments into locals first (at a clean stack), then push 'this' and reload them. Left-to-right evaluation order is preserved and ordinary base ctors emit byte-identical IL. The fix is applied in both GenApp and GenILCall.

Co-authored-by: Copilot <[email protected]>
Adds a Fixed release note for the Case-2 base-constructor argument hoist. Verified xlf/surface-area baselines are in sync and formatting is clean; ran full regression (NativeInterop 17/17, Byref 138/138, Language RegressionTests 29/29, SurfaceAreaTest green).

Co-authored-by: Copilot <[email protected]>
…20295 Case 1)

The syntactic PostInferenceChecks detection ran before inlining and closure
conversion, so it could not tell an escaping closure (whose 'localloc' lands in
its own method and is legal) from an inlined/immediately-applied lambda or a
'let inline' wrapper (whose 'localloc' lands in the handler region and is
illegal). This produced both a false positive (rejecting legal escaping
closures) and a false negative ('let inline' wrapper compiled and threw
InvalidProgramException at load).

Detect 'localloc' emission inside a catch/filter/finally/fault region at codegen
instead, via a new eenv.withinExnHandler flag set on the handler bodies in
GenTryWith/GenTryFinally and reset at method/closure boundaries. By that point
inlining and closure conversion have run, so the true exception-handling region
is known: escaping closures stay legal, inlined localloc is rejected. The try
body keeps 'localloc' legal per ECMA-335. Removes the now-unused withinHandler
plumbing and the nativeptr_stackalloc_vref intrinsic.

Co-authored-by: Copilot <[email protected]>
…20295)

The prior commit's test edit stripped the [<Fact>] let header, orphaning the
try-body assertion inside the escaping-closure test and breaking the
ComponentTests build with FS0020. Restore it as its own Fact.

Co-authored-by: Copilot <[email protected]>
@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ No release notes required

@github-actions github-actions Bot added the AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed label Sep 8, 2026
@T-Gro T-Gro added AI-Auto-Resolve-CI Opt-in: LabelOps triages CI failures on this PR every 3h AI-Auto-Resolve-Conflicts Opt-in: LabelOps merges main into this PR and resolves conflicts every 3h labels Sep 9, 2026
@T-Gro
T-Gro requested a review from abonie September 9, 2026 13:04
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Commit pushed: f0587a9

Generated by LabelOps — PR Maintenance · gpt56 2.5M

@github-actions

This comment has been minimized.

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 🕵️ LGTM

@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Sep 10, 2026
Copilot and others added 2 commits September 11, 2026 17:21
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 457e0e99-9880-4692-aed5-6ebcf92c9e43
…20342)

* Order explicit type args by constraint dependencies before unifying

A generic value/method with a subtype constraint that references a later
type parameter (Register<'a,'b when 'a :> I<'b>>) failed with FS0001 when
the argument implements the interface at several instantiations. The
explicit type arguments are now unified in dependency order so a parameter
referenced by another's constraint is solved first.

Fixes #20103

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 8ce30fb9-238f-4bb2-8f31-71e8d173c2ca

* Address review feedback

Add equal-arity competing overloads with explicit selection assertions and explicit/inferred type arguments. Verify dependency ordering and rollback through red-green and a failed-candidate undo mutation.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 137c3de0-8937-4b62-aff3-13ce91c4cdcb

---------

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8ce30fb9-238f-4bb2-8f31-71e8d173c2ca
Copilot-Session: 137c3de0-8937-4b62-aff3-13ce91c4cdcb
@github-actions

Copy link
Copy Markdown
Contributor

Commit pushed: da86fec

Generated by LabelOps — PR Maintenance · gpt56 3.9M

@github-actions

This comment has been minimized.

Comment thread src/Compiler/CodeGen/IlxGen.fs Resolved
Copilot and others added 2 commits September 16, 2026 15:06
* Remove always-on ErrorReportingOnStaticClasses flag

Co-authored-by: Copilot <[email protected]>

* Add release notes for ErrorReportingOnStaticClasses flag removal

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9f40324e-435c-473a-990c-f6d12e4d2884

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9f40324e-435c-473a-990c-f6d12e4d2884
@github-actions

Copy link
Copy Markdown
Contributor

Commit pushed: e728f78

Generated by LabelOps — PR Maintenance · gpt56 1.4M

@github-actions

This comment has been minimized.

Comment thread src/Compiler/Optimize/Optimizer.fs Resolved
@github-actions

This comment has been minimized.

Resolve the FS3923 diagnostic collision by assigning FS3924 to the NativePtr.stackalloc handler diagnostic and updating its tests and release note.

Co-authored-by: Copilot <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🤖 LabelOps — conflict resolution.

Merged current origin/main into the PR branch and resolved src/Compiler/FSComp.txt by retaining main’s FS3923 named-arguments diagnostic and assigning FS3924 to the PR’s NativePtr.stackalloc handler diagnostic. Updated the matching IlxGen documentation, NativeInterop expectations, and release note.

Validated the net11.0 Release component-test build and all 39 Libraries.NativeInterop tests. Commit: 2517f74d2.

<!-- labelops:result:9ea54b7810f6f0cf8f32c94d16ce39a1d2a411b3:0bde38cdbdcfc878cbbeff4ba38323a42f7ab500:conflicts:fix-submitted -->

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

Generated by LabelOps — PR Maintenance · gpt56 2.8M · ◷

Copilot and others added 3 commits September 21, 2026 11:11
Prevent immediate-consumption let elimination from moving frame-local allocations into exception handlers. Reuse the existing allocation detector, cover local functions and delegates in handler, filter, and finally contexts with optimization on and off, and update the release note.

Co-authored-by: Copilot <[email protected]>
@T-Gro
T-Gro requested a review from abonie September 21, 2026 12:17
@T-Gro
T-Gro enabled auto-merge (squash) September 21, 2026 12:17

@abonie abonie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖🕵️ AI review — verify independently.

Comment thread src/Compiler/Optimize/Optimizer.fs Resolved
Comment thread src/Compiler/Utilities/illib.fs Resolved
abonie and others added 3 commits September 21, 2026 15:58
Preserve handler-local callable boundaries behind leading bindings and rank ready generic constraints by subtype ambiguity.

Co-authored-by: Copilot App <[email protected]>
Resolve optimizer and NativeInterop test conflicts while preserving the remaining review fixes.

Co-authored-by: Copilot App <[email protected]>
@T-Gro
T-Gro requested a review from abonie September 21, 2026 14:04
abonie and others added 3 commits September 21, 2026 16:05
Adopt the review-feedback implementation already pushed to the PR branch.

Co-authored-by: Copilot App <[email protected]>

@abonie abonie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖🕵️ AI review — verify independently.

Comment thread src/Compiler/Optimize/Optimizer.fs Resolved
abonie and others added 3 commits September 21, 2026 16:47
Inspect two-target match branches before eliminating handler-local callable bindings, matching the computed-function application reduction shapes.

Co-authored-by: Copilot App <[email protected]>
Preserve the validated NativePtr.stackalloc fixes and computed helper review fix while incorporating upstream metadata trimming behavior.

Co-authored-by: Copilot <[email protected]>
Inspect extractable F# delegate bodies before eliminating handler-local callable bindings, and consolidate callable-shape coverage into the existing handler matrix.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-Auto-Resolve-CI Opt-in: LabelOps triages CI failures on this PR every 3h AI-Auto-Resolve-Conflicts Opt-in: LabelOps merges main into this PR and resolves conflicts every 3h AI-reviewed PR reviewed by AI review council AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Diagnostic for NativePtr.stackalloc in positions where localloc can never be valid IL

2 participants