Skip to content

Commit 1102b3b

Browse files
committed
Update vendored definition for System.Collections.Immutable
1 parent db7642d commit 1102b3b

1 file changed

Lines changed: 144 additions & 5 deletions

File tree

tracer/build/_build/UpdateVendors/VendoredDependency.cs

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,70 @@ static VendoredDependency()
9696

9797
Add(
9898
libraryName: "System.Collections.Immutable",
99-
version: "7.0.0",
100-
downloadUrl: "https://github.com/DataDog/dotnet-vendored-code/archive/refs/tags/1.0.0.zip",
101-
pathToSrc: new[] { "dotnet-vendored-code-1.0.0", "System.Reflection.Metadata", "System.Collections.Immutable" },
99+
version: "7.0.20",
100+
downloadUrl: "https://github.com/dotnet/runtime/archive/refs/tags/v7.0.20.zip",
101+
pathToSrc: new[] { "runtime-7.0.20", "src", "libraries", "System.Collections.Immutable", "src" },
102102
transform: filePath =>
103103
{
104-
RewriteCsFileWithStandardTransform(filePath, originalNamespace: "System.Collections.", AddNullableDirectiveTransform, AddIgnoreNullabilityWarningDisablePragma);
104+
RewriteCsFileWithStandardTransform(filePath, originalNamespace: "System.Collections.", AddIgnoreNullabilityWarningDisablePragma);
105+
// we run these _after_ the standard transform otherwise we get issues
106+
if (string.Equals(Path.GetExtension(filePath), ".cs", StringComparison.OrdinalIgnoreCase))
107+
{
108+
RewriteFileWithTransform(filePath, contents => FixSystemCollectionsImmutable(filePath, contents));
109+
}
110+
},
111+
onlyIncludePaths: new []
112+
{
113+
"System/Collections/Generic/IHashKeyCollection.cs",
114+
"System/Collections/Immutable/AllocFreeConcurrentStack.cs",
115+
"System/Collections/Immutable/DictionaryEnumerator.cs",
116+
"System/Collections/Immutable/DisposableEnumeratorAdapter_2.cs",
117+
"System/Collections/Immutable/IBinaryTree.cs",
118+
"System/Collections/Immutable/IImmutableArray.cs",
119+
"System/Collections/Immutable/IImmutableDictionary.cs",
120+
"System/Collections/Immutable/IImmutableDictionaryInternal.cs",
121+
"System/Collections/Immutable/IImmutableList.cs",
122+
"System/Collections/Immutable/IImmutableListQueries.cs",
123+
"System/Collections/Immutable/IImmutableSet.cs",
124+
"System/Collections/Immutable/IOrderedCollection.cs",
125+
"System/Collections/Immutable/IStrongEnumerable_2.cs",
126+
"System/Collections/Immutable/IStrongEnumerator_1.cs",
127+
"System/Collections/Immutable/ImmutableArray.cs",
128+
"System/Collections/Immutable/ImmutableArray_1.cs",
129+
"System/Collections/Immutable/ImmutableArray_1.Builder.cs",
130+
"System/Collections/Immutable/ImmutableArray_1.Enumerator.cs",
131+
"System/Collections/Immutable/ImmutableArray_1.Minimal.cs",
132+
"System/Collections/Immutable/ImmutableDictionary.cs",
133+
"System/Collections/Immutable/ImmutableDictionary_2.cs",
134+
"System/Collections/Immutable/ImmutableDictionary_2.Builder.cs",
135+
"System/Collections/Immutable/ImmutableDictionary_2.Comparers.cs",
136+
"System/Collections/Immutable/ImmutableDictionary_2.Enumerator.cs",
137+
"System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs",
138+
"System/Collections/Immutable/ImmutableDictionary_2.MutationInput.cs",
139+
"System/Collections/Immutable/ImmutableDictionary_2.MutationResult.cs",
140+
"System/Collections/Immutable/ImmutableExtensions.cs",
141+
"System/Collections/Immutable/ImmutableExtensions.Minimal.cs",
142+
"System/Collections/Immutable/ImmutableHashSet.cs",
143+
"System/Collections/Immutable/ImmutableHashSet_1.cs",
144+
"System/Collections/Immutable/ImmutableHashSet_1.Builder.cs",
145+
"System/Collections/Immutable/ImmutableHashSet_1.Enumerator.cs",
146+
"System/Collections/Immutable/ImmutableHashSet_1.HashBucket.cs",
147+
"System/Collections/Immutable/ImmutableHashSet_1.HashBucketByRefEqualityComparer.cs",
148+
"System/Collections/Immutable/ImmutableHashSet_1.HashBucketByValueEqualityComparer.cs",
149+
"System/Collections/Immutable/ImmutableHashSet_1.MutationInput.cs",
150+
"System/Collections/Immutable/ImmutableHashSet_1.MutationResult.cs",
151+
"System/Collections/Immutable/ImmutableHashSet_1.NodeEnumerable.cs",
152+
"System/Collections/Immutable/ImmutableList.cs",
153+
"System/Collections/Immutable/ImmutableList_1.cs",
154+
"System/Collections/Immutable/ImmutableList_1.Builder.cs",
155+
"System/Collections/Immutable/ImmutableList_1.Enumerator.cs",
156+
"System/Collections/Immutable/ImmutableList_1.Node.cs",
157+
"System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs",
158+
"System/Collections/Immutable/RefAsValueType.cs",
159+
"System/Collections/Immutable/SecureObjectPool.cs",
160+
"System/Collections/Immutable/SortedInt32KeyNode.cs",
161+
"System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs",
162+
"Validation/Requires.cs",
105163
});
106164

107165
Add(
@@ -304,6 +362,86 @@ private static string AddNullableDirectiveTransform(string filePath, string cont
304362
return content;
305363
}
306364

365+
private static string FixSystemCollectionsImmutable(string filePath, string contents)
366+
{
367+
// Strip the [DebuggerTypeProxy] attributes out
368+
contents = Regex.Replace(contents, @"^.*\[DebuggerTypeProxy\(.*\)\].*\r?\n?", "", RegexOptions.Multiline);
369+
370+
// Add additional usings which are assumed available
371+
// Find the namespace declaration
372+
var namespaceIndex = contents.IndexOf("\nnamespace ", StringComparison.Ordinal);
373+
if (namespaceIndex < 0)
374+
{
375+
return contents; // No namespace found, skip
376+
}
377+
378+
// Move to the start of the line
379+
namespaceIndex = contents.LastIndexOf('\n', namespaceIndex) + 1;
380+
381+
// Add all common using directives assumed to be available
382+
// Compiler ignores duplicates (CS0105 is suppressed in auto-generated header)
383+
// Also add nullable here tp make sure it's definitely added
384+
const string usings = "#nullable enable\n" +
385+
"using System;\n" +
386+
"using System.Collections;\n" +
387+
"using System.Collections.Generic;\n" +
388+
"using System.IO;\n" +
389+
"using System.Linq;\n" +
390+
"using System.Threading;\n" +
391+
"using System.Threading.Tasks;\n\n";
392+
393+
contents = contents.Insert(namespaceIndex, usings);
394+
395+
contents = contents
396+
.Replace("[NonVersionable]", "/* [NonVersionable] */")
397+
.Replace("[ValidatedNotNull]", string.Empty)
398+
.Replace("#nullable restore", "#nullable enable");
399+
400+
// some somewhat hacky fixes for specific issues
401+
if (string.Equals(Path.GetFileName(filePath), "ImmutableList_1.Enumerator.cs"))
402+
{
403+
contents = contents.Replace("System.Collections.IEnumerator.Current", "global::System.Collections.IEnumerator.Current");
404+
}
405+
406+
if (string.Equals(Path.GetFileName(filePath), "ImmutableList_1.cs"))
407+
{
408+
contents = contents
409+
.Replace("System.Collections.IEnumerator", "global::System.Collections.IEnumerator")
410+
.Replace("System.Collections.IEnumerable", "global::System.Collections.IEnumerable")
411+
.Replace("System.Collections.ICollection", "global::System.Collections.ICollection");
412+
}
413+
414+
if (string.Equals(Path.GetFileName(filePath), "KeysOrValuesCollectionAccessor.cs"))
415+
{
416+
// Hacky, but it works
417+
contents = contents.Replace("var sortedDictionary = this.Dictionary as ImmutableSortedDictionary<TKey, TValue>;", "var sortedDictionary = this.Dictionary as IImmutableDictionaryInternal<TKey, TValue>;");
418+
}
419+
420+
if (string.Equals(Path.GetFileName(filePath), "ImmutableList_1.Node.cs"))
421+
{
422+
contents = contents.Replace("root.AddRange(Linq.Enumerable.Select(this, converter));", "root.AddRange(global::System.Linq.Enumerable.Select(this, converter));");
423+
}
424+
425+
if (string.Equals(Path.GetFileName(filePath), "ImmutableList_1.Builder.cs"))
426+
{
427+
contents = contents.Replace("System.Threading.Interlocked.CompareExchange", "global::System.Threading.Interlocked.CompareExchange");
428+
}
429+
430+
if (string.Equals(Path.GetFileName(filePath), "ImmutableDictionary_2.Builder.cs"))
431+
{
432+
contents = contents.Replace("Threading.Interlocked.CompareExchange", "global::System.Threading.Interlocked.CompareExchange");
433+
}
434+
435+
// replace SR, hard to do generally
436+
contents = contents
437+
.Replace("""ArgumentException(SR.Format(SR.DuplicateKey, key))""", """ArgumentException("DuplicateKey" + key)""")
438+
.Replace("""KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()))""", """KeyNotFoundException("Arg_KeyNotFoundWithKey" + key.ToString())""")
439+
.Replace("""SR.Format(SR.Arg_KeyNotFoundWithKey, (object) key.ToString()));""", """key.ToString());""");
440+
contents = Regex.Replace(contents, @"SR\.(\w+)(?=\W)", "@\"$1\"");
441+
442+
return contents;
443+
}
444+
307445
private static void RewriteCsFileWithStandardTransform(string filePath, string originalNamespace, params Func<string, string, string>[] extraTransform)
308446
{
309447
if (string.Equals(Path.GetExtension(filePath), ".cs", StringComparison.OrdinalIgnoreCase))
@@ -441,14 +579,15 @@ private bool TryGetValue(string key, [NotNullWhen(true)]out JsonProperty? item)
441579
// by replacing all "public" access modifiers with "internal"
442580
return Regex.Replace(
443581
result,
444-
@"public(\s+((abstract|sealed|static|unsafe)\s+)*?(partial\s+)?(class|readonly\s+(ref\s+)?struct|struct|interface|enum|delegate))",
582+
@"public(\s+((abstract|sealed|static|unsafe)\s+)*?(readonly\s+)?(partial\s+)?(class|readonly\s+(ref\s+)?struct|struct|interface|enum|delegate))",
445583
match => $"internal{match.Groups[1]}");
446584
});
447585
}
448586
}
449587

450588
static string GenerateWarningDisablePragma() =>
451589
"#pragma warning disable " +
590+
"CS0105, " + // The using directive appeared previously
452591
"CS0618, " + // Type or member is obsolete
453592
"CS0649, " + // Field is never assigned to, and will always have its default value
454593
"CS1574, " + // XML comment has a cref attribute that could not be resolved

0 commit comments

Comments
 (0)