Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ public void GetDiagnosticsFromNullToken()
token.GetDiagnostics().Verify();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40773")]
public void ConstructedSyntaxTrivia_NoLocationAndDiagnostics()
{
var trivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ");
Assert.Equivalent(Location.None, trivia.GetLocation());
trivia.GetDiagnostics().Verify();
}

[Fact]
[WorkItem(21231, "https://github.com/dotnet/roslyn/issues/21231")]
public void TestSpacingOnNullableIntType()
Expand Down
24 changes: 20 additions & 4 deletions src/Compilers/Core/Portable/Syntax/SyntaxTrivia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -409,8 +411,7 @@ public SyntaxTree? SyntaxTree
/// </summary>
public Location GetLocation()
{
// https://github.com/dotnet/roslyn/issues/40773
return this.SyntaxTree!.GetLocation(this.Span);
return this.SyntaxTree?.GetLocation(this.Span) ?? Location.None;
}

/// <summary>
Expand All @@ -420,8 +421,23 @@ public Location GetLocation()
/// </summary>
public IEnumerable<Diagnostic> GetDiagnostics()
{
// https://github.com/dotnet/roslyn/issues/40773
return this.SyntaxTree!.GetDiagnostics(this);
if (UnderlyingNode is null)
{
return SpecializedCollections.EmptyEnumerable<Diagnostic>();
}

if (this.SyntaxTree is { } syntaxTree)
{
return syntaxTree.GetDiagnostics(this);
}
else
{
var diagnostics = UnderlyingNode.GetDiagnostics();

return diagnostics.Length == 0
? SpecializedCollections.EmptyEnumerable<Diagnostic>()
: diagnostics.Select(Diagnostic.Create);
}
}

/// <summary>
Expand Down