Skip to content

Analyze throw expression and adjust analysis of null-coalescing operator#32991

Merged
jcouv merged 12 commits intodotnet:masterfrom
jcouv:throw-expr
Feb 13, 2019
Merged

Analyze throw expression and adjust analysis of null-coalescing operator#32991
jcouv merged 12 commits intodotnet:masterfrom
jcouv:throw-expr

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Jan 31, 2019

  • A throw expression visits the expression (we check that it's not possibly null), then marks the state as unreachable.
  • In the left branch of x ?? y, we should learn that x != null.

Fixes #32879

@jcouv jcouv added this to the 16.0.P4 milestone Jan 31, 2019
@jcouv jcouv self-assigned this Jan 31, 2019
@jcouv jcouv requested a review from a team as a code owner January 31, 2019 02:26
Copy link
Copy Markdown
Contributor

@cston cston Jan 31, 2019

Choose a reason for hiding this comment

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

Consider adding a ref LocalState state parameter. #Closed

@jcouv jcouv added the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Jan 31, 2019
@jcouv jcouv removed the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Jan 31, 2019
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jan 31, 2019

Choose a reason for hiding this comment

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

var rightState = this.State.Clone(); [](start = 12, length = 36)

Why do we need this clone? Is LearnFromNonNullTest changing the current state? Also, the rightState name is somewhat misleading, we haven't visited the right side yet, which is not in line with the naming pattern used in this component. #Closed

Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Jan 31, 2019

Choose a reason for hiding this comment

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

warning CS8602: Possible dereference of a null reference. [](start = 27, length = 57)

The warning feels misleading, e is not dereferenced here. #Closed

@AlekseyTs
Copy link
Copy Markdown
Contributor

AlekseyTs commented Jan 31, 2019

Done with review pass (iteration 1) #Closed

Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Feb 1, 2019

Choose a reason for hiding this comment

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

node.Kind == BoundKind.ThrowExpression || [](start = 25, length = 42)

Relaxing this assert doesn't feel like a right thing to do. Are we not setting _resultType properly for a throw expression? I think we should do that instead. #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What do you think the _resultType should be?

Currently, we let _resultType be Exception (or whatever is thrown) from visiting the thrown value.
If we wanted to set it properly, it should probably be the converted type of the throw expression, which does not exist in the throw expression itself, only in the conversion that wraps the throw expression.

A possibility is to set the _resultType to null/default, since the BoundThrowExpression has a null Type. Then I would adjust this method to handle null/default _resultType, instead of special-casing throw expression.
Do you think that's better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think that's better?

In this visitor, after visiting each expression, we should set _resultType the result type should reflect the original type of the node plus added nullability information. It shouldn't matter if there is a conversion on top of the BoundThrowExpression node, it will be handled when its turn comes and the _resultType will be adjusted for it as appropriate. That said, there should be no special handling/treatment for BoundThrowExpression, but adjusting the assert adds it.


In reply to: 253510940 [](ancestors = 253510940)

@AlekseyTs
Copy link
Copy Markdown
Contributor

AlekseyTs commented Feb 1, 2019

Done with review pass (iteration 2) #Closed

Copy link
Copy Markdown
Member

@333fred 333fred Feb 4, 2019

Choose a reason for hiding this comment

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

Consider adding a test for string~. #Closed

@333fred
Copy link
Copy Markdown
Member

333fred commented Feb 4, 2019

Done review pass (commit 3). #Closed

@333fred
Copy link
Copy Markdown
Member

333fred commented Feb 5, 2019

    (throw null!)!;

Revert


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:1790 in 36c319f. [](commit_id = 36c319fd8e756ea6e9e0e4e5919651d92c523b33, deletion_comment = False)

@333fred
Copy link
Copy Markdown
Member

333fred commented Feb 5, 2019

Done review pass (commit 8)

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Feb 6, 2019

throw null!!;

Indeed. Thanks

(throw null!)!;

This one is fine. Getting an extra warning on the null isn't the purpose of that test.

Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 9)

Copy link
Copy Markdown
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

LGTM (commit 9). Comment about string~ in ThrowExpressionInNullCoalescingOperator still needs to be addressed, but that can just be added to the linked bug as a note.

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Feb 7, 2019

Thanks all for your feedback and patience.
I'm going to hold off merging this til next week (for 16.1).

@333fred
Copy link
Copy Markdown
Member

333fred commented Feb 8, 2019

@jcouv master is 16.1 now.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants