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

NULL_LITERAL expressions may always be null #749

Merged
merged 1 commit into from Mar 31, 2023

Conversation

XN137
Copy link
Contributor

@XN137 XN137 commented Mar 12, 2023

there is no reason to give handlers a chance to override this because we are also not doing this for other literals that we filter out at the start of the method

}
Symbol exprSymbol = ASTHelpers.getSymbol(expr);
boolean exprMayBeNull;
switch (expr.getKind()) {
case ARRAY_ACCESS:
// unsound! we cannot check for nullness of array contents yet
exprMayBeNull = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

imo this is another candidate to be pulled into the 1st switch to return false immediately?
i might be missing something but what value is there in passing this through the handlers chain when none of them deal with ARRAY_ACCESS expressions?
this can then of course be revisited once there is a dedicated handler for this.

same imo for the expression types in the next block that are // clearly not null

Copy link
Collaborator

Choose a reason for hiding this comment

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

For array accesses I'd rather not bail out for now. For array accesses we are thinking about doing better handling according to JSpecify in the medium term, and it's possible a handler may want to override what happens there.

For the // clearly not null cases I'm not sure. @lazaroclapp do you think it's worth bailing out early to avoid running the handlers for these cases, where basically according to the language spec the expression cannot be 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.

imo as long as no handler cares for these expression it is best to return early, because this avoids redundant work (calling the handler chain for no reason).
see also suggestion in #753

if in the near future a handler for array access will be added, it is trivial to move that expression back to the 2nd switch ...
if proper tests get added this required move will become obvious.

switch (expr.getKind()) {
case NULL_LITERAL:
// obviously null
exprMayBeNull = true;
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

doing this i think was the reason why i saw a small but noticeable difference in the NullawayRelease JMH benchmark, YMMV

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, this is a good change I think. I cannot imagine a handler wanting to override the nullness of null 🙂 Let's just add a comment stating we don't allow handlers to override, so we return early as a performance optimization?

Copy link
Contributor Author

@XN137 XN137 Mar 31, 2023

Choose a reason for hiding this comment

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

ok comment adjusted...
calling it a perf optimization felt redundant because we also dont do that for the other literals above.
WDYT ?

@XN137 XN137 force-pushed the handlers-never-override-NULL_LITERAL branch from c0a0449 to 3273051 Compare March 31, 2023 10:39
@msridhar
Copy link
Collaborator

Assuming we're going to do something like #753 is this change still needed? Or should it be folded in?

@XN137 XN137 force-pushed the handlers-never-override-NULL_LITERAL branch from 3273051 to 2b38ee8 Compare March 31, 2023 17:04
@XN137
Copy link
Contributor Author

XN137 commented Mar 31, 2023

@msridhar it is already folded into the other PR, but since the scope there is broader and the review more involved, we can maybe get this merged more quickly?

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

We can land this ahead of #753 etc but one request

@@ -2197,14 +2197,17 @@ private boolean mayBeNullExpr(VisitorState state, ExpressionTree expr) {
// obviously not null
return false;
}
switch (expr.getKind()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make this an if rather than a switch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i made it a switch in anticipation of more expression types returning early, but ok changed to if

@XN137 XN137 force-pushed the handlers-never-override-NULL_LITERAL branch 2 times, most recently from 027a80f to 8b003b3 Compare March 31, 2023 18:59
@msridhar
Copy link
Collaborator

Whoops looks like we need some kind of import now? Could you fix?

there is no reason to give handlers a chance to override this because we
are also not doing this for other literals that we filter out at the
start of the method
@XN137 XN137 force-pushed the handlers-never-override-NULL_LITERAL branch from 8b003b3 to 9595546 Compare March 31, 2023 21:23
@msridhar msridhar merged commit e4dfeac into uber:master Mar 31, 2023
7 checks passed
@XN137 XN137 deleted the handlers-never-override-NULL_LITERAL branch April 5, 2023 16:27
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

2 participants