From 5c206a4e78888c9ed7ccb1b57c08926d73a0b792 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Thu, 22 Oct 2020 11:01:03 +0200 Subject: [PATCH 1/4] Improve annotations for XLinq classes taking params object[] --- .../src/System/Xml/Linq/XContainer.cs | 20 +++++++++---------- .../src/System/Xml/Linq/XDocument.cs | 4 ++-- .../src/System/Xml/Linq/XElement.cs | 6 +++--- .../src/System/Xml/Linq/XNode.cs | 6 +++--- .../src/System/Xml/Linq/XStreamingElement.cs | 4 ++-- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs index 325c00d90cfc91..9967f3f8869e0f 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs @@ -165,16 +165,16 @@ public void Add(object? content) AddNode(new XElement(x)); return; } - object[]? o = content as object[]; + object?[]? o = content as object?[]; if (o != null) { - foreach (object obj in o) Add(obj); + foreach (object? obj in o) Add(obj); return; } IEnumerable? e = content as IEnumerable; if (e != null) { - foreach (object obj in e) Add(obj); + foreach (object? obj in e) Add(obj); return; } AddString(GetStringValue(content)); @@ -190,7 +190,7 @@ public void Add(object? content) /// See XContainer.Add(object content) for details about the content that can be added /// using this method. /// - public void Add(params object[] content) + public void Add(params object?[] content) { Add((object)content); } @@ -229,7 +229,7 @@ public void AddFirst(object? content) /// /// Thrown if the parent is null. /// - public void AddFirst(params object[] content) + public void AddFirst(params object?[] content) { AddFirst((object)content); } @@ -452,7 +452,7 @@ public void ReplaceNodes(object? content) /// See XContainer.Add(object content) for details about the content that can be added /// using this method. /// - public void ReplaceNodes(params object[] content) + public void ReplaceNodes(params object?[] content) { ReplaceNodes((object)content); } @@ -492,16 +492,16 @@ internal void AddContentSkipNotify(object? content) AddNodeSkipNotify(new XElement(x)); return; } - object[]? o = content as object[]; + object?[]? o = content as object?[]; if (o != null) { - foreach (object obj in o) AddContentSkipNotify(obj); + foreach (object? obj in o) AddContentSkipNotify(obj); return; } IEnumerable? e = content as IEnumerable; if (e != null) { - foreach (object obj in e) AddContentSkipNotify(obj); + foreach (object? obj in e) AddContentSkipNotify(obj); return; } AddStringSkipNotify(GetStringValue(content)); @@ -1398,7 +1398,7 @@ private static void AddContentToList(List list, object content) } else { - foreach (object obj in e) + foreach (object? obj in e) { if (obj != null) AddContentToList(list, obj); } diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDocument.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDocument.cs index 5ee37a8fe6ddc1..9afbc2ba9431e3 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDocument.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDocument.cs @@ -59,7 +59,7 @@ public XDocument() /// See for details about the content that can be added /// using this method. /// - public XDocument(params object[] content) + public XDocument(params object?[] content) : this() { AddContentSkipNotify(content); @@ -87,7 +87,7 @@ public XDocument(params object[] content) /// See for details about the content that can be added /// using this method. /// - public XDocument(XDeclaration? declaration, params object[] content) + public XDocument(XDeclaration? declaration, params object?[] content) : this(content) { _declaration = declaration; diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XElement.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XElement.cs index 8b595a34e5f967..283b42a82e6cda 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XElement.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XElement.cs @@ -92,7 +92,7 @@ public XElement(XName name, object? content) /// See XContainer.Add(object content) for details about the content that can be added /// using this method. /// - public XElement(XName name, params object[] content) : this(name, (object)content) { } + public XElement(XName name, params object?[] content) : this(name, (object)content) { } /// /// Initializes a new instance of the XElement class from another XElement object. @@ -983,7 +983,7 @@ public void ReplaceAll(object? content) /// See XContainer.Add(object content) for details about the content that can be added /// using this method. /// - public void ReplaceAll(params object[] content) + public void ReplaceAll(params object?[] content) { ReplaceAll((object)content); } @@ -1020,7 +1020,7 @@ public void ReplaceAttributes(object? content) /// See XContainer.Add(object content) for details about the content that can be added /// using this method. /// - public void ReplaceAttributes(params object[] content) + public void ReplaceAttributes(params object?[] content) { ReplaceAttributes((object)content); } diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XNode.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XNode.cs index aa34604d8a05bd..389b9dc5767149 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XNode.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XNode.cs @@ -138,7 +138,7 @@ public void AddAfterSelf(object? content) /// /// Thrown if the parent is null. /// - public void AddAfterSelf(params object[] content) + public void AddAfterSelf(params object?[] content) { AddAfterSelf((object)content); } @@ -185,7 +185,7 @@ public void AddBeforeSelf(object? content) /// /// Thrown if the parent is null. /// - public void AddBeforeSelf(params object[] content) + public void AddBeforeSelf(params object?[] content) { AddBeforeSelf((object)content); } @@ -567,7 +567,7 @@ public void ReplaceWith(object? content) /// Replaces this node with the specified content. /// /// Content that replaces this node. - public void ReplaceWith(params object[] content) + public void ReplaceWith(params object?[] content) { ReplaceWith((object)content); } diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs index d8a71a9c203f7f..82c21f74cb0fe4 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs @@ -43,7 +43,7 @@ public XStreamingElement(XName name, object content) /// /// The name to assign to the new node /// An array containing content to assign to the new node - public XStreamingElement(XName name, params object[] content) + public XStreamingElement(XName name, params object?[] content) : this(name) { this.content = content; @@ -88,7 +88,7 @@ public void Add(object? content) /// Add content to an /// /// array of objects containing content to add - public void Add(params object[] content) + public void Add(params object?[] content) { Add((object)content); } From 1ac19a1fa70a722249ca15e31497a9c015c891f5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Thu, 22 Oct 2020 17:58:10 +0200 Subject: [PATCH 2/4] annotate ref --- .../ref/System.Xml.XDocument.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs b/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs index 2f0a98f962e8dd..90818a96c19e63 100644 --- a/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs +++ b/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs @@ -150,9 +150,9 @@ internal XContainer() { } public System.Xml.Linq.XNode? FirstNode { get { throw null; } } public System.Xml.Linq.XNode? LastNode { get { throw null; } } public void Add(object? content) { } - public void Add(params object[] content) { } + public void Add(params object?[] content) { } public void AddFirst(object? content) { } - public void AddFirst(params object[] content) { } + public void AddFirst(params object?[] content) { } public System.Xml.XmlWriter CreateWriter() { throw null; } public System.Collections.Generic.IEnumerable DescendantNodes() { throw null; } public System.Collections.Generic.IEnumerable Descendants() { throw null; } @@ -163,7 +163,7 @@ public void AddFirst(params object[] content) { } public System.Collections.Generic.IEnumerable Nodes() { throw null; } public void RemoveNodes() { } public void ReplaceNodes(object? content) { } - public void ReplaceNodes(params object[] content) { } + public void ReplaceNodes(params object?[] content) { } } public partial class XDeclaration { @@ -177,8 +177,8 @@ public XDeclaration(System.Xml.Linq.XDeclaration other) { } public partial class XDocument : System.Xml.Linq.XContainer { public XDocument() { } - public XDocument(params object[] content) { } - public XDocument(System.Xml.Linq.XDeclaration? declaration, params object[] content) { } + public XDocument(params object?[] content) { } + public XDocument(System.Xml.Linq.XDeclaration? declaration, params object?[] content) { } public XDocument(System.Xml.Linq.XDocument other) { } public System.Xml.Linq.XDeclaration? Declaration { get { throw null; } set { } } public System.Xml.Linq.XDocumentType? DocumentType { get { throw null; } } @@ -229,7 +229,7 @@ public partial class XElement : System.Xml.Linq.XContainer, System.Xml.Serializa public XElement(System.Xml.Linq.XElement other) { } public XElement(System.Xml.Linq.XName name) { } public XElement(System.Xml.Linq.XName name, object? content) { } - public XElement(System.Xml.Linq.XName name, params object[] content) { } + public XElement(System.Xml.Linq.XName name, params object?[] content) { } public XElement(System.Xml.Linq.XStreamingElement other) { } public static System.Collections.Generic.IEnumerable EmptySequence { get { throw null; } } public System.Xml.Linq.XAttribute? FirstAttribute { get { throw null; } } @@ -330,9 +330,9 @@ public XElement(System.Xml.Linq.XStreamingElement other) { } public void RemoveAll() { } public void RemoveAttributes() { } public void ReplaceAll(object? content) { } - public void ReplaceAll(params object[] content) { } + public void ReplaceAll(params object?[] content) { } public void ReplaceAttributes(object? content) { } - public void ReplaceAttributes(params object[] content) { } + public void ReplaceAttributes(params object?[] content) { } public void Save(System.IO.Stream stream) { } public void Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options) { } public void Save(System.IO.TextWriter textWriter) { } @@ -398,9 +398,9 @@ internal XNode() { } public System.Xml.Linq.XNode? NextNode { get { throw null; } } public System.Xml.Linq.XNode? PreviousNode { get { throw null; } } public void AddAfterSelf(object? content) { } - public void AddAfterSelf(params object[] content) { } + public void AddAfterSelf(params object?[] content) { } public void AddBeforeSelf(object? content) { } - public void AddBeforeSelf(params object[] content) { } + public void AddBeforeSelf(params object?[] content) { } public System.Collections.Generic.IEnumerable Ancestors() { throw null; } public System.Collections.Generic.IEnumerable Ancestors(System.Xml.Linq.XName? name) { throw null; } public static int CompareDocumentOrder(System.Xml.Linq.XNode? n1, System.Xml.Linq.XNode? n2) { throw null; } @@ -419,7 +419,7 @@ public void AddBeforeSelf(params object[] content) { } public static System.Threading.Tasks.Task ReadFromAsync(System.Xml.XmlReader reader, System.Threading.CancellationToken cancellationToken) { throw null; } public void Remove() { } public void ReplaceWith(object? content) { } - public void ReplaceWith(params object[] content) { } + public void ReplaceWith(params object?[] content) { } public override string ToString() { throw null; } public string ToString(System.Xml.Linq.SaveOptions options) { throw null; } public abstract void WriteTo(System.Xml.XmlWriter writer); @@ -489,10 +489,10 @@ public partial class XStreamingElement { public XStreamingElement(System.Xml.Linq.XName name) { } public XStreamingElement(System.Xml.Linq.XName name, object content) { } - public XStreamingElement(System.Xml.Linq.XName name, params object[] content) { } + public XStreamingElement(System.Xml.Linq.XName name, params object?[] content) { } public System.Xml.Linq.XName Name { get { throw null; } set { } } public void Add(object? content) { } - public void Add(params object[] content) { } + public void Add(params object?[] content) { } public void Save(System.IO.Stream stream) { } public void Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options) { } public void Save(System.IO.TextWriter textWriter) { } From f4008376c1143ecb31aa3796b0aff2b469480780 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Fri, 23 Oct 2020 11:34:55 +0200 Subject: [PATCH 3/4] address Jozkee's feedback --- .../System.Private.Xml.Linq/src/System/Xml/Linq/XLinq.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XLinq.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XLinq.cs index 0d47b5596cdc4a..b27a85a49ea154 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XLinq.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XLinq.cs @@ -95,16 +95,16 @@ private void AddContent(object? content) AddNode(new XElement(x)); return; } - object[]? o = content as object[]; + object?[]? o = content as object?[]; if (o != null) { - foreach (object obj in o) AddContent(obj); + foreach (object? obj in o) AddContent(obj); return; } IEnumerable? e = content as IEnumerable; if (e != null) { - foreach (object obj in e) AddContent(obj); + foreach (object? obj in e) AddContent(obj); return; } if (content is XAttribute) throw new ArgumentException(SR.Argument_AddAttribute); From 4889c0ef44423c353040747c6235f4adef7a5fcb Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Mon, 26 Oct 2020 12:11:26 +0100 Subject: [PATCH 4/4] Fix XStreamingElement ctor to take nullable content --- .../System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs | 4 ++-- .../src/System/Xml/Linq/XStreamingElement.cs | 4 ++-- .../System.Xml.XDocument/ref/System.Xml.XDocument.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs index 9967f3f8869e0f..1a0d8492fd2fe2 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs @@ -1389,7 +1389,7 @@ internal async Task WriteContentToAsync(XmlWriter writer, CancellationToken canc } } - private static void AddContentToList(List list, object content) + private static void AddContentToList(List list, object? content) { IEnumerable? e = content is string ? null : content as IEnumerable; if (e == null) @@ -1409,7 +1409,7 @@ private static void AddContentToList(List list, object content) internal static object? GetContentSnapshot(object? content) { if (content is string || !(content is IEnumerable)) return content; - List list = new List(); + List list = new List(); AddContentToList(list, content); return list; } diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs index 82c21f74cb0fe4..4e9373f031b1c6 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XStreamingElement.cs @@ -32,10 +32,10 @@ public XStreamingElement(XName name) /// /// The name to assign to the new node /// The content to assign to the new node - public XStreamingElement(XName name, object content) + public XStreamingElement(XName name, object? content) : this(name) { - this.content = content is List ? new object[] { content } : content; + this.content = content is List ? new object?[] { content } : content; } /// diff --git a/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs b/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs index 90818a96c19e63..5849d80eb2d5b7 100644 --- a/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs +++ b/src/libraries/System.Xml.XDocument/ref/System.Xml.XDocument.cs @@ -488,7 +488,7 @@ public override void WriteTo(System.Xml.XmlWriter writer) { } public partial class XStreamingElement { public XStreamingElement(System.Xml.Linq.XName name) { } - public XStreamingElement(System.Xml.Linq.XName name, object content) { } + public XStreamingElement(System.Xml.Linq.XName name, object? content) { } public XStreamingElement(System.Xml.Linq.XName name, params object?[] content) { } public System.Xml.Linq.XName Name { get { throw null; } set { } } public void Add(object? content) { }