diff --git a/src/Compilers/Core/Portable/Compilation/Compilation.cs b/src/Compilers/Core/Portable/Compilation/Compilation.cs index 3b5abaaa8587a..dae90c6a5c695 100644 --- a/src/Compilers/Core/Portable/Compilation/Compilation.cs +++ b/src/Compilers/Core/Portable/Compilation/Compilation.cs @@ -3000,7 +3000,7 @@ internal void MarkImportDirectiveAsUsed(SyntaxNode node) MarkImportDirectiveAsUsed(node.SyntaxTree, node.Span.Start); } - internal void MarkImportDirectiveAsUsed(SyntaxTree syntaxTree, int position) + internal void MarkImportDirectiveAsUsed(SyntaxTree? syntaxTree, int position) { // Optimization: Don't initialize TreeToUsedImportDirectivesMap in submissions. if (!IsSubmission && syntaxTree != null) diff --git a/src/Compilers/Core/Portable/Syntax/InternalSyntax/SyntaxList.WithManyChildren.cs b/src/Compilers/Core/Portable/Syntax/InternalSyntax/SyntaxList.WithManyChildren.cs index 1d9d6dfb56422..51969c86a24ca 100644 --- a/src/Compilers/Core/Portable/Syntax/InternalSyntax/SyntaxList.WithManyChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/InternalSyntax/SyntaxList.WithManyChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Diagnostics; using Roslyn.Utilities; @@ -86,7 +88,7 @@ internal override void CopyTo(ArrayElement[] array, int offset) Array.Copy(this.children, 0, array, offset, this.children.Length); } - internal override SyntaxNode CreateRed(SyntaxNode parent, int position) + internal override SyntaxNode CreateRed(SyntaxNode? parent, int position) { var separated = this.SlotCount > 1 && HasNodeTokenPattern(); if (parent != null && parent.ShouldCreateWeakList()) diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyChildren.cs index 76c0555fc46b0..697fd45756859 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; namespace Microsoft.CodeAnalysis.Syntax @@ -8,15 +10,15 @@ internal partial class SyntaxList { internal class SeparatedWithManyChildren : SyntaxList { - private readonly ArrayElement[] _children; + private readonly ArrayElement[] _children; - internal SeparatedWithManyChildren(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) + internal SeparatedWithManyChildren(InternalSyntax.SyntaxList green, SyntaxNode? parent, int position) : base(green, parent, position) { - _children = new ArrayElement[(green.SlotCount + 1) >> 1]; + _children = new ArrayElement[(green.SlotCount + 1) >> 1]; } - internal override SyntaxNode GetNodeSlot(int i) + internal override SyntaxNode? GetNodeSlot(int i) { if ((i & 1) != 0) { @@ -27,7 +29,7 @@ internal override SyntaxNode GetNodeSlot(int i) return this.GetRedElement(ref _children[i >> 1].Value, i); } - internal override SyntaxNode GetCachedSlot(int i) + internal override SyntaxNode? GetCachedSlot(int i) { if ((i & 1) != 0) { diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyWeakChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyWeakChildren.cs index 142df6cdb7a37..4db286fdfb442 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyWeakChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.SeparatedWithManyWeakChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; namespace Microsoft.CodeAnalysis.Syntax @@ -8,17 +10,17 @@ internal partial class SyntaxList { internal class SeparatedWithManyWeakChildren : SyntaxList { - private readonly ArrayElement>[] _children; + private readonly ArrayElement?>[] _children; internal SeparatedWithManyWeakChildren(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) : base(green, parent, position) { - _children = new ArrayElement>[(((green.SlotCount + 1) >> 1) - 1)]; + _children = new ArrayElement?>[(((green.SlotCount + 1) >> 1) - 1)]; } - internal override SyntaxNode GetNodeSlot(int i) + internal override SyntaxNode? GetNodeSlot(int i) { - SyntaxNode result = null; + SyntaxNode? result = null; if ((i & 1) == 0) { @@ -29,9 +31,9 @@ internal override SyntaxNode GetNodeSlot(int i) return result; } - internal override SyntaxNode GetCachedSlot(int i) + internal override SyntaxNode? GetCachedSlot(int i) { - SyntaxNode result = null; + SyntaxNode? result = null; if ((i & 1) == 0) { diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyChildren.cs index 3b2617ef6d23d..810dca48d63b1 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; namespace Microsoft.CodeAnalysis.Syntax @@ -8,20 +10,20 @@ internal partial class SyntaxList { internal class WithManyChildren : SyntaxList { - private readonly ArrayElement[] _children; + private readonly ArrayElement[] _children; - internal WithManyChildren(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) + internal WithManyChildren(InternalSyntax.SyntaxList green, SyntaxNode? parent, int position) : base(green, parent, position) { - _children = new ArrayElement[green.SlotCount]; + _children = new ArrayElement[green.SlotCount]; } - internal override SyntaxNode GetNodeSlot(int index) + internal override SyntaxNode? GetNodeSlot(int index) { return this.GetRedElement(ref _children[index].Value, index); } - internal override SyntaxNode GetCachedSlot(int index) + internal override SyntaxNode? GetCachedSlot(int index) { return _children[index]; } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyWeakChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyWeakChildren.cs index a5f81751ee44b..a4484bf8b27e6 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyWeakChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithManyWeakChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Threading; @@ -9,7 +11,7 @@ internal partial class SyntaxList { internal class WithManyWeakChildren : SyntaxList { - private readonly ArrayElement>[] _children; + private readonly ArrayElement?>[] _children; // We calculate and store the positions of all children here. This way, getting the position // of all children is O(N) [N being the list size], otherwise it is O(N^2) because getting @@ -20,7 +22,7 @@ internal WithManyWeakChildren(InternalSyntax.SyntaxList.WithManyChildrenBase gre : base(green, parent, position) { int count = green.SlotCount; - _children = new ArrayElement>[count]; + _children = new ArrayElement?>[count]; var childOffsets = new int[count]; @@ -45,9 +47,9 @@ internal override SyntaxNode GetNodeSlot(int index) return GetWeakRedElement(ref _children[index].Value, index); } - internal override SyntaxNode GetCachedSlot(int index) + internal override SyntaxNode? GetCachedSlot(int index) { - SyntaxNode value = null; + SyntaxNode? value = null; _children[index].Value?.TryGetTarget(out value); return value; } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithThreeChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithThreeChildren.cs index 6c24aac1dcc33..92067f63cd8ba 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithThreeChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithThreeChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; namespace Microsoft.CodeAnalysis.Syntax @@ -8,16 +10,16 @@ internal partial class SyntaxList { internal class WithThreeChildren : SyntaxList { - private SyntaxNode _child0; - private SyntaxNode _child1; - private SyntaxNode _child2; + private SyntaxNode? _child0; + private SyntaxNode? _child1; + private SyntaxNode? _child2; internal WithThreeChildren(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) : base(green, parent, position) { } - internal override SyntaxNode GetNodeSlot(int index) + internal override SyntaxNode? GetNodeSlot(int index) { switch (index) { @@ -32,7 +34,7 @@ internal override SyntaxNode GetNodeSlot(int index) } } - internal override SyntaxNode GetCachedSlot(int index) + internal override SyntaxNode? GetCachedSlot(int index) { switch (index) { diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithTwoChildren.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithTwoChildren.cs index ec83d05220d5b..ef6ea136a908e 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.WithTwoChildren.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.WithTwoChildren.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections.Generic; using Roslyn.Utilities; @@ -10,15 +12,15 @@ internal partial class SyntaxList { internal class WithTwoChildren : SyntaxList { - private SyntaxNode _child0; - private SyntaxNode _child1; + private SyntaxNode? _child0; + private SyntaxNode? _child1; internal WithTwoChildren(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) : base(green, parent, position) { } - internal override SyntaxNode GetNodeSlot(int index) + internal override SyntaxNode? GetNodeSlot(int index) { switch (index) { @@ -31,7 +33,7 @@ internal override SyntaxNode GetNodeSlot(int index) } } - internal override SyntaxNode GetCachedSlot(int index) + internal override SyntaxNode? GetCachedSlot(int index) { switch (index) { diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList.cs index 1d3c750f39ef5..ace2bbad3c261 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections.Generic; using Roslyn.Utilities; @@ -8,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Syntax { internal abstract partial class SyntaxList : SyntaxNode { - internal SyntaxList(InternalSyntax.SyntaxList green, SyntaxNode parent, int position) + internal SyntaxList(InternalSyntax.SyntaxList green, SyntaxNode? parent, int position) : base(green, parent, position) { } @@ -21,9 +23,10 @@ public override string Language } } - protected override SyntaxTree SyntaxTreeCore => this.Parent.SyntaxTree; + // https://github.com/dotnet/roslyn/issues/40733 + protected override SyntaxTree SyntaxTreeCore => this.Parent!.SyntaxTree; - protected internal override SyntaxNode ReplaceCore(IEnumerable nodes = null, Func computeReplacementNode = null, IEnumerable tokens = null, Func computeReplacementToken = null, IEnumerable trivia = null, Func computeReplacementTrivia = null) + protected internal override SyntaxNode ReplaceCore(IEnumerable? nodes = null, Func? computeReplacementNode = null, IEnumerable? tokens = null, Func? computeReplacementToken = null, IEnumerable? trivia = null, Func? computeReplacementTrivia = null) { throw ExceptionUtilities.Unreachable; } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder.cs b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder.cs index 4f383a8d8441e..0da31b0e9b2b1 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Diagnostics; @@ -7,12 +9,12 @@ namespace Microsoft.CodeAnalysis.Syntax { internal class SyntaxListBuilder { - private ArrayElement[] _nodes; + private ArrayElement[] _nodes; public int Count { get; private set; } public SyntaxListBuilder(int size) { - _nodes = new ArrayElement[size]; + _nodes = new ArrayElement[size]; } public void Clear() @@ -32,7 +34,7 @@ internal void AddInternal(GreenNode item) throw new ArgumentNullException(); } - if (_nodes == null || Count >= _nodes.Length) + if (Count >= _nodes.Length) { this.Grow(Count == 0 ? 8 : _nodes.Length * 2); } @@ -47,7 +49,7 @@ public void AddRange(SyntaxNode[] items) public void AddRange(SyntaxNode[] items, int offset, int length) { - if (_nodes == null || Count + length > _nodes.Length) + if (Count + length > _nodes.Length) { this.Grow(Count + length); } @@ -81,7 +83,7 @@ public void AddRange(SyntaxList list) public void AddRange(SyntaxList list, int offset, int count) { - if (_nodes == null || this.Count + count > _nodes.Length) + if (this.Count + count > _nodes.Length) { this.Grow(Count + count); } @@ -89,7 +91,7 @@ public void AddRange(SyntaxList list, int offset, int count) var dst = this.Count; for (int i = offset, limit = offset + count; i < limit; i++) { - _nodes[dst].Value = list.ItemInternal(i).Green; + _nodes[dst].Value = list.ItemInternal(i)!.Green; dst++; } @@ -115,7 +117,7 @@ public void AddRange(SyntaxNodeOrTokenList list) public void AddRange(SyntaxNodeOrTokenList list, int offset, int count) { - if (_nodes == null || this.Count + count > _nodes.Length) + if (this.Count + count > _nodes.Length) { this.Grow(Count + count); } @@ -144,7 +146,7 @@ public void AddRange(SyntaxTokenList list, int offset, int length) private void Grow(int size) { - var tmp = new ArrayElement[size]; + var tmp = new ArrayElement[size]; Array.Copy(_nodes, tmp, _nodes.Length); _nodes = tmp; } @@ -153,7 +155,7 @@ public bool Any(int kind) { for (int i = 0; i < Count; i++) { - if (_nodes[i].Value.RawKind == kind) + if (_nodes[i].Value!.RawKind == kind) { return true; } @@ -162,7 +164,7 @@ public bool Any(int kind) return false; } - internal GreenNode ToListNode() + internal GreenNode? ToListNode() { switch (this.Count) { @@ -175,7 +177,7 @@ internal GreenNode ToListNode() case 3: return InternalSyntax.SyntaxList.List(_nodes[0].Value, _nodes[1].Value, _nodes[2].Value); default: - var tmp = new ArrayElement[this.Count]; + var tmp = new ArrayElement[this.Count]; for (int i = 0; i < this.Count; i++) { tmp[i].Value = _nodes[i].Value; @@ -198,7 +200,7 @@ public static implicit operator SyntaxList(SyntaxListBuilder builder internal void RemoveLast() { this.Count -= 1; - this._nodes[Count] = default(ArrayElement); + this._nodes[Count] = default; } } } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilderExtensions.cs b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilderExtensions.cs index 1ed5ee6854389..ba508eb49630e 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilderExtensions.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilderExtensions.cs @@ -1,10 +1,12 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + namespace Microsoft.CodeAnalysis.Syntax { internal static class SyntaxListBuilderExtensions { - public static SyntaxTokenList ToTokenList(this SyntaxListBuilder builder) + public static SyntaxTokenList ToTokenList(this SyntaxListBuilder? builder) { if (builder == null || builder.Count == 0) { @@ -14,35 +16,38 @@ public static SyntaxTokenList ToTokenList(this SyntaxListBuilder builder) return new SyntaxTokenList(null, builder.ToListNode(), 0, 0); } - public static SyntaxList ToList(this SyntaxListBuilder builder) + public static SyntaxList ToList(this SyntaxListBuilder? builder) { - if (builder == null || builder.Count == 0) + var listNode = builder?.ToListNode(); + if (listNode is null) { - return default(SyntaxList); + return default; } - return new SyntaxList(builder.ToListNode().CreateRed()); + return new SyntaxList(listNode.CreateRed()); } - public static SyntaxList ToList(this SyntaxListBuilder builder) + public static SyntaxList ToList(this SyntaxListBuilder? builder) where TNode : SyntaxNode { - if (builder == null || builder.Count == 0) + var listNode = builder?.ToListNode(); + if (listNode is null) { - return new SyntaxList(); + return default; } - return new SyntaxList(builder.ToListNode().CreateRed()); + return new SyntaxList(listNode.CreateRed()); } - public static SeparatedSyntaxList ToSeparatedList(this SyntaxListBuilder builder) where TNode : SyntaxNode + public static SeparatedSyntaxList ToSeparatedList(this SyntaxListBuilder? builder) where TNode : SyntaxNode { - if (builder == null || builder.Count == 0) + var listNode = builder?.ToListNode(); + if (listNode is null) { - return default(SeparatedSyntaxList); + return default; } - return new SeparatedSyntaxList(new SyntaxNodeOrTokenList(builder.ToListNode().CreateRed(), 0)); + return new SeparatedSyntaxList(new SyntaxNodeOrTokenList(listNode.CreateRed(), 0)); } } } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder`1.cs b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder`1.cs index 608f5949267eb..6348741935403 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder`1.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxListBuilder`1.cs @@ -1,10 +1,12 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + namespace Microsoft.CodeAnalysis.Syntax { internal struct SyntaxListBuilder where TNode : SyntaxNode { - private readonly SyntaxListBuilder _builder; + private readonly SyntaxListBuilder? _builder; public SyntaxListBuilder(int size) : this(new SyntaxListBuilder(size)) @@ -16,7 +18,7 @@ public static SyntaxListBuilder Create() return new SyntaxListBuilder(8); } - internal SyntaxListBuilder(SyntaxListBuilder builder) + internal SyntaxListBuilder(SyntaxListBuilder? builder) { _builder = builder; } @@ -33,39 +35,39 @@ public int Count { get { - return _builder.Count; + return _builder!.Count; } } public void Clear() { - _builder.Clear(); + _builder!.Clear(); } public SyntaxListBuilder Add(TNode node) { - _builder.Add(node); + _builder!.Add(node); return this; } public void AddRange(TNode[] items, int offset, int length) { - _builder.AddRange(items, offset, length); + _builder!.AddRange(items, offset, length); } public void AddRange(SyntaxList nodes) { - _builder.AddRange(nodes); + _builder!.AddRange(nodes); } public void AddRange(SyntaxList nodes, int offset, int length) { - _builder.AddRange(nodes, offset, length); + _builder!.AddRange(nodes, offset, length); } public bool Any(int kind) { - return _builder.Any(kind); + return _builder!.Any(kind); } public SyntaxList ToList() @@ -73,7 +75,7 @@ public SyntaxList ToList() return _builder.ToList(); } - public static implicit operator SyntaxListBuilder(SyntaxListBuilder builder) + public static implicit operator SyntaxListBuilder?(SyntaxListBuilder builder) { return builder._builder; } @@ -85,7 +87,7 @@ public static implicit operator SyntaxList(SyntaxListBuilder build return builder.ToList(); } - return default(SyntaxList); + return default; } } } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList`1.Enumerator.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList`1.Enumerator.cs index 37f42a328a6fd..aee40bc166f91 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList`1.Enumerator.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList`1.Enumerator.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections; using System.Collections.Generic; @@ -37,7 +39,7 @@ public TNode Current { get { - return (TNode)_list.ItemInternal(_index); + return (TNode)_list.ItemInternal(_index)!; } } diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxList`1.cs b/src/Compilers/Core/Portable/Syntax/SyntaxList`1.cs index f121cbb00c0b6..e4f5096e1260f 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxList`1.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxList`1.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections; using System.Collections.Generic; @@ -17,9 +19,9 @@ namespace Microsoft.CodeAnalysis public readonly partial struct SyntaxList : IReadOnlyList, IEquatable> where TNode : SyntaxNode { - private readonly SyntaxNode _node; + private readonly SyntaxNode? _node; - internal SyntaxList(SyntaxNode node) + internal SyntaxList(SyntaxNode? node) { _node = node; } @@ -28,8 +30,8 @@ internal SyntaxList(SyntaxNode node) /// Creates a singleton list of syntax nodes. /// /// The single element node. - public SyntaxList(TNode node) - : this((SyntaxNode)node) + public SyntaxList(TNode? node) + : this((SyntaxNode?)node) { } @@ -37,12 +39,12 @@ public SyntaxList(TNode node) /// Creates a list of syntax nodes. /// /// A sequence of element nodes. - public SyntaxList(IEnumerable nodes) + public SyntaxList(IEnumerable? nodes) : this(CreateNode(nodes)) { } - private static SyntaxNode CreateNode(IEnumerable nodes) + private static SyntaxNode? CreateNode(IEnumerable? nodes) { if (nodes == null) { @@ -60,7 +62,7 @@ private static SyntaxNode CreateNode(IEnumerable nodes) return builder.ToList().Node; } - internal SyntaxNode Node + internal SyntaxNode? Node { get { @@ -94,7 +96,7 @@ public TNode this[int index] { if (unchecked((uint)index < (uint)_node.SlotCount)) { - return (TNode)_node.GetNodeSlot(index); + return (TNode)_node.GetNodeSlot(index)!; } } else if (index == 0) @@ -102,13 +104,13 @@ public TNode this[int index] return (TNode)_node; } } - throw new ArgumentOutOfRangeException(); + throw new ArgumentOutOfRangeException(nameof(index)); } } - internal SyntaxNode ItemInternal(int index) + internal SyntaxNode? ItemInternal(int index) { - if (_node.IsList) + if (_node?.IsList == true) { return _node.GetNodeSlot(index); } @@ -340,7 +342,7 @@ public TNode First() /// /// The first node in the list or default if the list is empty. /// - public TNode FirstOrDefault() + public TNode? FirstOrDefault() { if (this.Any()) { @@ -363,7 +365,7 @@ public TNode Last() /// /// The last node in the list or default if the list is empty. /// - public TNode LastOrDefault() + public TNode? LastOrDefault() { if (this.Any()) { diff --git a/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs b/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs index b478454ce3870..95ec8e5510299 100644 --- a/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs +++ b/src/Compilers/Core/Portable/Syntax/SyntaxNode.cs @@ -643,6 +643,7 @@ public virtual SyntaxNodeOrToken ChildThatContainsPosition(int position) /// /// Gets node at given node index. /// This WILL force node creation if node has not yet been created. + /// Can still return null for invalid slot numbers /// internal abstract SyntaxNode? GetNodeSlot(int slot); @@ -1267,8 +1268,8 @@ protected virtual bool EquivalentToCore(SyntaxNode other) } /// - /// Returns SyntaxTree that owns the node or null if node does not belong to a - /// SyntaxTree + /// Returns SyntaxTree that owns the node. If the node does not belong to a tree then + /// one will be generated. /// protected abstract SyntaxTree SyntaxTreeCore { get; }