dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.08k stars 4.04k forks source link

[New API] IsIterator field on DeclarationModifiers type #75388

Open CoolCoderSuper opened 1 month ago

CoolCoderSuper commented 1 month ago

Background and Motivation

For quick reference the file can be found here The purpose of this api is to solve this issue in the make member static analyzer.

C# does not have the explicit Iterator api and when using the SyntaxGenerator the modifier is dropped. The type already has features that are language specific (ie IsUnsafe) so I would see no harm in supporting this as well.

This would bring the option for generators to create iterator methods closer to reality (not necessarily important).

Proposed API

namespace Microsoft.CodeAnalysis.Editing
{
     public readonly record struct DeclarationModifiers
     {
+           public bool IsIterator => (_modifiers & Modifiers.Iterator ) != 0;
     }

This would also include the corresponding WithIterator() api.

Usage Examples

Alternative Designs

Not having the API and having language specific code in the analyzer.

Risks

Possible confusion since it only exists in VB.

CyrusNajmabadi commented 1 month ago

Why does this need to be public? As opposed to just fixing GetModifiers to include this data internally to ensure that WithModifiers doesn't remove the modifier?

CoolCoderSuper commented 1 month ago

I suppose it wouldn't have to be but this would allow new generation to have the modifier applied. But maybe that doesn't matter since you would still have to deal with yield return vs Yield.

CoolCoderSuper commented 1 month ago

@CyrusNajmabadi just so I understand, your proposal would be the same just internal rather than public?