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

Merge pull request #21860 from VSadov/fix21858

Val-escape for a struct field should defer to _val_ escape of the receiver
......@@ -1903,7 +1903,7 @@ internal static uint GetValEscape(BoundExpression expr, uint scopeOfTheContainin
Debug.Assert(!fieldSymbol.IsStatic && fieldSymbol.ContainingType.IsByRefLikeType);
// for ref-like fields defer to the receiver.
return GetRefEscape(fieldAccess.ReceiverOpt, scopeOfTheContainingExpression);
return GetValEscape(fieldAccess.ReceiverOpt, scopeOfTheContainingExpression);
case BoundKind.Call:
var call = (BoundCall)expr;
......
......@@ -84,21 +84,9 @@ internal override SyntaxNode ScopeDesignatorOpt
get { return _scopeBinder.ScopeDesignator; }
}
internal override uint RefEscapeScope
{
get
{
return _refEscapeScope;
}
}
internal override uint RefEscapeScope => _refEscapeScope;
internal override uint ValEscapeScope
{
get
{
return _valEscapeScope;
}
}
internal override uint ValEscapeScope => _valEscapeScope;
/// <summary>
/// Binder that should be used to bind type syntax for the local.
......
......@@ -1250,5 +1250,54 @@ static S1 MayWrap(ref int arg)
Diagnostic(ErrorCode.ERR_EscapeLocal, "sp1").WithArguments("sp1").WithLocation(21, 20)
);
}
[WorkItem(21858, "https://github.com/dotnet/roslyn/issues/21858")]
[Fact()]
public void FieldOfRefLikeEscape()
{
var text = @"
class Program
{
static void Main()
{
}
ref struct S1
{
private S2 x;
public S1(S2 arg) => x = arg;
public S2 M1()
{
// ok
return x;
}
public S2 M2()
{
var toReturn = x;
// ok
return toReturn;
}
public ref S2 M3()
{
// not ok
return ref x;
}
}
ref struct S2{}
}
";
CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
// (31,28): error CS8170: Struct members cannot return 'this' or other instance members by reference
// return ref x;
Diagnostic(ErrorCode.ERR_RefReturnStructThis, "x").WithArguments("this").WithLocation(31, 28)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册