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

Consider adding MaybeDefault to Microsoft.CodeAnalysis.NullableFlowState #39981

Closed
cston opened this issue Nov 22, 2019 · 7 comments
Closed

Consider adding MaybeDefault to Microsoft.CodeAnalysis.NullableFlowState #39981

cston opened this issue Nov 22, 2019 · 7 comments

Comments

@cston
Copy link
Member

cston commented Nov 22, 2019

Consider adding MaybeDefault to Microsoft.CodeAnalysis.NullableFlowState to allow analyzers to differentiate values that may be null even if the type may be not nullable.

For instance, to represent the flow state of item:

static void M<T>(List<T> list)
{
    T item =  list.FirstOrDefault();
    // ...
}

See dotnet/csharplang#2946
See #39778

[jcouv: even if we don't make this change, we should document the maybe-default state in Mads' spec]

@cston
Copy link
Member Author

cston commented Nov 22, 2019

cc @jasonmalinowski, @333fred

@sharwell
Copy link
Member

sharwell commented Nov 22, 2019

This would be a huge help for ImmutableArray<T>, where default behaves more like null than new T[0]. Most of the flow tracking would work great with existing attributes. The only part that can't be easily represented today is indicating that a field of one of these types is not allowed to have a default value (can't reuse the ? syntax because it means Nullable<T> for value types).

Related to dotnet/csharplang#146
Related to dotnet/csharplang#2840 (comment)

@333fred
Copy link
Member

333fred commented Nov 22, 2019

@sharwell I don't think this helps at all for ImmutableArray<T>. I think you're misconstruing from the naming here. MaybeDefault applies to unconstrained type parameters, not to value types. We're not planning on extending tracking whether or not a known struct type is defaulted or not.

@sharwell
Copy link
Member

In that case, how would MaybeDefault help? MaybeNull already has the same meaning in that case.

@333fred
Copy link
Member

333fred commented Nov 23, 2019

It does not. This is how we've solved the [MaybeNull] on unconstrained type parameters, it's very possible some IDE features will want to see this info as well.

@jcouv jcouv added the Feature - Nullable Reference Types Nullable Reference Types label Dec 11, 2019
@jaredpar jaredpar added this to the Compiler.Net5 milestone Dec 14, 2019
@Joe4evr
Copy link

Joe4evr commented Mar 27, 2020

@sharwell

The only part that can't be easily represented today is indicating that a field of one of these types is not allowed to have a default value

I've said before, it might be the most practical to let that be a documented caveat when the default-ness is a private implementation detail. It's primarily the places where the default-ness can escape over an API boundary where the flow analysis should inform the consuming code.

@jaredpar jaredpar modified the milestones: Compiler.Net5, 16.8 Jun 23, 2020
@jaredpar
Copy link
Member

jaredpar commented Aug 4, 2020

Closing as this would represent a breaking change at this point, particularly since we added the defaultable generic support to nullability. Can re-open and re-visit a different style of API if we get enough requests for it.

@jaredpar jaredpar closed this as completed Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants