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

Pass resolved Symbols into Handler methods #729

Merged
merged 2 commits into from
Mar 9, 2023

Conversation

XN137
Copy link
Contributor

@XN137 XN137 commented Feb 2, 2023

this avoids redundant ASTHelpers.getSymbol calls and is more consistent with the other Handler method signatures

Nullness nullness = returnValueNullness(node, input, nullnessHint);
if (booleanReturnType(node)) {
if (booleanReturnType(callee)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that getSymbol(MethodInvocationTree tree) never returns null (maybe this was different in previous errorprone versions?)

@@ -2279,7 +2279,7 @@ private boolean mayBeNullExpr(VisitorState state, ExpressionTree expr) {
"whoops, better handle " + expr.getKind() + " " + state.getSourceForNode(expr));
}
}
exprMayBeNull = handler.onOverrideMayBeNullExpr(this, expr, state, exprMayBeNull);
exprMayBeNull = handler.onOverrideMayBeNullExpr(this, expr, exprSymbol, state, exprMayBeNull);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

do we worry about exprSymbol being null here? should the parameter be marked nullable?
should we do Preconditions.checkNotNull here because we know it cant be null?

taking a step back:
should we add a library model for ASTHelpers ?

@@ -45,17 +45,13 @@ public class AssertionHandler extends BaseNoOpHandler {
@Override
public NullnessHint onDataflowVisitMethodInvocation(
MethodInvocationNode node,
Symbol.MethodSymbol callee,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that i've adjusted the parameter name in the handlers to keep the diff small

if you want it to be same as in the base interface let me know (and how to call it i.e. callee, symbol, methodSymbol etc.)

VisitorState state,
AccessPath.AccessPathContext apContext,
AccessPathNullnessPropagation.SubNodeValues inputs,
AccessPathNullnessPropagation.Updates thenUpdates,
AccessPathNullnessPropagation.Updates elseUpdates,
AccessPathNullnessPropagation.Updates bothUpdates) {
Symbol.MethodSymbol callee = ASTHelpers.getSymbol(node.getTree());
if (callee == null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this could never be null (and is no checked at the root call site anyway)

@XN137 XN137 marked this pull request as ready for review February 2, 2023 08:16
@msridhar
Copy link
Collaborator

msridhar commented Feb 5, 2023

@XN137 in my opinion, the benefit of avoiding extra ASTHelpers.getSymbol calls here is not worth having to add another parameter to the handler method, just in terms of readability. Other handler methods take either a Symbol or a Tree, but none take both. If we could eliminate the expr parameter this could be worthwhile, but unfortunately it looks like it is needed in a couple of places. I am open to discussion if there are benefits to this change I am not seeing.

@XN137
Copy link
Contributor Author

XN137 commented Feb 5, 2023

@msridhar thanks for the feedback

Other handler methods take either a Symbol or a Tree, but none take both.

i might be missing something but afaict the first 5 methods of the Handler interface DO take both:

void onMatchTopLevelClass(
NullAway analysis, ClassTree tree, VisitorState state, Symbol.ClassSymbol classSymbol);

void onMatchMethod(
NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol);

etc...

also further down:

NullnessHint onDataflowVisitFieldAccess(
FieldAccessNode node,
Symbol symbol,

so there are cases for passing the symbol along both trees and nodes, which is why i suggested to do the same in the other 2 methods., seems more uniform and avoids redundant work (also the nullablity of the parameter could be decided once, instead of in each subclass)

@msridhar
Copy link
Collaborator

msridhar commented Feb 5, 2023

so there are cases for passing the symbol along both trees and nodes, which is why i suggested to do the same in the other 2 methods., seems more uniform and avoids redundant work (also the nullablity of the parameter could be decided once, instead of in each subclass)

You are completely right; somehow I missed that. My mistake.

Not all ExpressionTrees will have a symbol necessarily. The one case I am thinking of is a ConditionalExpressionTree, representing an expression like b ? x : y, which I believe will not have a symbol. So ASTHelpers.getSymbol() can certainly return null. That said, there are many cases where we can be strongly confident it will not return null unless something else is messed up in the compilation, e.g., for an identifier. In all the cases where handler methods currently take a symbol, we can be confident it won't be null.

Since we can't capture the right invariants in the NullAway type system, I'm not sure if making ASTHelpers.getSymbol() return @Nullable in general will be an overall positive for the codebase. @lazaroclapp thoughts here? Would be interested to get your thoughts on this PR in general. Overall I'm still unsure if this change is a net win.

@XN137 XN137 force-pushed the pass-handler-symbols branch 3 times, most recently from 91bdf51 to fb72a81 Compare February 6, 2023 11:53
@XN137
Copy link
Contributor Author

XN137 commented Feb 6, 2023

i've filed #733 for the general discussion around ASTHelpers

for this PR i have marked the new parameter @Nullable Symbol exprSymbol

let me know what you think

@XN137 XN137 force-pushed the pass-handler-symbols branch 2 times, most recently from e425958 to f917733 Compare February 8, 2023 10:29
@XN137
Copy link
Contributor Author

XN137 commented Feb 13, 2023

@lazaroclapp sorry for the ping, but do you have any feedback here? if you also think this is not a clear improvement we can close it, thanks

Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

Hey, sorry for the very long delay on this one! I've been recovering from COVID these last two weeks and busy the one before that.

I actually agree this is more consistent with other Handler methods. My only comment here before approving is that we don't usually encourage @Nullable in local variables for NullAway (and either way, the real fix is to make getSymbol(...)'s return @Nullable, which seems to be happening separately from this PR). Happy to approve the refactoring after that change!

@@ -2189,7 +2189,7 @@ private boolean mayBeNullExpr(VisitorState state, ExpressionTree expr) {
return false;
}
// the logic here is to avoid doing dataflow analysis whenever possible
Symbol exprSymbol = ASTHelpers.getSymbol(expr);
@Nullable Symbol exprSymbol = ASTHelpers.getSymbol(expr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nullability annotations should not be needed in local variables. If anything, this means we might indeed need a model for ASTHelpers.getSymbol(...) (or, presumably, we just wait for EP to mark it @Nullable, given the discussion in #733 )

this avoids redundant ASTHelpers.getSymbol calls and
is more consistent with the other Handler method signatures
@XN137
Copy link
Contributor Author

XN137 commented Mar 9, 2023

Hey, sorry for the very long delay on this one! I've been recovering from COVID these last two weeks and busy the one before that.

no worries, hope you made a full/speedy recovery and thanks for getting back to this.

i've rebased and hopefully addressed your concern in the additional review commit,
let me know in case anything else is needed

Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

LGTM!

@lazaroclapp lazaroclapp enabled auto-merge (squash) March 9, 2023 21:17
@lazaroclapp lazaroclapp merged commit f743be3 into uber:master Mar 9, 2023
@XN137 XN137 deleted the pass-handler-symbols branch March 10, 2023 06:43
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 18, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 18, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants