Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some AssumeNotNull assumptions #10901

Merged
merged 4 commits into from
Sep 19, 2024
Merged
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
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Razor;
using Microsoft.CodeAnalysis.Text;

namespace Roslyn.LanguageServer.Protocol;
Expand Down Expand Up @@ -30,6 +29,5 @@ public static TextChange GetTextChange(this SourceText text, TextEdit edit)
=> new(text.GetTextSpan(edit.Range), edit.NewText);

public static TextEdit GetTextEdit(this SourceText text, TextChange change)
=> RoslynLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText.AssumeNotNull());

=> RoslynLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText ?? "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""

Nit: string.Empty? not sure if it makes any difference...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, it does make a difference, but an absurdly small one. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it didn't make a difference after .NET Framework 2.0 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it definitely made a difference in .NET Framework 2.0. There was actually effort to inline string.Empty for empty string literals in the JIT more aggressively post .NET Framework 4.0. I know that because it caused a bug that @jasonmalinowski and I ran into where the value of string.Empty could become corrupted by the COM marshaller if strings were annotated incorrectly (e.g. BStr instead of LPWStr).

Crazy days.

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Text;

Expand Down Expand Up @@ -46,5 +45,5 @@ public static TextChange GetTextChange(this SourceText text, TextEdit edit)
=> new(text.GetTextSpan(edit.Range), edit.NewText);

public static TextEdit GetTextEdit(this SourceText text, TextChange change)
=> VsLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText.AssumeNotNull());
=> VsLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText ?? "");
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,12 @@ private static Entry ComputeNewEntry(Entry originalEntry, IUpdateProjectAction a

case CloseDocumentAction(var textLoader):
{
documentState.AssumeNotNull();
// If the document being closed has already been removed from the project then we no-op
if (documentState is null)
{
return originalEntry;
}

var state = originalEntry.State.WithChangedHostDocument(
documentState.HostDocument,
() => textLoader.LoadTextAndVersionAsync(s_loadTextOptions, cancellationToken: default));
Expand Down Expand Up @@ -562,7 +567,11 @@ private static Entry ComputeNewEntry(Entry originalEntry, IUpdateProjectAction a

case DocumentTextChangedAction(var sourceText):
{
documentState.AssumeNotNull();
// If the document being changed has already been removed from the project then we no-op
if (documentState is null)
{
return originalEntry;
}

if (documentState.TryGetText(out var olderText) &&
documentState.TryGetTextVersion(out var olderVersion))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,25 +1034,6 @@ public async Task UpdateDocument_DocumentVersionUpdated()
Assert.Equal(2, latestVersion);
}

[Fact]
public async Task UpdateDocument_ThrowsForUnknownDocument()
{
// Arrange
const string ProjectFilePath = "C:/path/to/project.csproj";
const string IntermediateOutputPath = "C:/path/to/obj";
const string RootNamespace = "TestRootNamespace";
const string DocumentFilePath = "C:/path/to/document.cshtml";

await _projectService.AddProjectAsync(
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);

// Act
await Assert.ThrowsAnyAsync<InvalidOperationException>(() =>
{
return _projectService.UpdateDocumentAsync(DocumentFilePath, s_emptyText.Replace(0, 0, "Hello World"), DisposalToken);
});
}

[Fact]
public async Task AddProject_AddsProjectWithDefaultConfiguration()
{
Expand Down
Loading