diff --git a/src/Compilers/CSharp/Portable/Lowering/MethodToClassRewriter.cs b/src/Compilers/CSharp/Portable/Lowering/MethodToClassRewriter.cs index 1d63a89667b675ba4edaeaa239e9d822ba492e63..33a12582db9fd609ea0ccfa04c0a87d4bb547610 100644 --- a/src/Compilers/CSharp/Portable/Lowering/MethodToClassRewriter.cs +++ b/src/Compilers/CSharp/Portable/Lowering/MethodToClassRewriter.cs @@ -721,6 +721,15 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati AddSynthesizedAttribute(ref attributes, this.DeclaringCompilation.TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor)); } + + internal override TypeSymbol IteratorElementType + { + get + { + // BaseMethodWrapperSymbol should not be rewritten by the IteratorRewriter + return null; + } + } } } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/IteratorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/IteratorTests.cs index b6ab1da315d1df655b51f50767b7b906231531a7..4a65838e4ede09e29a189c47c27314d3b27766cd 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/IteratorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/IteratorTests.cs @@ -339,5 +339,73 @@ public void TopLevelYieldBreak() Assert.NotNull(yieldNode); Assert.Equal(SyntaxKind.YieldBreakStatement, yieldNode.Kind()); } + + [Fact] + [WorkItem(11649, "https://github.com/dotnet/roslyn/issues/11649")] + public void IteratorRewriterShouldNotRewriteBaseMethodWrapperSymbol() + { + var text = +@"using System.Collections.Generic; + +class Base +{ + protected virtual IEnumerable M() + { + yield break; } + + class D : Base + { + protected override IEnumerable M() + { + base.M(); // the rewriting of D.M() synthesizes a BaseMethodWrapperSymbol for this base call, with return type IEnumerable, + // but it should not in turn be lowered by the IteratorRewriter + yield break; + } + } +}"; + var comp = CreateCompilationWithMscorlib(text, options: TestOptions.DebugDll); + comp.VerifyEmitDiagnostics(); // without the fix for bug 11649, the compilation would fail emitting + CompileAndVerify(comp); + } + + [Fact] + [WorkItem(11649, "https://github.com/dotnet/roslyn/issues/11649")] + public void IteratorRewriterShouldNotRewriteBaseMethodWrapperSymbol2() + { + var source = +@"using System.Collections.Generic; + +class Base +{ + public static void Main() + { + System.Console.WriteLine(string.Join("","", new D().M())); + } + + protected virtual IEnumerable M() + { + yield return 1; + yield return 2; + yield break; + } + + class D : Base + { + protected override IEnumerable M() + { + yield return 0; + foreach (var n in base.M()) + { + yield return n; + } + yield return 3; + yield break; + } + } +}"; + var comp = CompileAndVerify(source, expectedOutput: "0,1,2,3", options: TestOptions.DebugExe); + comp.Compilation.VerifyDiagnostics(); + } + } }