Skip to content

Initial support for non-boxing union TryGetValue access pattern#82325

Merged
AlekseyTs merged 2 commits into
dotnet:features/Unionsfrom
AlekseyTs:Unions_12_3
Feb 13, 2026
Merged

Initial support for non-boxing union TryGetValue access pattern#82325
AlekseyTs merged 2 commits into
dotnet:features/Unionsfrom
AlekseyTs:Unions_12_3

Conversation

@AlekseyTs

Copy link
Copy Markdown
Contributor

No description provided.

@AlekseyTs AlekseyTs requested review from a team as code owners February 7, 2026 00:35
@dotnet-policy-service dotnet-policy-service Bot added the Needs API Review Needs to be reviewed by the API review council label Feb 7, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR modifies public API files. Please follow the instructions at https://github.com/dotnet/roslyn/blob/main/docs/contributing/API%20Review%20Process.md for ensuring all public APIs are reviewed before merging.

@RikkiGibson RikkiGibson self-assigned this Feb 7, 2026

@RikkiGibson RikkiGibson 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.

Got thru implementation and some tests. Taking a break and will try to finish later today.

static bool isTryGetValueSignature(MethodSymbol method)
{
// PROTOTYPE: Cover individual conditions with tests
return method is

@RikkiGibson RikkiGibson Feb 9, 2026

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.

Is the TryGetValue method allowed to have type parameters? #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is the TryGetValue method allowed to have type parameters?

Good question. Not according to my reading of the spec. I'll adjust the condition.

if (input.UnionValue is { } unionValue)
{
BoundDagTemp temp = MakeUnionValue(input.DagTemp, unionValue, out BoundDagEvaluation valueEvaluation);
unionValueEvaluation = new Tests.One(valueEvaluation);

@RikkiGibson RikkiGibson Feb 9, 2026

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.

nit: consider both declaring + assigning unionValueEvaluation at this point. #Resolved

Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
Comment thread src/Compilers/CSharp/Test/CSharp15/UnionsTests.cs
Comment thread src/Compilers/CSharp/Test/CSharp15/UnionsTests.cs
@AlekseyTs AlekseyTs requested a review from a team February 11, 2026 17:14
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@333fred, @dotnet/roslyn-compiler For a second review

1 similar comment
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@333fred, @dotnet/roslyn-compiler For a second review

@333fred 333fred 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.

Got through the impl side. Will get to the tests this afternoon.

if (candidate.Parameters[0].Type.Equals(type, TypeCompareKind.AllIgnoreOptions) && // PROTOTYPE: Allow conversions per spec
caseTypes.Contains(method.Parameters[0].Type, Symbols.SymbolEqualityComparer.AllIgnoreOptions)) // PROTOTYPE: Optimize this check?
{
if (method.GetUseSiteInfo().DiagnosticInfo?.DefaultSeverity == DiagnosticSeverity.Error)

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.

I'm a bit conflicted here: I think there's a valid argument that we because TryGetValue is an optimization, we should fall back to just using Value. But I also somewhat feel like we should just proceed and report the error, rather than bailing for some potentially non-obvious reason and silently getting boxing codegen.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit conflicted here: I think there's a valid argument that we because TryGetValue is an optimization, we should fall back to just using Value. But I also somewhat feel like we should just proceed and report the error, rather than bailing for some potentially non-obvious reason and silently getting boxing codegen.

I will capture this in a speclet as an open question

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

Labels

Area-Compilers Feature - Unions Needs API Review Needs to be reviewed by the API review council

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants