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

Added notes about interpolated strings in nullable flow analysis #5550

Merged
merged 2 commits into from
Jan 31, 2022

Conversation

333fred
Copy link
Member

@333fred 333fred commented Dec 15, 2021

Followup from dotnet/roslyn#57780.
Relates to test plan dotnet/roslyn#51499

@333fred 333fred requested a review from a team as a code owner December 15, 2021 22:33
@333fred
Copy link
Member Author

333fred commented Dec 15, 2021

@AlekseyTs @RikkiGibson for a look. This is documenting the limitations around nullable flow analysis from the roslyn PR.

@RikkiGibson RikkiGibson self-assigned this Jan 10, 2022
types to inform generic type inference for type parameters in the containing method. An example of where this can have an impact is:

```cs
string s = """";
Copy link
Contributor

Choose a reason for hiding this comment

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

Was it intended to have doubled quote marks here and below? This looks like it might have been left over from pasting in a test.

```cs
string s = """";
C c = new C();
c.M(s, $"""", c.ToString(), s.ToString()); // No warnings on c.ToString() or s.ToString(), as the `MaybeNull` does not flow back.
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't expect [MaybeNull] to have any impact here (interpolated strings are no). That attribute should only be impacting the input position, not the output position. What am I missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

AllowNull is for inputs, MaybeNull is for outputs.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, seems odd for that to have value in non-ref parameters. That isn't an output

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider Assert.NotNull. That will use a NotNull attribute on a by-val parameter and throw/assert if it's null.

Copy link
Member

Choose a reason for hiding this comment

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

Correct. But it's reasonable for an API to strengthen our understanding of a piece of nullable state. The implementation of a method can do all manner of tricks to verify that a piece of input is not null. Effectively elevating the state from "maybe null" to "not null".

Having [MaybeNull] impact the state is saying that an API can downgrade the state. Taking something the compiler believes is "not null" and making it "maybe null". Having trouble seeing how an API can make such an assertion. Basically because you passed A to me you should assume A is now possible null.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see a real reason to have it not impact things, and I don't think that this spec is the place to make the argument to change the feature in any case 😀

Copy link
Member

Choose a reason for hiding this comment

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

Don't want my debate to be about changing the feature. Want to make sure I understand [MaybeNull] so I can better evaluate this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Basically, in an ideal world, this feature would behave exactly as if you called all these methods in order manually, and that would mean any nullability post-condition attributes flowing back to the original expression (MaybeNull included). For implementation simplicity, we have avoided this.

@RikkiGibson
Copy link
Contributor

Looks like this is ready to go just pending minor fixes to the quotes in code examples.

@333fred 333fred merged commit 762e176 into dotnet:main Jan 31, 2022
@333fred 333fred deleted the update-interp-nullable branch January 31, 2022 19:39
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