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

Set null state on dereference #30564

Merged
merged 1 commit into from Oct 19, 2018
Merged

Set null state on dereference #30564

merged 1 commit into from Oct 19, 2018

Conversation

cston
Copy link
Member

@cston cston commented Oct 17, 2018

Fixes #23270.

@cston cston requested a review from a team as a code owner October 17, 2018 19:38
@cston
Copy link
Member Author

cston commented Oct 17, 2018

@dotnet/roslyn-compiler please review.

@cston
Copy link
Member Author

cston commented Oct 18, 2018

@dotnet/roslyn-compiler, please review. This change prevents redundant (and incorrect) warnings on subsequent dereferences of the same variable. The actual code change is small - most of the changes are to the tests.

@gafter
Copy link
Member

gafter commented Oct 18, 2018

@gafter is added to the review. #Closed

Copy link
Member

@gafter gafter left a comment

Choose a reason for hiding this comment

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

:shipit:

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        CheckPossibleNullReceiver(node.Expression);

It feels incorrect to mark node.Expression as not null at this point, the value won't be "checked" until after all indices are evaluated. #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:1357 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

            CheckPossibleNullReceiver(receiverOpt);

It feels incorrect to mark receiverOpt as not null at this point, the value won't be "checked" until after all arguments are evaluated. #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:1946 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        CheckPossibleNullReceiver(receiverOpt);

It feels incorrect to mark receiverOpt as not null at this point, the value won't be "checked" until after all arguments are evaluated. #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:3679 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

        CheckPossibleNullReceiver(receiver);

If this node can be a part of dynamic invocation, it feels incorrect to mark receiver as not null at this point, the value won't be "checked" until after all arguments are evaluated.


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:4185 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

            CheckPossibleNullReceiver(receiverOpt);

It feels incorrect to mark receiverOpt as not null at this point. Is value "checked" before argument is evaluated? #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:4213 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        CheckPossibleNullReceiver(receiver);

It feels incorrect to mark receiver as not null at this point, the value won't be "checked" until after all arguments are evaluated. #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:4301 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

                    VisitMemberAccess(propertyAccess.ReceiverOpt, propertyAccess.PropertySymbol, asLvalue: true);

If this is a target of an assignment, is receiver actually going to be checked before the value (right hand side) is evaluated?


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:453 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

                    VisitMemberAccess(eventAccess.ReceiverOpt, eventAccess.EventSymbol, asLvalue: true);

Similar question here


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:459 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

        VisitMemberAccess(node.ReceiverOpt, node.PropertySymbol, asLvalue: false);

It would be good to have a clear understanding what constructs go through here


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:3671 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

        VisitMemberAccess(node.ReceiverOpt, node.FieldSymbol, asLvalue: false);

It would be good to have a clear understanding what constructs go through here


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:3665 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

                    VisitMemberAccess(fieldAccess.ReceiverOpt, fieldAccess.FieldSymbol, asLvalue: true);

If this is a target of an assignment, is receiver actually going to be checked before the value (right hand side) is evaluated? #Pending


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:447 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

        VisitMemberAccess(node.ReceiverOpt, node.EventSymbol, asLvalue: false);

It would be good to have a clear understanding what constructs go through here


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:3690 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

Done with review pass (iteration 2). Tests are not looked at. #Closed

@cston
Copy link
Member Author

cston commented Oct 18, 2018

@dotnet-bot retest this please

@cston
Copy link
Member Author

cston commented Oct 18, 2018

        CheckPossibleNullReceiver(node.Expression);

Good catch. In these cases, we're reporting one warning dereferencing the variable, but not reporting the same warning for other dereferences within the same expression. Logged ##30598 and added test NullableReferenceTypesTests.NotNullAfterDereference_Indexer.


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


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:1357 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        // https://github.com/dotnet/roslyn/issues/29855: there should only be two diagnostics

The comment probably should be adjusted #Closed


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:9864 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        // https://github.com/dotnet/roslyn/issues/29855: there should only be two diagnostics

Here too #Closed


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:9900 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 18, 2018

        // https://github.com/dotnet/roslyn/issues/29855: there should only be two diagnostics

And here #Closed


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:9936 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

Copy link
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 3), identified issues can be followed up on later.

@cston
Copy link
Member Author

cston commented Oct 18, 2018

                    VisitMemberAccess(fieldAccess.ReceiverOpt, fieldAccess.FieldSymbol, asLvalue: true);

Added tests and added comment to VisitMemberAccess.


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


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:447 in cf7548d. [](commit_id = cf7548d58a41cf652d841bfea163dcbd21081a6d, deletion_comment = False)

@cston
Copy link
Member Author

cston commented Oct 18, 2018

@AlekseyTs, all comments have been addressed. Thanks.

@cston
Copy link
Member Author

cston commented Oct 19, 2018

@jaredpar for approval

@jaredpar
Copy link
Member

approved to merge

@cston cston merged commit 5bb48db into dotnet:dev16.0.x Oct 19, 2018
@cston cston deleted the 23270 branch October 19, 2018 14:44
@sharwell
Copy link
Member

@cston Do we have a test covering this behavior?

void Method(object? obj)
{
  obj.Extension(); // Should not warn
  obj.ToString(); // Should warn
}

static void Extension(this object? obj) { }

@cston
Copy link
Member Author

cston commented Nov 11, 2018

@sharwell, there are a couple of tests with extension methods (see NotNullAfterDereference_03 for instance), but there is not a test of a call to an extension method followed by an instance method. I'll add that, thanks.

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.

None yet

5 participants