Skip to content

Convert nullary union cases to static property reads in LINQ trees - #21007

Open
pcshrosbree wants to merge 2 commits into
dotnet:mainfrom
pcshrosbree:fix/linq-nullary-union-case-property
Open

pcshrosbree wants to merge 2 commits into
dotnet:mainfrom
pcshrosbree:fix/linq-nullary-union-case-property

Conversation

@pcshrosbree

Copy link
Copy Markdown
Contributor

Description

LeafExpressionConverter now converts a nullary union case such as Kind.Handle to a read of its static property, Expression.Property(null, Handle), which is what C# produces for the same member. Until now it produced Expression.Call(null, get_Handle), which prints as Kind.get_Handle(). Code that looks for static members as MemberExpression, or prints a tree back as C#, saw a call to an accessor instead, and C# does not accept an explicit accessor call (CS0571). The compiler emits the static property for nullary cases of reference, struct and generic unions and for []. The converted tree evaluates to the same value as before. I found this while testing LINQ providers.

Two kinds of case keep the call:

  • A nullary case of a union marked with UseNullAsTrueValue, such as None. F# LINQ providers recognise None by matching get_None() (SQLProvider's OptionNone pattern, for example), and its value is null either way.
  • A case whose getter has no corresponding static property, which [<DefaultAugmentation(false)>] can cause.

The new tests cover the property reads and both kinds of case that keep the call.

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes (not a performance change)
  • Release notes entry updated

Note

Prepared with AI assistance (Anthropic Claude, OpenAI Codex). I reviewed it before posting.

…nConverter

`LeafExpressionConverter` converted every union case construction to a call of the method that `FSharpValue.PreComputeUnionConstructorInfo` returns. For a nullary case with a static property, that method is the property's getter, so `Kind.Handle` became `Expression.Call(get_Handle)` and printed as `Kind.get_Handle()`. C# produces `Expression.Property(null, Handle)` for the same member, which is the shape LINQ providers and tree printers expect for a static property. Use the property when the getter belongs to one. The compiler emits that property for nullary cases of reference, struct and generic unions, and for `[]`.

Nullary cases of unions marked with `UseNullAsTrueValue`, including `None`, keep the call: F# LINQ providers recognise `None` by matching `get_None()`, and its value is null either way. A case whose getter has no corresponding static property keeps the call too; `[<DefaultAugmentation(false)>]` can cause that.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Change path Release notes path Description
`src/FSharp.Core` docs/release-notes/.FSharp.Core/11.0.200.md

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

1 participant