Skip to content

Unions: add most basic IDE support#82604

Merged
jcouv merged 16 commits into
dotnet:features/Unionsfrom
jcouv:unions-ide2
Mar 3, 2026
Merged

Unions: add most basic IDE support#82604
jcouv merged 16 commits into
dotnet:features/Unionsfrom
jcouv:unions-ide2

Conversation

@jcouv

@jcouv jcouv commented Mar 3, 2026

Copy link
Copy Markdown
Member

This is forked from #82526
Given that we're trying to merge minimal support for preview this week, but there's still discussion on the desired behavior, I'm splitting off the smaller part of the PR. This most basic approach is smaller and safer for merging this week. We can expand support after initial preview.

Opened #82607 to track some follow-ups that were discussed and mostly handled in the original PR

@jcouv jcouv self-assigned this Mar 3, 2026

@CyrusNajmabadi CyrusNajmabadi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good. And it makes sense to pull out the easy/safe stuff from teh other PR. Can you make sure we have tracking bugs though? Thanks!

case DeclaredSymbolInfoKind.Property:
return NavigateToItemKind.Property;
case DeclaredSymbolInfoKind.Struct:
// PROTOTYPE consider having a separate NavigateToItemKind category for unions

@CyrusNajmabadi CyrusNajmabadi Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you track this with an issue at least. We do need this, or the glyph will be inconsistent in these different VS experiences, which we try very hard to avoid. Thanks! :) #Pending

context.IsGlobalStatementContext ||
context.IsTypeDeclarationContext(
validModifiers: s_validModifiers,
validTypeDeclarations: SyntaxKindSet.NonEnumTypeDeclarations,

@CyrusNajmabadi CyrusNajmabadi Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same question from before. is NonEnumTypeDecls right here? (just want to make sure it was loooked at. #Resolved

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.

I think that's correct. This is the same validTypeDeclarations as we use for StructKeywordRecommender. It means that we won't recommend union inside an enum.
The only thing that's a bit weird is that NonEnumTypeDeclarations includes extension blocks. This means that we're currently recommending struct and union inside extension blocks, even though that will end up erroring. That said, my understanding is that we'd rather recommend a bit too much than a bit too little. So seems fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yup. This is totally fine. Thanks for checking/confirming.

<PropertyGroup Condition="'$(DotNetBuildSourceOnly)' != 'true'">
<DefineConstants>$(DefineConstants),ROSLYN_4_12_OR_LOWER</DefineConstants>
<!-- Projects built with OLDER_ROSLYN do not have access to the latest Roslyn APIs -->
<DefineConstants>$(DefineConstants),OLDER_ROSLYN</DefineConstants>

@CyrusNajmabadi CyrusNajmabadi Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice. #Resolved


/// <summary>
/// When adding a new value, also bump <see cref="AbstractSyntaxIndex{TIndex}.s_serializationFormatChecksum"/>.
/// </summary>

@CyrusNajmabadi CyrusNajmabadi Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tnx #Resolved

SyntaxKind.RecordDeclaration => ClassificationTypeNames.RecordClassName,
SyntaxKind.RecordStructDeclaration => ClassificationTypeNames.RecordStructName,
SyntaxKind.StructDeclaration => ClassificationTypeNames.StructName,
// PROTOTYPE consider using a separate classification type for unions so users can color them differently

@CyrusNajmabadi CyrusNajmabadi Mar 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we have an issue on this. we def want this. note: this part can likely easily be part of this PR, it doesn't hit the compiler side. but it can also def come later. #Pending

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.

I created a follow-up issue: #82607
Will update these PROTOTYPE markers to reference the issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perfect. Thanks.

@jcouv jcouv marked this pull request as ready for review March 3, 2026 09:04
@jcouv jcouv requested review from a team as code owners March 3, 2026 09:04
@AlekseyTs

Copy link
Copy Markdown
Contributor

Since #82526 is already under review. I suggest that we bring that PR to a mergeable state and continue working in its context rather than asking everybody to review from scratch.

@AlekseyTs

AlekseyTs commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

(displaying as structs)

BTW, I would like to point out again, that classes can be unions as well. #Resolved

@AlekseyTs

AlekseyTs commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

I suggest that we bring that PR to a mergeable state and continue working in its context rather than asking everybody to review from scratch.

Specifically, I suggest revering controversial changes rather than trying to reach concensus. #ByDesign

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

BTW, I would like to point out again, that classes can be unions as well.

It would definitely be good to have an API from teh compiler (if not already available) so we can determine if something is a union, and then present things that the lang/compiler thinks are unions in a consistent fashion.

Is it just a 'boolean' "yes/no this is a union"? Or is there additional information that is needed to expose?


[Fact]
public Task TestInCompilationUnit_LangVer14()
=> VerifyKeywordAsync("$$", CSharp14ParseOptions);

@JoeRobich JoeRobich Mar 3, 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.

Why would union be suggested for C# 14? #Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We still recommemd, so that you can type the nifty thing you just saw a tweet about. Then we offer the fixer to upgrade your project version to use it.

@jcouv jcouv changed the title Unions: add basic IDE support (displaying as structs) Unions: add most basic IDE support Mar 3, 2026
@jcouv

jcouv commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

I suggest that we bring that PR to a mergeable state and continue working in its context rather than asking everybody to review from scratch.

You can see the commit/SHA before merge (entitled "Comments") is the same commit/SHA as the original PR (ee738d9)

BTW, I would like to point out again, that classes can be unions as well.

Thanks, I'll add some tests for that

@jcouv jcouv requested a review from JoeRobich March 3, 2026 21:49

@AlekseyTs AlekseyTs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM (commit 16)

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.

4 participants