Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
Any tools that contribute to the design-time experience should use the MicrosoftCodeAnalysisVersion_LatestVS property above to ensure
they do not break the local dev experience.
-->
<MicrosoftCodeAnalysisCSharpVersion>4.7.0-3.23373.1</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisVersion>4.7.0-3.23373.1</MicrosoftCodeAnalysisVersion>
<MicrosoftNetCompilersToolsetVersion>4.7.0-3.23373.1</MicrosoftNetCompilersToolsetVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.8.0-1.23378.8</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisVersion>4.8.0-1.23378.8</MicrosoftCodeAnalysisVersion>
<MicrosoftNetCompilersToolsetVersion>4.8.0-1.23378.8</MicrosoftNetCompilersToolsetVersion>
</PropertyGroup>
<!--
For source generator support we need to target multiple versions of Roslyn in order to be able to run on older versions of Roslyn.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ public static long Add(ref long location1, long value) =>
/// <summary>Returns a 64-bit signed value, loaded as an atomic operation.</summary>
/// <param name="location">The 64-bit value to be loaded.</param>
/// <returns>The loaded value.</returns>
public static long Read(ref long location) =>
CompareExchange(ref location, 0, 0);
public static long Read(ref readonly long location) =>
CompareExchange(ref Unsafe.AsRef(in location), 0, 0);
#endregion

#region MemoryBarrierProcessWide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void CloseValue(char marker)

private bool WriteSeparator() => _isCommaNeeded ? WriteChar(',') : true;

private bool WriteChar(char source) => WriteChars(new ReadOnlySpan<char>(source));
private bool WriteChar(char source) => WriteChars(new ReadOnlySpan<char>(in source));

private bool WriteChars(ReadOnlySpan<char> chars)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,12 @@ internal ref struct StackAllocatedArguments
[InlineArray(MaxStackAllocArgCount)]
internal ref struct StackAllocatedByRefs
{
// We're intentionally taking advantage of the runtime functionality, even if the language functionality won't work
// CS9184: 'Inline arrays' language feature is not supported for inline array types with element field which is either a 'ref' field, or has type that is not valid as a type argument.

#pragma warning disable CS9184
internal ref byte _arg0;
#pragma warning restore CS9184
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public abstract partial class ComWrappers
internal static IntPtr TaggedImplVftblPtr { get; } = CreateTaggedImplVftbl();
internal static IntPtr DefaultIReferenceTrackerTargetVftblPtr { get; } = CreateDefaultIReferenceTrackerTargetVftbl();

internal static Guid IID_IUnknown = new Guid(0x00000000, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
internal static Guid IID_IReferenceTrackerTarget = new Guid(0x64bd43f8, 0xbfee, 0x4ec4, 0xb7, 0xeb, 0x29, 0x35, 0x15, 0x8d, 0xae, 0x21);
internal static Guid IID_TaggedImpl = new Guid(0x5c13e51c, 0x4f32, 0x4726, 0xa3, 0xfd, 0xf3, 0xed, 0xd6, 0x3d, 0xa3, 0xa0);
internal static readonly Guid IID_IUnknown = new Guid(0x00000000, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
internal static readonly Guid IID_IReferenceTrackerTarget = new Guid(0x64bd43f8, 0xbfee, 0x4ec4, 0xb7, 0xeb, 0x29, 0x35, 0x15, 0x8d, 0xae, 0x21);
internal static readonly Guid IID_TaggedImpl = new Guid(0x5c13e51c, 0x4f32, 0x4726, 0xa3, 0xfd, 0xf3, 0xed, 0xd6, 0x3d, 0xa3, 0xa0);

private static readonly ConditionalWeakTable<object, NativeObjectWrapper> s_rcwTable = new ConditionalWeakTable<object, NativeObjectWrapper>();

Expand All @@ -46,7 +46,7 @@ public static unsafe bool TryGetComInstance(object obj, out IntPtr unknown)
return false;
}

return Marshal.QueryInterface(wrapper._externalComObject, ref IID_IUnknown, out unknown) == 0;
return Marshal.QueryInterface(wrapper._externalComObject, in IID_IUnknown, out unknown) == 0;
}

public static unsafe bool TryGetObject(IntPtr unknown, [NotNullWhen(true)] out object? obj)
Expand Down Expand Up @@ -675,7 +675,7 @@ public object GetOrRegisterObjectForComInstance(IntPtr externalComObject, Create
{
// It is possible the user has defined their own IUnknown impl so
// we fallback to the tagged interface approach to be sure.
if (0 != Marshal.QueryInterface(comObject, ref IID_TaggedImpl, out nint implMaybe))
if (0 != Marshal.QueryInterface(comObject, in IID_TaggedImpl, out nint implMaybe))
{
return null;
}
Expand Down Expand Up @@ -737,8 +737,7 @@ private unsafe bool TryGetOrCreateObjectForComInstanceInternal(
{
// The unwrapped object has a CCW in this context. Get the IUnknown for the externalComObject
// so we can see if it's the CCW for the unwrapped object in this context.
Guid iid = IID_IUnknown;
int hr = Marshal.QueryInterface(externalComObject, ref iid, out IntPtr externalIUnknown);
int hr = Marshal.QueryInterface(externalComObject, in IID_IUnknown, out IntPtr externalIUnknown);
Debug.Assert(hr == 0); // An external COM object that came from a ComWrappers instance
// will always be well-formed.
if (unwrappedWrapperInThisContext.ComIp == externalIUnknown)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static unsafe byte[] ToByteArray(List<SslApplicationProtocol> application
byte[] buffer = new byte[sizeof(Sec_Application_Protocols) + protocolListSize];
int index = 0;

MemoryMarshal.Write(buffer.AsSpan(index), ref protocols);
MemoryMarshal.Write(buffer.AsSpan(index), in protocols);
index += sizeof(Sec_Application_Protocols);

for (int i = 0; i < applicationProtocols.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void ICollection.CopyTo(Array array, int index)
/// <summary>Gets either a reference to a <typeparamref name="TValue"/> in the dictionary or a null reference if the key does not exist in the dictionary.</summary>
/// <param name="key">The key used for lookup.</param>
/// <returns>A reference to a <typeparamref name="TValue"/> in the dictionary or a null reference if the key does not exist in the dictionary.</returns>
/// <remarks>The null reference can be detected by calling <see cref="Unsafe.IsNullRef{T}(ref T)"/>.</remarks>
/// <remarks>The null reference can be detected by calling <see cref="Unsafe.IsNullRef{T}(ref readonly T)"/>.</remarks>
public ref readonly TValue GetValueRefOrNullRef(TKey key)
{
if (key is null)
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Data.OleDb/src/OleDbComWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// We need to target netstandard2.0, so keep using ref for MemoryMarshal.Write
// CS9191: The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.
#pragma warning disable CS9191

namespace System.Data.OleDb
{
/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Data.OleDb/src/SafeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
using System.Runtime.Versioning;
using static System.Data.Common.UnsafeNativeMethods;

// We need to target netstandard2.0, so keep using ref for MemoryMarshal.Write
// CS9191: The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.
#pragma warning disable CS9191

namespace System.Data.OleDb
{
internal sealed class DualCoTaskMem : SafeHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
using System.Runtime.Versioning;
using System.Text;

// We need to target netstandard2.0, so keep using ref for MemoryMarshal.Write
// CS9191: The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.
#pragma warning disable CS9191

namespace WbemClient_v1 { }
namespace WbemUtilities_v1 { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

using System.Runtime.InteropServices;

// We need to target netstandard2.0, so keep using ref for MemoryMarshal.Write
// CS9191: The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.
#pragma warning disable CS9191

namespace System.Management
{

Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public static partial class MemoryMarshal
public static System.ReadOnlySpan<TTo> Cast<TFrom, TTo>(System.ReadOnlySpan<TFrom> span) where TFrom : struct where TTo : struct { throw null; }
public static System.Span<TTo> Cast<TFrom, TTo>(System.Span<TFrom> span) where TFrom : struct where TTo : struct { throw null; }
public static System.Memory<T> CreateFromPinnedArray<T>(T[]? array, int start, int length) { throw null; }
public static System.ReadOnlySpan<T> CreateReadOnlySpan<T>(scoped ref T reference, int length) { throw null; }
public static System.ReadOnlySpan<T> CreateReadOnlySpan<T>(scoped ref readonly T reference, int length) { throw null; }
[System.CLSCompliant(false)]
public static unsafe ReadOnlySpan<byte> CreateReadOnlySpanFromNullTerminated(byte* value) { throw null; }
[System.CLSCompliant(false)]
Expand All @@ -710,8 +710,8 @@ public static partial class MemoryMarshal
public static bool TryGetMemoryManager<T, TManager>(System.ReadOnlyMemory<T> memory, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out TManager? manager, out int start, out int length) where TManager : System.Buffers.MemoryManager<T> { throw null; }
public static bool TryGetString(System.ReadOnlyMemory<char> memory, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out string? text, out int start, out int length) { throw null; }
public static bool TryRead<T>(System.ReadOnlySpan<byte> source, out T value) where T : struct { throw null; }
public static bool TryWrite<T>(System.Span<byte> destination, ref T value) where T : struct { throw null; }
public static void Write<T>(System.Span<byte> destination, ref T value) where T : struct { }
public static bool TryWrite<T>(System.Span<byte> destination, in T value) where T : struct { throw null; }
public static void Write<T>(System.Span<byte> destination, in T value) where T : struct { }
}
}
namespace System.Text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public readonly long Length
if (_length < 0)
{
// Cast-away readonly to initialize lazy field
Unsafe.AsRef(_length) = Sequence.Length;
Unsafe.AsRef(in _length) = Sequence.Length;
}
return _length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private void WriteIPv6Bytes(Span<byte> destination)
private void WriteIPv4Bytes(Span<byte> destination)
{
uint address = PrivateAddress;
MemoryMarshal.Write(destination, ref address);
MemoryMarshal.Write(destination, in address);
Comment on lines 333 to +334
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this to:

MemoryMarshal.Write(destination, PrivateAddress);

? Seems like the primary motivation behind changing the parameter from ref to in is so that it can use rvalues

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely can. I had just gone with the most straightforward set of changes to start since we're trying to land this in .NET 8

}

/// <devdoc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public static Exception GetException(SecurityStatusPal status)
internal const bool StartMutualAuthAsAnonymous = true;
internal const bool CanEncryptEmptyMessage = true;

private static readonly byte[] s_sessionTokenBuffer = MemoryMarshal.AsBytes(new ReadOnlySpan<Interop.SChannel.SCHANNEL_SESSION_TOKEN>(
new Interop.SChannel.SCHANNEL_SESSION_TOKEN()
{
private static readonly byte[] s_sessionTokenBuffer = InitSessionTokenBuffer();

private static byte[] InitSessionTokenBuffer()
{
var schannelSessionToken = new Interop.SChannel.SCHANNEL_SESSION_TOKEN() {
dwTokenType = Interop.SChannel.SCHANNEL_SESSION,
dwFlags = Interop.SChannel.SSL_SESSION_DISABLE_RECONNECTS,
}
)).ToArray();
};
return MemoryMarshal.AsBytes(new ReadOnlySpan<Interop.SChannel.SCHANNEL_SESSION_TOKEN>(in schannelSessionToken)).ToArray();
}

public static void VerifyPackageInfo()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public static partial class Vector
[System.CLSCompliantAttribute(false)]
public static unsafe System.Numerics.Vector<T> LoadAlignedNonTemporal<T>(T* source) { throw null; }
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type ('T')
public static System.Numerics.Vector<T> LoadUnsafe<T>(ref T source) { throw null; }
public static System.Numerics.Vector<T> LoadUnsafe<T>(ref readonly T source) { throw null; }
[System.CLSCompliantAttribute(false)]
public static System.Numerics.Vector<T> LoadUnsafe<T>(ref T source, nuint elementOffset) { throw null; }
public static System.Numerics.Vector<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) { throw null; }
public static System.Numerics.Vector<T> Max<T>(System.Numerics.Vector<T> left, System.Numerics.Vector<T> right) { throw null; }
public static System.Numerics.Vector<T> Min<T>(System.Numerics.Vector<T> left, System.Numerics.Vector<T> right) { throw null; }
public static System.Numerics.Vector<T> Multiply<T>(System.Numerics.Vector<T> left, System.Numerics.Vector<T> right) { throw null; }
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public bool TryFormat(Span<char> destination, out int charsWritten)
if (destination.Length > 3)
{
ulong true_val = BitConverter.IsLittleEndian ? 0x65007500720054ul : 0x54007200750065ul; // "True"
MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), ref true_val);
MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), in true_val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and elsewhere

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning this?

MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), BitConverter.IsLittleEndian ? 0x65007500720054ul : 0x54007200750065ul); // "True"

charsWritten = 4;
return true;
}
Expand All @@ -109,7 +109,7 @@ public bool TryFormat(Span<char> destination, out int charsWritten)
if (destination.Length > 4)
{
ulong fals_val = BitConverter.IsLittleEndian ? 0x73006C00610046ul : 0x460061006C0073ul; // "Fals"
MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), ref fals_val);
MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), in fals_val);
destination[4] = 'e';
charsWritten = 5;
return true;
Expand Down
Loading