Skip to content

Commit b44e705

Browse files
authored
Move vendored ReadOnlySpan<T>, Span<T> et. al. to System namespace (#8476)
## Summary of changes Updates the System.Memory vendoring code to move types like `ReadOnlySpan<T>`, `Span<T>` into `System` instead of `Datadog.Trace.VendoredMicrosoftCode.System` ## Reason for change The compiler has various functionality that relies on the `Span<T>` (and `ReadOnlySpan<T>`) being available in the `System` namespace. By making this change, we get the advantage of those types being available. A separate PR will actually update code to use those types, except where changes were required to make it compile in this PR. ## Implementation details Update the vendoring code to stop changing the namespace. ## Test coverage This is the test, if the tests pass, we should be fine. ## Other details Depends on a stack updating our vendored system code - #8391 - #8454 - #8455 - #8459 - #8461 - #8469
1 parent 14c47d8 commit b44e705

92 files changed

Lines changed: 276 additions & 252 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tracer/build/_build/UpdateVendors/VendoredDependency.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,27 @@ private static string FixSystemMemory(string filePath, string contents)
725725

726726
contents = contents.Insert(namespaceIndex, usings);
727727

728+
// Leave these in the vendored namespace to avoid naming conflicts with various _other_ ThrowHelper files,
729+
// and MemoryExtensions causes conflicts in the test projects - it's extension methods, so doesn't matter too much where it is
730+
if (string.Equals(Path.GetFileName(filePath), "ThrowHelper.cs")
731+
|| string.Equals(Path.GetFileName(filePath), "MemoryExtensions.cs")
732+
|| string.Equals(Path.GetFileName(filePath), "MemoryExtensions.Portable.cs"))
733+
{
734+
contents = contents.Replace(
735+
"namespace System",
736+
"namespace Datadog.Trace.VendoredMicrosoftCode.System");
737+
}
738+
739+
// Note that we leave everything in the System namespace where it is,
740+
// so that the compiler can light up for things like Span<T>. It does raise the question
741+
// of whether we should bother renaming _any_ of these APIs, but leaving as-is for now
742+
// to avoid confusion
728743
contents = contents.Replace(
729744
"Datadog.Trace.VendoredMicrosoftCode.System..omponentModel.",
730745
"System.ComponentModel.")
731746
.Replace(
732747
"Datadog.Trace.VendoredMicrosoftCode.System..UInt",
733-
"Datadog.Trace.VendoredMicrosoftCode.System.NUInt")
748+
"System.NUInt")
734749
.Replace(
735750
"using Datadog.Trace.VendoredMicrosoftCode.System.ComponentModel",
736751
"using System.ComponentModel")
@@ -747,12 +762,8 @@ private static string FixSystemMemory(string filePath, string contents)
747762
"using Datadog.Trace.VendoredMicrosoftCode.System.Text",
748763
"using System.Text")
749764
.Replace(
750-
"namespace System\r\n",
751-
"namespace System\n")
752-
// TODO: We should consider _not_ making this change in the future
753-
.Replace(
754-
"namespace System\n",
755-
"namespace Datadog.Trace.VendoredMicrosoftCode.System\n");
765+
"ThrowHelper.",
766+
"global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.");
756767

757768
var resourceReplacements = new List<KeyValuePair<string, string>>
758769
{

tracer/src/Datadog.Trace/ExtensionMethods/StringBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#nullable enable
77

8+
using System;
89
using System.Text;
910

1011
#if !NETCOREAPP3_1_OR_GREATER

tracer/src/Datadog.Trace/Propagators/W3CTraceContextPropagator.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ internal static bool TryParseTraceParent(string header, out W3CTraceParent trace
269269
string rawTraceId;
270270
string rawSpanId;
271271

272-
#if NETCOREAPP
273272
var w3cTraceId = header.AsSpan(start: 3, length: 32);
274273
var w3cSpanId = header.AsSpan(start: 36, length: 16);
275274

@@ -295,31 +294,6 @@ internal static bool TryParseTraceParent(string header, out W3CTraceParent trace
295294
{
296295
return false;
297296
}
298-
#else
299-
rawTraceId = header.Substring(startIndex: 3, length: 32);
300-
rawSpanId = header.Substring(startIndex: 36, length: 16);
301-
302-
if (!HexString.TryParseTraceId(rawTraceId, out traceId) || traceId == TraceId.Zero)
303-
{
304-
return false;
305-
}
306-
307-
if (!HexString.TryParseUInt64(rawSpanId, out parentId) || parentId == 0)
308-
{
309-
return false;
310-
}
311-
312-
bool sampled;
313-
314-
if (HexString.TryParseByte(header.Substring(53, 2), out var traceFlags))
315-
{
316-
sampled = ((TraceFlags)traceFlags).HasFlagFast(TraceFlags.Sampled);
317-
}
318-
else
319-
{
320-
return false;
321-
}
322-
#endif
323297

324298
traceParent = new W3CTraceParent(
325299
traceId: traceId,

tracer/src/Datadog.Trace/Vendors/System.Memory/System/Buffers/ArrayMemoryPool.ArrayMemoryPoolBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Memory<T> Memory
3939
T[] array = _array;
4040
if (array == null)
4141
{
42-
ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
42+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
4343
}
4444

4545
return new Memory<T>(array);

tracer/src/Datadog.Trace/Vendors/System.Memory/System/Buffers/ArrayMemoryPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed override IMemoryOwner<T> Rent(int minimumBufferSize = -1)
3131
if (minimumBufferSize == -1)
3232
minimumBufferSize = 1 + (4095 / Unsafe.SizeOf<T>());
3333
else if (((uint)minimumBufferSize) > s_maxBufferSize)
34-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.minimumBufferSize);
34+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.minimumBufferSize);
3535

3636
return new ArrayMemoryPoolBuffer(minimumBufferSize);
3737
}

tracer/src/Datadog.Trace/Vendors/System.Memory/System/Buffers/BuffersExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal static class BuffersExtensions
7979
public static void CopyTo<T>(in this ReadOnlySequence<T> source, Span<T> destination)
8080
{
8181
if (source.Length > destination.Length)
82-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination);
82+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination);
8383

8484
if (source.IsSingleSegment)
8585
{

tracer/src/Datadog.Trace/Vendors/System.Memory/System/Buffers/ReadOnlySequence.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public ReadOnlySequence(ReadOnlySequenceSegment<T> startSegment, int startIndex,
9797
(uint)startSegment.Memory.Length < (uint)startIndex ||
9898
(uint)endSegment.Memory.Length < (uint)endIndex ||
9999
(startSegment == endSegment && endIndex < startIndex))
100-
ThrowHelper.ThrowArgumentValidationException(startSegment, startIndex, endSegment);
100+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentValidationException(startSegment, startIndex, endSegment);
101101

102102
_sequenceStart = new SequencePosition(startSegment, ReadOnlySequence.SegmentToSequenceStart(startIndex));
103103
_sequenceEnd = new SequencePosition(endSegment, ReadOnlySequence.SegmentToSequenceEnd(endIndex));
@@ -109,7 +109,7 @@ public ReadOnlySequence(ReadOnlySequenceSegment<T> startSegment, int startIndex,
109109
public ReadOnlySequence(T[] array)
110110
{
111111
if (array == null)
112-
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
112+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
113113

114114
_sequenceStart = new SequencePosition(array, ReadOnlySequence.ArrayToSequenceStart(0));
115115
_sequenceEnd = new SequencePosition(array, ReadOnlySequence.ArrayToSequenceEnd(array.Length));
@@ -123,7 +123,7 @@ public ReadOnlySequence(T[] array, int start, int length)
123123
if (array == null ||
124124
(uint)start > (uint)array.Length ||
125125
(uint)length > (uint)(array.Length - start))
126-
ThrowHelper.ThrowArgumentValidationException(array, start);
126+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentValidationException(array, start);
127127

128128
_sequenceStart = new SequencePosition(array, ReadOnlySequence.ArrayToSequenceStart(start));
129129
_sequenceEnd = new SequencePosition(array, ReadOnlySequence.ArrayToSequenceEnd(start + length));
@@ -150,15 +150,15 @@ public ReadOnlySequence(ReadOnlyMemory<T> memory)
150150
else if (typeof(T) == typeof(char))
151151
{
152152
if (!MemoryMarshal.TryGetString(((ReadOnlyMemory<char>)(object)memory), out string text, out int start, out length))
153-
ThrowHelper.ThrowInvalidOperationException();
153+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowInvalidOperationException();
154154

155155
_sequenceStart = new SequencePosition(text, ReadOnlySequence.StringToSequenceStart(start));
156156
_sequenceEnd = new SequencePosition(text, ReadOnlySequence.StringToSequenceEnd(start + length));
157157
}
158158
else
159159
{
160160
// Should never be reached
161-
ThrowHelper.ThrowInvalidOperationException();
161+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowInvalidOperationException();
162162
_sequenceStart = default;
163163
_sequenceEnd = default;
164164
}
@@ -172,7 +172,7 @@ public ReadOnlySequence(ReadOnlyMemory<T> memory)
172172
public ReadOnlySequence<T> Slice(long start, long length)
173173
{
174174
if (start < 0 || length < 0)
175-
ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
175+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
176176

177177
SequencePosition begin;
178178
SequencePosition end;
@@ -201,7 +201,7 @@ public ReadOnlySequence<T> Slice(long start, long length)
201201
else
202202
{
203203
if (currentLength < 0)
204-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
204+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
205205

206206
begin = SeekMultiSegment(startSegment.Next, endObject, endIndex, start - currentLength, ExceptionArgument.start);
207207

@@ -216,7 +216,7 @@ public ReadOnlySequence<T> Slice(long start, long length)
216216
else
217217
{
218218
if (endIndex - beginIndex < length)
219-
ThrowHelper.ThrowStartOrEndArgumentValidationException(0); // Passing value >= 0 means throw exception on length argument
219+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(0); // Passing value >= 0 means throw exception on length argument
220220

221221
end = new SequencePosition(beginObject, beginIndex + (int)length);
222222
}
@@ -225,13 +225,13 @@ public ReadOnlySequence<T> Slice(long start, long length)
225225
else
226226
{
227227
if (endIndex - startIndex < start)
228-
ThrowHelper.ThrowStartOrEndArgumentValidationException(-1); // Passing value < 0 means throw exception on start argument
228+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(-1); // Passing value < 0 means throw exception on start argument
229229

230230
startIndex += (int)start;
231231
begin = new SequencePosition(startObject, startIndex);
232232

233233
if (endIndex - startIndex < length)
234-
ThrowHelper.ThrowStartOrEndArgumentValidationException(0); // Passing value >= 0 means throw exception on length argument
234+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(0); // Passing value >= 0 means throw exception on length argument
235235

236236
end = new SequencePosition(startObject, startIndex + (int)length);
237237
}
@@ -247,7 +247,7 @@ public ReadOnlySequence<T> Slice(long start, long length)
247247
public ReadOnlySequence<T> Slice(long start, SequencePosition end)
248248
{
249249
if (start < 0)
250-
ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
250+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
251251

252252
uint sliceEndIndex = (uint)GetIndex(end);
253253
object sliceEndObject = end.GetObject();
@@ -263,11 +263,11 @@ public ReadOnlySequence<T> Slice(long start, SequencePosition end)
263263
{
264264
if (!InRange(sliceEndIndex, startIndex, endIndex))
265265
{
266-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
266+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
267267
}
268268

269269
if (sliceEndIndex - startIndex < start)
270-
ThrowHelper.ThrowStartOrEndArgumentValidationException(-1); // Passing value < 0 means throw exception on start argument
270+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(-1); // Passing value < 0 means throw exception on start argument
271271

272272
goto FoundInFirstSegment;
273273
}
@@ -284,12 +284,12 @@ public ReadOnlySequence<T> Slice(long start, SequencePosition end)
284284
startRange,
285285
(ulong)(((ReadOnlySequenceSegment<T>)endObject).RunningIndex + endIndex)))
286286
{
287-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
287+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
288288
}
289289

290290
if (startRange + (ulong)start > sliceRange)
291291
{
292-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
292+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
293293
}
294294

295295
int currentLength = startSegment.Memory.Length - (int)startIndex;
@@ -298,7 +298,7 @@ public ReadOnlySequence<T> Slice(long start, SequencePosition end)
298298
if (currentLength <= start)
299299
{
300300
if (currentLength < 0)
301-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
301+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
302302

303303
// End of segment. Move to start of next.
304304
SequencePosition begin = SeekMultiSegment(startSegment.Next, sliceEndObject, (int)sliceEndIndex, start - currentLength, ExceptionArgument.start);
@@ -333,15 +333,15 @@ public ReadOnlySequence<T> Slice(SequencePosition start, long length)
333333
{
334334
if (!InRange(sliceStartIndex, startIndex, endIndex))
335335
{
336-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
336+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
337337
}
338338

339339
if (length < 0)
340340
// Passing value >= 0 means throw exception on length argument
341-
ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
341+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
342342

343343
if (endIndex - sliceStartIndex < length)
344-
ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
344+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
345345

346346
goto FoundInFirstSegment;
347347
}
@@ -356,16 +356,16 @@ public ReadOnlySequence<T> Slice(SequencePosition start, long length)
356356
Debug.Assert(sliceStartIndex >= 0 && startIndex >= 0 && endIndex >= 0);
357357
if (!InRange(sliceRange, startRange, endRange))
358358
{
359-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
359+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
360360
}
361361

362362
if (length < 0)
363363
// Passing value >= 0 means throw exception on length argument
364-
ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
364+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(0);
365365

366366
if (sliceRange + (ulong)length > endRange)
367367
{
368-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
368+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
369369
}
370370

371371
int currentLength = sliceStartSegment.Memory.Length - (int)sliceStartIndex;
@@ -374,7 +374,7 @@ public ReadOnlySequence<T> Slice(SequencePosition start, long length)
374374
if (currentLength < length)
375375
{
376376
if (currentLength < 0)
377-
ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
377+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_PositionOutOfRange();
378378

379379
// End of segment. Move to start of next.
380380
SequencePosition end = SeekMultiSegment(sliceStartSegment.Next, endObject, (int)endIndex, length - currentLength, ExceptionArgument.length);
@@ -438,7 +438,7 @@ public ReadOnlySequence<T> Slice(SequencePosition start)
438438
public ReadOnlySequence<T> Slice(long start)
439439
{
440440
if (start < 0)
441-
ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
441+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowStartOrEndArgumentValidationException(start);
442442

443443
if (start == 0)
444444
return this;
@@ -485,7 +485,7 @@ public override string ToString()
485485
public SequencePosition GetPosition(long offset, SequencePosition origin)
486486
{
487487
if (offset < 0)
488-
ThrowHelper.ThrowArgumentOutOfRangeException_OffsetOutOfRange();
488+
global::Datadog.Trace.VendoredMicrosoftCode.System.ThrowHelper.ThrowArgumentOutOfRangeException_OffsetOutOfRange();
489489

490490
return Seek(origin, _sequenceEnd, offset, ExceptionArgument.offset);
491491
}

0 commit comments

Comments
 (0)