未验证 提交 868ea4cd 编写于 作者: J Jared Parsons 提交者: GitHub

Merge pull request #28206 from OmarTawfik/bugs/28117/ref-assign-to-parameters

Produce errors on ref-assignment to byval parameters
......@@ -136,3 +136,4 @@ Example:
- Visual Studio 2017 version 15.8: https://github.com/dotnet/roslyn/pull/27803 pattern matching now will produce errors when trying to return a stack bound value to an invalid escape scope.
- Visual Studio 2017 version 15.8: https://github.com/dotnet/roslyn/issues/26743 C# will now reject expressions such as `public int* M => &this.Bar[0];` if `Bar` is a fixed field of the containing type.
- Visual Studio 2017 version 15.8: https://github.com/dotnet/roslyn/issues/27772 invocation receivers will be checked for escape scope errors now in nested scope expressions.
- Visual Studio 2017 version 15.8: https://github.com/dotnet/roslyn/issues/28117 C# will now reject ref assignments to byval parameters.
......@@ -588,6 +588,11 @@ private bool CheckParameterValueKind(SyntaxNode node, BoundParameter parameter,
ReportReadOnlyError(parameterSymbol, node, valueKind, checkingReceiver, diagnostics);
return false;
}
else if (parameterSymbol.RefKind == RefKind.None && RequiresRefAssignableVariable(valueKind))
{
Error(diagnostics, ErrorCode.ERR_RefLocalOrParamExpected, node);
return false;
}
if (this.LockedOrDisposedVariables.Contains(parameterSymbol))
{
......
......@@ -3818,6 +3818,23 @@ void N()
Diagnostic(ErrorCode.ERR_SyntaxError, "x").WithArguments(",", "").WithLocation(10, 22));
}
[Fact]
[WorkItem(28117, "https://github.com/dotnet/roslyn/issues/28117")]
public void AssigningRefToParameter()
{
CreateCompilation(@"
public class C
{
void M(int a, ref int b)
{
a = ref b;
}
}").VerifyDiagnostics(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// a = ref b;
Diagnostic(ErrorCode.ERR_RefLocalOrParamExpected, "a").WithLocation(6, 9));
}
[Fact]
[WorkItem(26516, "https://github.com/dotnet/roslyn/issues/26516")]
public void BindingRefVoidAssignment()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册