Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Emit3/Attributes/AttributeTests_Synthesized.cs (C#)

Code before:

            var type = compilation.GlobalNamespace.GetMember<NamedTypeSymbol>("Test");
            Assert.Equal(new[] { "F", ".ctor" }, type.GetMembers().SelectAsArray(m => m.Name));

            var asyncMethod = type.GetMember<MethodSymbol>("F");

            if (optimizationLevel == OptimizationLevel.Debug)
            {
                Assert.Equal("DebuggerStepThroughAttribute", asyncMethod.GetAttributes().Single().AttributeClass.Name);
            }
            else
            {
                Assert.Empty(asyncMethod.GetAttributes());
            }
        }

        [Theory]
        [MemberData(nameof(OptimizationLevelTheoryData))]
        public void AsyncStateMachineAttribute_RuntimeAsync(OptimizationLevel optimizationLevel)
        {
            string source = """
                using System.Threading.Tasks;

                class Test
                {
                    public static async Task F()
                    {
                        await Task.Delay(0);
                    }
                }
                """;

            var options = TestOptions.CreateTestOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel)
                .WithMetadataImportOptions(MetadataImportOptions.All);

            var compilation = CreateRuntimeAsyncCompilation(source, options);
            var verifier = CompileAndVerify(compilation, verify: Verification.Skipped, symbolValidator: verify);
            verifier.VerifyDiagnostics();

            void verify(ModuleSymbol module)
            {
                var type = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test");
                var asyncMethod = type.GetMember<MethodSymbol>("F");

                var attributes = asyncMethod.GetAttributes();

                // When runtime async is enabled, no state machine is generated,
                // so there should be no AsyncStateMachineAttribute and no DebuggerStepThroughAttribute
                Assert.Empty(attributes);

                // Verify no state machine type was generated

Review comment (suggestion):
>public static async Task F() [](http://example.com/codeflow?start=20&length=28)

Could lambdas or local functions be runtime-async? Let's test them too, including scenarios when they are used inside an extension block member. #Closed

Code after:

            var type = compilation.GlobalNamespace.GetMember<NamedTypeSymbol>("Test");
            Assert.Equal(new[] { "F", ".ctor" }, type.GetMembers().SelectAsArray(m => m.Name));

            var asyncMethod = type.GetMember<MethodSymbol>("F");

            if (optimizationLevel == OptimizationLevel.Debug)
            {
                Assert.Equal("DebuggerStepThroughAttribute", asyncMethod.GetAttributes().Single().AttributeClass.Name);
            }
            else
            {
                Assert.Empty(asyncMethod.GetAttributes());
            }
        }

        [Theory]
        [MemberData(nameof(OptimizationLevelTheoryData))]
        public void AsyncStateMachineAttribute_RuntimeAsync(OptimizationLevel optimizationLevel)
        {
            string source = """
                using System.Threading.Tasks;

                class Test
                {
                    public static async Task F()
                    {
                        await Task.Delay(0);
                    }
                }
                """;

            var options = TestOptions.CreateTestOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel);

            var compilation = CreateRuntimeAsyncCompilation(source, options);
            var verifier = CompileAndVerify(compilation, verify: Verification.Skipped, symbolValidator: static module =>
            {
                var type = module.GlobalNamespace.GetMember<NamedTypeSymbol>("Test");
                var asyncMethod = type.GetMember<MethodSymbol>("F");

                // When runtime async is enabled, no state machine is generated,
                // so there should be no AsyncStateMachineAttribute and no DebuggerStepThroughAttribute
                Assert.Empty(asyncMethod.GetAttributes());

                // Verify no state machine types were generated
                Assert.Empty(type.GetTypeMembers());
            });
            verifier.VerifyDiagnostics();
        }

        [Theory]