提交 aee80db7 编写于 作者: V Vladimir Sadov 提交者: GitHub

Merge pull request #16132 from VSadov/refValTest

Added a test to validate that __refvalue is not allowed to be returned by reference
......@@ -225,6 +225,47 @@ static void Main()
);
}
[Fact]
public void RefValueUnsafeToReturn()
{
var text = @"
using System;
class C
{
private static ref int Test()
{
int aa = 42;
var tr = __makeref(aa);
ref var r = ref Test2(ref __refvalue(tr, int));
return ref r;
}
private static ref int Test2(ref int r)
{
return ref r;
}
private static ref int Test3(TypedReference tr)
{
return ref __refvalue(tr, int);
}
}";
var comp = CreateCompilationWithMscorlib(text);
comp.VerifyDiagnostics(
// (13,20): error CS8157: Cannot return 'r' by reference because it was initialized to a value that cannot be returned by reference
// return ref r;
Diagnostic(ErrorCode.ERR_RefReturnNonreturnableLocal, "r").WithArguments("r").WithLocation(13, 20),
// (23,20): error CS8156: An expression cannot be used in this context because it may not be returned by reference
// return ref __refvalue(tr, int);
Diagnostic(ErrorCode.ERR_RefReturnLvalueExpected, "__refvalue(tr, int)").WithLocation(23, 20)
);
}
[Fact]
public void MakeRefTest03_Dynamic_Bind()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册