提交 43025e41 编写于 作者: V vsadov
上级 07d69501
......@@ -2069,7 +2069,7 @@ internal override bool IsByRefLikeType
{
isByRefLike = ThreeState.True;
}
else
else if (this.TypeKind == TypeKind.Struct)
{
var moduleSymbol = this.ContainingPEModule;
var module = moduleSymbol.Module;
......@@ -2098,7 +2098,14 @@ internal override bool IsReadOnly
{
var isReadOnly = ThreeState.False;
if (this.TypeKind == TypeKind.Struct)
//PROTOTYPE(span): Span and ReadOnlySpan should have IsReadOnly attribute, eventually.
// For now assume that any "System.Span" and "System.ReadOnlySpan" structs
// are ReadOnly
if (this.IsSpanType())
{
isReadOnly = ThreeState.True;
}
else if (this.TypeKind == TypeKind.Struct)
{
var moduleSymbol = this.ContainingPEModule;
var module = moduleSymbol.Module;
......
......@@ -1342,5 +1342,49 @@ public void TryGet(out int result)
Diagnostic(ErrorCode.ERR_CallArgMixing, "new SW().TryGet(out int value2)").WithArguments("SW.TryGet(out int)", "result").WithLocation(10, 13)
);
}
[WorkItem(21911, "https://github.com/dotnet/roslyn/issues/21911")]
[Fact()]
public void MemberOfReadonlyRefLikeEscapeSpans()
{
var text = @"
using System;
public static class Program
{
public static void Main()
{
Span<int> stackAllocated = stackalloc int[100];
// OK, Span is a readonly struct
new Span<int>().CopyTo(stackAllocated);
// OK, ReadOnlySpan is a readonly struct
new ReadOnlySpan<int>().CopyTo(stackAllocated);
// not OK, Span is writeable
new NotReadOnly<int>().CopyTo(stackAllocated);
}
}
public ref struct NotReadOnly<T>
{
private Span<T> wrapped;
public void CopyTo(Span<T> other)
{
wrapped = other;
}
}
";
CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
// (17,43): error CS8526: Cannot use local 'stackAllocated' in this context because it may expose referenced variables outside of their declaration scope
// new NotReadOnly<int>().CopyTo(stackAllocated);
Diagnostic(ErrorCode.ERR_EscapeLocal, "stackAllocated").WithArguments("stackAllocated").WithLocation(17, 43),
// (17,13): error CS8524: This combination of arguments to 'NotReadOnly<int>.CopyTo(Span<int>)' is disallowed because it may expose variables referenced by parameter 'other' outside of their declaration scope
// new NotReadOnly<int>().CopyTo(stackAllocated);
Diagnostic(ErrorCode.ERR_CallArgMixing, "new NotReadOnly<int>().CopyTo(stackAllocated)").WithArguments("NotReadOnly<int>.CopyTo(System.Span<int>)", "other").WithLocation(17, 13)
);
}
}
}
......@@ -1112,12 +1112,16 @@ unsafe public Span(void* pointer, int length)
{
this.Length = length;
}
public void CopyTo(Span<T> other){}
}
public ref struct ReadOnlySpan<T>
{
public ref readonly T this[int i] => throw null;
public override int GetHashCode() => 2;
public void CopyTo(Span<T> other){}
}
public ref struct SpanLike<T>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册