Skip to content

Unions: Align implementation with recent design changes around case types and default nullability - #83161

Merged
AlekseyTs merged 4 commits into
dotnet:features/Unionsfrom
AlekseyTs:Unions_41
Apr 15, 2026
Merged

Unions: Align implementation with recent design changes around case types and default nullability#83161
AlekseyTs merged 4 commits into
dotnet:features/Unionsfrom
AlekseyTs:Unions_41

Conversation

@AlekseyTs

@AlekseyTs AlekseyTs commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

@AlekseyTs
AlekseyTs marked this pull request as ready for review April 13, 2026 22:51
@AlekseyTs
AlekseyTs requested a review from a team as a code owner April 13, 2026 22:51
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @jjonescz, @dotnet/roslyn-compiler Please review

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @jjonescz, @dotnet/roslyn-compiler Please review

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

I didn't see test coverage for the following scenario mentioned in dotnet/csharplang#10092:

When a union creation member is called (explicitly or through a union conversion), the new union's Value property gets the null state of the incoming value.

It's possible I missed it, or, it is meant to be implemented in a later PR.

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

I didn't see test coverage for the following scenario mentioned in dotnet/csharplang#10092:

When a union creation member is called (explicitly or through a union conversion), the new union's Value property gets the null state of the incoming value.

It's possible I missed it, or, it is meant to be implemented in a later PR.

There is no change in the quoted rule. The diff is somewhat confusing, a new bullet was added before it. The rule was implemented earlier with relevant tests and remains implemented now (this PR didn't make any changes around it).

@AlekseyTs
AlekseyTs requested a review from a team April 15, 2026 03:28
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@jjonescz, @dotnet/roslyn-compiler For a second review

if (NamedTypeSymbol.IsSuitableUnionConstructor(ctor))
{
var parameter = ctor.Parameters[0];
result = result.Join(GetParameterState(parameter.TypeWithAnnotations, parameter.FlowAnalysisAnnotations).State);

@jjonescz jjonescz Apr 15, 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.

Do we have tests where the parameter has nullability annotations via attributes like [NotNull]? #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.

Do we have tests where the parameter has nullability annotations via attributes like [NotNull]?

There is a test - NullableAnalysis_04_State_From_Default_PostCondition.

And there is an open design question whether the spec should be adjusted for cases like this.

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 would expect some test like that to be changed by this PR though since we added this new code path.

@AlekseyTs AlekseyTs Apr 15, 2026

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.

Since we are not paying attention to the attributes (per spec) there is no change in behavior. We didn't look at them and we continue not looking at the attributes now. NullableAnalysis_04_State_From_Default_PostCondition demonstrates that.

// For union types where none of the case types are nullable, the default state for Value is "not null" rather than "maybe null".
var result = NullableFlowState.NotNull;

foreach (var ctor in unionType.InstanceConstructors)

@jjonescz jjonescz Apr 15, 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.

Do we need to handle IUnionMembers factory methods too? #Resolved

@AlekseyTs AlekseyTs Apr 15, 2026

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.

Do we need to handle IUnionMembers factory methods too?

Yes we need to handle them too, but I am not planning to do that in this PR. At the moment support for factory methods is limited to calculation of case types. Other consumption scenarios are not implemented

@AlekseyTs
AlekseyTs requested a review from jjonescz April 15, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants