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

Enable nullability, round 3 of x #525

Merged
merged 4 commits into from
Mar 11, 2021
Merged

Enable nullability, round 3 of x #525

merged 4 commits into from
Mar 11, 2021

Conversation

iamcarbon
Copy link
Contributor

Another big chunk of nullability annotations

@iamcarbon
Copy link
Contributor Author

Ready for review.

Comment on lines +33 to +34
this.writer = writer;
this.writer.NewLine = "\n";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
this.writer = writer;
this.writer.NewLine = "\n";
Writer = writer;

We don't gain anything by duplicating this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly, the analyzer doesn't follow the setter through and mark the writer field as non-null if we do this. We could set a MemberNotNull attribute and throw when setting the writer, but we also double up some logic if we do this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunate :/
This would be a fine place for MemberNotNull, but since we'd have to add a dummy class just for this, it's fine as is.

}

public void Sort(Comparison<Block> comparison)
{
if (comparison == null) ThrowHelper.ArgumentNullException(nameof(comparison));
Array.Sort(children, 0, Count, new BlockComparer(comparison));
Array.Sort(children, 0, Count, new BlockComparer(comparison)!);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be needed right after the new

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 backed up out making the comparison delegate nullable, so these suppressions are no longer needed.

Comment on lines +309 to 312
public int Compare(Block? x, Block? y)
{
return comparison(x, y);
return comparison(x!, y!);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should either accept non-nullable blocks, or the comparer should allow nullable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an annoying case where the nullability contract of IComparer (below) forces us to define these as nullable.

int Compare([AllowNull] T x, [AllowNull] T y);

The System.Comparison delegate, sadly, does NOT define the system contract with nullable attributes. Our options are limited here since we don't control either implementation / contract.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

UPDATE: we can make the comparison explicitly accept nullable. But this forces us to suppress its use in Array.Sort -- and we don't actually have a case to compare against null. IComparer, regardless, requires us to accept null -- so I believe this is a valid case for suppression. We could also make this class oblivious.

Copy link
Collaborator

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@xoofx xoofx merged commit 6b433d9 into xoofx:master Mar 11, 2021
@xoofx
Copy link
Owner

xoofx commented Mar 11, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants