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

GenerateType isn't aware of nullability #30316

Closed
jcouv opened this issue Oct 4, 2018 · 6 comments · Fixed by #36605
Closed

GenerateType isn't aware of nullability #30316

jcouv opened this issue Oct 4, 2018 · 6 comments · Fixed by #36605

Comments

@jcouv
Copy link
Member

jcouv commented Oct 4, 2018

The refactoring is successful, but nullability annotations (?) are missing in the generated code.

Note: this screenshot doesn't actually demonstrate the bug, since x2 and y2 are both non-null at that point in the code.

image

FYI @dpoeschl

@CyrusNajmabadi
Copy link
Member

Julien, how is this expected to work? What is the type of the 'x2' local? Is it string? (because that's how it was declared), or is it string, because flow analysis determined it is never null?

if the former, is this just a matter of SyntaxGenerator needing to be updated to just put in the question? If it's the latter, is this actually a bug? Seems fine that this would generate 'string' if we had provent he value is not nullable at that point.

@jcouv
Copy link
Member Author

jcouv commented Oct 4, 2018

My expectation the latter: we should use the null-state of the expression (rather than declared nullability for the few expressions where that is applicable). For example, generating M from M(MakeArray(nullableString)) should generate void M(string?[] x).

The reason why is because you're generating from a point where we see that your passing null, so the generated API should accept null (that's likely what you want, based on usage). Otherwise the refactoring results in diagnostics.

@CyrusNajmabadi
Copy link
Member

My expectation the latter: we should use the null-state of the expression (rather than declared nullability for the few expressions where that is applicable).

Ok agreed. In that case, isn't this working correctly? both x2 and y2 are non-null thanks to flow analysis. x2 is assigned a non-null string, and y2 is assigned x2, so it would be non-null. So we shuld generate the type without nullability annotations...

@CyrusNajmabadi
Copy link
Member

another concern i just realized. should we be taking [NonNull] annotations into account? i.e. if you're in a type that has this annotation, and we generate a new type, should we add this attribute to it?

@jcouv
Copy link
Member Author

jcouv commented Oct 4, 2018

Your correct, I made a mistake in this example (the ! on null makes it non-null).
Yes, we'll have to deal with context. As a side-note, the context is going to switch to using some form of directive syntax (#nonnull or some such), which may further complicate refactorings.

@jinujoseph jinujoseph added the Bug label Oct 5, 2018
@jinujoseph jinujoseph modified the milestones: 16.0, 16.1 Jan 16, 2019
@jinujoseph jinujoseph modified the milestones: 16.1, Backlog Apr 24, 2019
@jasonmalinowski jasonmalinowski self-assigned this Jun 4, 2019
@jasonmalinowski
Copy link
Member

This works now -- we'll close out the bug once we add tests.

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

Successfully merging a pull request may close this issue.

4 participants