Document span lifetime issue in CreateSpan (dotnet/corefxdotnet/coreclr#30490) (dotne...
authordotnet bot <dotnet-bot@dotnetfoundation.org>
Mon, 18 Jun 2018 17:45:42 +0000 (10:45 -0700)
committerJan Kotas <jkotas@microsoft.com>
Mon, 18 Jun 2018 17:45:42 +0000 (10:45 -0700)
commit2cf01b54d855f8b8a060b25975d2030b45759ce8
tree63829f8736bde666e0cf8b091e28ee755bab001e
parentbdd95622ee59296b595132964d2fae17594a9ed8
Document span lifetime issue in CreateSpan (dotnet/corefxdotnet/coreclr#30490) (dotnet/coreclr#18528)

The language rules around span safety that C# and F# adhere to assume
there is no way to create a `Span<T>` wrapper over a `ref` local /
parameter. This means `ref` inputs into a method are not considered when
calculating the allowed lifetime of a returned `Span<T>`. Hence both
CreateSpan and CreateReadOnlySpan will be assumed to have heap lifetime
even when provided stack based inputs. Example:

``` c#
Span<int> Example() {
  int i = 42;
  Span<int> span = MemoryMarshal.CreateSpan(ref i, length: 1);
  return span; // C# and F# will allow this
}
```

In this case the actual lifetime of `span` is that of `i`. Yet the
compiler doesn't consider the `ref i` input and hence believes this must
be heap based and hence safe to return out of the method.

This is okay as these methods are unsafe. But want to explicitly
document that fact.

More information on the safety rules can be found in the [span safety
proposal](https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/span-safety.md)

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
Commit migrated from https://github.com/dotnet/coreclr/commit/ac637552982717e72e473df03d0994e65c2dccd8
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.Fast.cs