提交 b4501d10 编写于 作者: Š Šimon Koníček

For a yield return, inferring the type as the type argument of any generic type of arity 1

上级 b20f0bf1
......@@ -5134,6 +5134,36 @@ IEnumerable<int> Goo()
yield return Bar();
}
private int Bar()
{
throw new NotImplementedException();
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
public async Task TestWithYieldReturnInAsyncMethod()
{
await TestInRegularAndScriptAsync(
@"using System.Collections.Generic;
class Program
{
async IAsyncEnumerable<int> Goo()
{
yield return [|Bar|]();
}
}",
@"using System;
using System.Collections.Generic;
class Program
{
async IAsyncEnumerable<int> Goo()
{
yield return Bar();
}
private int Bar()
{
throw new NotImplementedException();
......
......@@ -4716,6 +4716,34 @@ IEnumerable<DayOfWeek> Goo()
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public async Task TestWithYieldReturnInAsyncMethod()
{
await TestInRegularAndScriptAsync(
@"using System;
using System.Collections.Generic;
class Program
{
async IAsyncEnumerable<DayOfWeek> Goo()
{
yield return [|abc|];
}
}",
@"using System;
using System.Collections.Generic;
class Program
{
private DayOfWeek abc;
async IAsyncEnumerable<DayOfWeek> Goo()
{
yield return abc;
}
}");
}
[WorkItem(30235, "https://github.com/dotnet/roslyn/issues/30235")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public async Task TestWithYieldReturnInLocalFunction()
......
......@@ -1060,34 +1060,94 @@ public async Task TestExpressionBodiedAsyncVoidLocalFunction()
}
[WorkItem(827897, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/827897")]
[Theory, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
[InlineData("IEnumerable")]
[InlineData("IEnumerator")]
[InlineData("InvalidGenericType")]
public async Task TestYieldReturnInMethod(string returnTypeName)
{
var markup =
$@"using System.Collections.Generic;
class C
{{
{returnTypeName}<int> M()
{{
yield return [|abc|]
}}
}}";
await TestAsync(markup, "global::System.Int32");
}
[Theory, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
[InlineData("IAsyncEnumerable")]
[InlineData("IAsyncEnumerator")]
[InlineData("InvalidGenericType")]
public async Task TestYieldReturnInAsyncMethod(string returnTypeName)
{
var markup =
$@"namespace System.Collections.Generic
{{
class C
{{
interface {returnTypeName}<T> {{ }}
async {returnTypeName}<int> M()
{{
yield return [|abc|]
}}
}}
}}";
await TestAsync(markup, "global::System.Int32");
}
[Theory, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
[InlineData("int[]")]
[InlineData("InvalidNonGenericType")]
[InlineData("InvalidGenericType<int, int>")]
public async Task TestYieldReturnInvalidTypeInMethod(string returnType)
{
var markup =
$@"class C
{{
{returnType} M()
{{
yield return [|abc|]
}}
}}";
await TestAsync(markup, "global::System.Object");
}
[WorkItem(30235, "https://github.com/dotnet/roslyn/issues/30235")]
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestYieldReturnInMethod()
public async Task TestYieldReturnInLocalFunction()
{
var markup =
@"using System.Collections.Generic;
class Program
class C
{
IEnumerable<int> M()
void M()
{
yield return [|abc|]
IEnumerable<int> F()
{
yield return [|abc|]
}
}
}";
await TestAsync(markup, "global::System.Int32");
}
[WorkItem(30235, "https://github.com/dotnet/roslyn/issues/30235")]
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestYieldReturnInLocalFunction()
public async Task TestYieldReturnInPropertyGetter()
{
var markup =
@"using System.Collections.Generic;
class Program
class C
{
void M()
IEnumerable<int> P
{
IEnumerable<int> F()
get
{
yield return [|abc|]
}
......@@ -1096,6 +1156,25 @@ IEnumerable<int> F()
await TestAsync(markup, "global::System.Int32");
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestYieldReturnInPropertySetter()
{
var markup =
@"using System.Collections.Generic;
class C
{
IEnumerable<int> P
{
set
{
yield return [|abc|]
}
}
}";
await TestAsync(markup, "global::System.Object");
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestYieldReturnAsGlobalStatement()
{
......
......@@ -1778,16 +1778,9 @@ private IEnumerable<TypeInferenceInfo> InferTypeInYieldStatement(YieldStatementS
var memberType = GetMemberType(memberSymbol, out _);
if (memberType is INamedTypeSymbol namedType)
{
if (memberType.OriginalDefinition.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T ||
memberType.OriginalDefinition.SpecialType == SpecialType.System_Collections_Generic_IEnumerator_T)
{
return SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(namedType.TypeArguments[0]));
}
}
return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
return memberType is INamedTypeSymbol namedType && namedType.TypeArguments.Length == 1
? SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(namedType.TypeArguments[0]))
: SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
}
private static ITypeSymbol GetMemberType(ISymbol memberSymbol, out bool isAsync)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册