提交 07d69501 编写于 作者: V vsadov

in escape analysis receivers of readonly structs should be considered as `in` parameters.

Fixes:#21880
上级 b69b9cfe
......@@ -1115,7 +1115,8 @@ private bool CheckPropertyValueKind(SyntaxNode node, BoundExpression expr, BindV
uint escapeTo = scopeOfTheContainingExpression;
// collect all writeable ref-like arguments, including receiver
if (receiverOpt?.Type?.IsByRefLikeType == true)
var receiverType = receiverOpt?.Type;
if (receiverType?.IsByRefLikeType == true && receiverType?.IsReadOnly == false)
{
escapeTo = GetValEscape(receiverOpt, scopeOfTheContainingExpression);
}
......
......@@ -1299,5 +1299,48 @@ public S2 M2()
Diagnostic(ErrorCode.ERR_RefReturnStructThis, "x").WithArguments("this").WithLocation(31, 28)
);
}
[WorkItem(21880, "https://github.com/dotnet/roslyn/issues/21880")]
[Fact()]
public void MemberOfReadonlyRefLikeEscape()
{
var text = @"
public static class Program
{
public static void Main()
{
// OK, SR is readonly
new SR().TryGet(out int value1);
// not OK, TryGet can write into the instance
new SW().TryGet(out int value2);
}
}
public readonly ref struct SR
{
public void TryGet(out int result)
{
result = 1;
}
}
public ref struct SW
{
public void TryGet(out int result)
{
result = 1;
}
}
";
CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
// (10,33): error CS8168: Cannot return local 'value2' by reference because it is not a ref local
// new SW().TryGet(out int value2);
Diagnostic(ErrorCode.ERR_RefReturnLocal, "int value2").WithArguments("value2").WithLocation(10, 33),
// (10,13): error CS8524: This combination of arguments to 'SW.TryGet(out int)' is disallowed because it may expose variables referenced by parameter 'result' outside of their declaration scope
// new SW().TryGet(out int value2);
Diagnostic(ErrorCode.ERR_CallArgMixing, "new SW().TryGet(out int value2)").WithArguments("SW.TryGet(out int)", "result").WithLocation(10, 13)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册