未验证 提交 c03a2bb7 编写于 作者: R Rikki Gibson 提交者: GitHub

Allow declaring iterator in records (#46713) (#46751)

Co-authored-by: NYair Halberstadt <yairhalberstadt@gmail.com>
上级 3808e924
......@@ -27,7 +27,7 @@ internal sealed class SourceConstructorSymbol : SourceConstructorSymbolBase
ConstructorDeclarationSyntax syntax,
MethodKind methodKind,
DiagnosticBag diagnostics) :
base(containingType, location, syntax)
base(containingType, location, syntax, SyntaxFacts.HasYieldOperations(syntax))
{
bool hasBlockBody = syntax.Body != null;
_isExpressionBodied = !hasBlockBody && syntax.ExpressionBody != null;
......
......@@ -19,8 +19,9 @@ internal abstract class SourceConstructorSymbolBase : SourceMemberMethodSymbol
protected SourceConstructorSymbolBase(
SourceMemberContainerTypeSymbol containingType,
Location location,
CSharpSyntaxNode syntax)
: base(containingType, syntax.GetReference(), ImmutableArray.Create(location), SyntaxFacts.HasYieldOperations(syntax))
CSharpSyntaxNode syntax,
bool isIterator)
: base(containingType, syntax.GetReference(), ImmutableArray.Create(location), isIterator)
{
Debug.Assert(
syntax.IsKind(SyntaxKind.ConstructorDeclaration) ||
......
......@@ -15,7 +15,7 @@ internal sealed class SynthesizedRecordConstructor : SourceConstructorSymbolBase
SourceMemberContainerTypeSymbol containingType,
RecordDeclarationSyntax syntax,
DiagnosticBag diagnostics) :
base(containingType, syntax.ParameterList!.GetLocation(), syntax)
base(containingType, syntax.ParameterList!.GetLocation(), syntax, isIterator: false)
{
this.MakeFlags(MethodKind.Constructor, containingType.IsAbstract ? DeclarationModifiers.Protected : DeclarationModifiers.Public, returnsVoid: true, isExtensionMethod: false);
}
......
......@@ -4,6 +4,7 @@
#nullable enable
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
......@@ -550,7 +551,11 @@ internal static bool HasYieldOperations(SyntaxNode? node)
{
// Do not descend into functions and expressions
return node is object &&
node.DescendantNodesAndSelf(child => !IsNestedFunction(child) && !(node is ExpressionSyntax)).Any(n => n is YieldStatementSyntax);
node.DescendantNodesAndSelf(child =>
{
Debug.Assert(ReferenceEquals(node, child) || child is not (MemberDeclarationSyntax or TypeDeclarationSyntax));
return !IsNestedFunction(child) && !(node is ExpressionSyntax);
}).Any(n => n is YieldStatementSyntax);
}
internal static bool HasReturnWithExpression(SyntaxNode? node)
......
......@@ -22198,5 +22198,30 @@ private void Handle3(SymbolAnalysisContext context)
}
}
}
[Fact]
[WorkItem(46657, "https://github.com/dotnet/roslyn/issues/46657")]
public void CanDeclareIteratorInRecord()
{
var source = @"
using System.Collections.Generic;
public record X(int a)
{
public static void Main()
{
foreach(var i in new X(42).GetItems())
{
System.Console.Write(i);
}
}
public IEnumerable<int> GetItems() { yield return a; yield return a + 1; }
}";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, options: TestOptions.DebugExe, parseOptions: TestOptions.RegularPreview)
.VerifyDiagnostics();
CompileAndVerify(comp, expectedOutput: "4243", verify: Verification.Skipped /* init-only */);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册