Skip to content

Nullability: flow analysis with array creation #1243

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

Closed
jnm2 opened this issue Jan 1, 2025 · 3 comments
Closed

Nullability: flow analysis with array creation #1243

jnm2 opened this issue Jan 1, 2025 · 3 comments

Comments

@jnm2
Copy link
Contributor

jnm2 commented Jan 1, 2025

Would it be a good thing to mention that a compiler may treat a newly created array as though it was already initialized with non-null values, even though it was not?

var array = new string[10];
_ = array[0].Length; // No warning
@jnm2
Copy link
Contributor Author

jnm2 commented Jan 2, 2025

More ideas:

  1. Should we also mention more warnings related to passing nullable expressions to certain language constructs?

    • throw issues a very specific warning: "CS8597 - Thrown value may be null."
    • using and fixed work when null is passed.
    • lock never warns, but probably should since it throws if it receives null.
    • await and foreach don't do anything specific besides pass through any warning you'd get if you called expr.GetAwaiter() or expr.GetEnumerator() manually. (This can be seen by declaring extension GetAwaiter or GetEnumerator methods which do or don't declare the receiver parameter to be nullable.)
  2. Should we mention that apparent instance calls on nullable expressions maybeNull.Xyz() do not warn if the call is to an extension method and the receiver parameter of the extension method is nullable, despite the apparent dereferencing syntax?

@Nigel-Ecma
Copy link
Contributor

More ideas:

  1. Should we also mention more warnings…

Short: No.

Longer:

The Standard is aiming to specify certain features; e.g. specific pragma, annotations, and operations; upon which an implementation may, but need not, build a null analysis implementation and present warnings – it is not aiming to specify the null analysis algorithm or the warnings that must be produced. In doing this the Standard aims to specify the bare minimum, so as not to put unnecessary requirements on implementations.

The warnings it currently mentions are intended to all be non-normative examples. In reality of course many readers will take these examples as behaviour that an implementation should follow, something implementors will understand and so the more examples there are indirectly increases the expectations (but not requirements) on implementations, while also limiting how implementations evolve their warnings.

@jnm2
Copy link
Contributor Author

jnm2 commented Jan 3, 2025

Sounds good! I may have some more questions for guidance on what is and isn't bare minimum. I'll ping you in #1242.

@jnm2 jnm2 closed this as completed Jan 3, 2025
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

No branches or pull requests

2 participants