Skip to content

TextChange.NewText has an unexpected nullable annotation #85746

Description

@jasonmalinowski

This member:

public string? NewText { get; }

is marked as nullable, but there's no clear indication what we intend by that. The constructor throws if you try to pass null, so it seems like it's only allowing null to handle the case of a default-constructed TextChange. SourceText.WithChanges has some code to handle that being null and treat that as an empty string. (But if the only way you can get this is a default-constructed struct, then what's the point since you're replacing nothing with nothing?)

But some other code has odd asserts that we expect non-null values:

public static implicit operator TextChangeRange(TextChange change)
{
Debug.Assert(change.NewText is object);
return new TextChangeRange(change.Span, change.NewText.Length);
}

But not if you call this other method that should do the same thing!

public static TextChangeRange ToTextChangeRange(this TextChange textChange)
{
return new TextChangeRange(textChange.Span, textChange.NewText?.Length ?? 0);
}

If the intent of the nullable annotation was to ensure we're "safe" in regards to default constructed structs, it seems like in practice this didn't result in good safety.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersConcept-APIThis issue involves adding, removing, clarification, or modification of an API.Concept-Design DebtEngineering Debt, Design Debt, or poor product code qualityConcept-Null AnnotationsThe issue involves annotating an API for nullable reference types

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions