未验证 提交 c0d03bfb 编写于 作者: J Julien Couvreur 提交者: GitHub

Extra check for EnumeratorCancellation parameter type (#39978)

上级 5be9cb6f
......@@ -197,9 +197,10 @@ protected override BoundStatement InitializeParameterField(MethodSymbol getEnume
{
BoundStatement result;
if (_combinedTokensField is object &&
parameter is SourceComplexParameterSymbol { HasEnumeratorCancellationAttribute: true })
parameter is SourceComplexParameterSymbol { HasEnumeratorCancellationAttribute: true } &&
parameter.Type.Equals(F.Compilation.GetWellKnownType(WellKnownType.System_Threading_CancellationToken), TypeCompareKind.ConsiderEverything))
{
// For the parameter with [EnumeratorCancellation]
// For a parameter of type CancellationToken with [EnumeratorCancellation]
// if (this.parameterProxy.Equals(default))
// {
// result.parameter = token;
......
......@@ -6487,6 +6487,7 @@ static async System.Collections.Generic.IAsyncEnumerable<int> Iter([EnumeratorCa
}
[Fact, WorkItem(34407, "https://github.com/dotnet/roslyn/issues/34407")]
[WorkItem(39961, "https://github.com/dotnet/roslyn/issues/39961")]
public void CancellationTokenParameter_WrongParameterType()
{
string source = @"
......@@ -6499,13 +6500,21 @@ static async System.Collections.Generic.IAsyncEnumerable<int> Iter([EnumeratorCa
yield return value++;
await Task.Yield();
}
static async Task Main()
{
await foreach (var i in Iter(42))
{
System.Console.Write(i);
}
}
}";
var comp = CreateCompilationWithAsyncIterator(new[] { source, EnumeratorCancellationAttributeType });
var comp = CreateCompilationWithAsyncIterator(new[] { source, EnumeratorCancellationAttributeType }, TestOptions.DebugExe);
comp.VerifyDiagnostics(
// (6,73): warning CS8424: The EnumeratorCancellationAttribute applied to parameter 'value' will have no effect. The attribute is only effective on a parameter of type CancellationToken in an async-enumerable method
// static async System.Collections.Generic.IAsyncEnumerable<int> Iter([EnumeratorCancellation] int value)
Diagnostic(ErrorCode.WRN_UnconsumedEnumeratorCancellationAttributeUsage, "EnumeratorCancellation").WithArguments("value").WithLocation(6, 73)
);
CompileAndVerify(comp, expectedOutput: "42");
}
[Fact, WorkItem(34407, "https://github.com/dotnet/roslyn/issues/34407")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册