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

Refaster: fix expression matching in absence of additional syntactic context #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rickie
Copy link
Member

@rickie rickie commented Sep 18, 2021

@BeforeTemplate implementations of the form return someParam; are valid
constructs. With these changes such templates behave as expected, such that
someParam only matches expressions of the appropriate type, not references to
the type itself.

@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from c779e6f to 11ebd54 Compare September 20, 2021 05:54
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

I forgot the use case we had for this; can you refresh my memory? I think it'll be important to provide context when we open a PR upstream.

@rickie
Copy link
Member Author

rickie commented Oct 10, 2021

Yes of course. The example that is provided in the OnlyBeforeSimpleReturnTemplate is considered a valid case Refaster template that one could write. However, without our changes the example would not run successfully.

This problem came up when we were defining the MigrationTemplates. A migration template consists of one expression in the @BeforeTemplate like in the example. Without this change, the template would not correctly match occurrences in the code.

In the end, this seemed to be the minimal example for a reproduction.

The JCTree.JCPackageDecl change prevents Refaster from going over the package statement (obviously haha). For this specific Refaster template that solves part of the problem because there were some errors when Refaster was scanning that statement (I'm not sure what the exact error was though).

The other changes in visitIdentifier prevent Refaster from thinking that String as ClassSymbol is a "normal" expression that should be matched. That should not be the case, therefore we aim to filter out JCExpressions that do not represent valid Java expressions.

@rickie rickie requested a review from Stephan202 October 10, 2021 13:39
@Stephan202 Stephan202 force-pushed the rossendrijver/refaster_simple_return branch from 11ebd54 to 581e166 Compare January 2, 2022 10:27
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Rebased and added one commit. Suggested commit message:

Refaster: fix expression matching in absence of additional syntactic context

`@BeforeTemplate` implementations of the form `return someParam;` are valid
constructs. With these changes such templates behave as expected, such that
`someParam` only matches expressions of the appropriate type, not references to
the type itself.

@@ -101,7 +101,7 @@ public Void visitVariable(VariableTree node, Context context) {

@Override
public Void scan(Tree tree, Context context) {
if (tree == null) {
if (tree == null || tree instanceof JCTree.JCPackageDecl) {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we'd do this:

Suggested change
if (tree == null || tree instanceof JCTree.JCPackageDecl) {
if (tree == null || tree instanceof PackageTree) {

But PackageTree is marked @since 9. Perhaps we should wait for (or contribute) the changes necessary to target JDK 11, so that we can make this nice. See google#2783 (comment).

Edit: it's just IDEA flagging this; the build does pass with this change. In light of the above I think it's fine to go ahead with this then.

Symbol currentExprSymbol = ASTHelpers.getSymbol(expression);
if (currentExprSymbol instanceof TypeSymbol
|| (currentExprSymbol instanceof Symbol.MethodSymbol
&& target instanceof JCTree.JCIdent)) {
Copy link
Member

Choose a reason for hiding this comment

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

IIUC we can also use an interface here:

Suggested change
&& target instanceof JCTree.JCIdent)) {
&& target instanceof IdentifierTree)) {

@@ -365,4 +365,9 @@ public void staticImportClassToken() throws IOException {
public void suppressWarnings() throws IOException {
runTest("SuppressWarningsTemplate");
}

@Test
public void onlyBeforeTemplate() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public void onlyBeforeTemplate() throws IOException {
public void onlyBeforeSimpleReturnTemplate() throws IOException {

Comment on lines 21 to 24
/**
* Template for handling the case where only the {@link BeforeTemplate} is specified with a simple return
* statement.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/**
* Template for handling the case where only the {@link BeforeTemplate} is specified with a simple return
* statement.
*/
/**
* Template that flags all string-typed expressions, irrespective of the syntactic context in which
* they occur.
*/

@@ -0,0 +1,24 @@
/*
* Copyright 2021 The Error Prone Authors.
Copy link
Member

Choose a reason for hiding this comment

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

We can now use 2022.

@rickie rickie changed the title Add support for simple return statement with only a BeforeTemplate Refaster: fix expression matching in absence of additional syntactic context Jan 2, 2022
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch 2 times, most recently from b1adbf3 to 21bc6e2 Compare January 5, 2022 09:18
@rickie
Copy link
Member Author

rickie commented Jan 5, 2022

Upstream PR: google#2805

@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from 3b86713 to a1fd29b Compare July 4, 2022 10:54
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from a1fd29b to e3d86cf Compare August 8, 2022 07:07
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from e3d86cf to 29b528a Compare September 2, 2022 08:03
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from 29b528a to e5a7be0 Compare January 9, 2023 07:52
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from 79366f8 to 8bab354 Compare January 16, 2023 08:04
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from 8bab354 to d334c28 Compare January 23, 2023 08:32
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from d334c28 to fdc96e3 Compare June 8, 2023 07:48
@rickie rickie force-pushed the rossendrijver/refaster_simple_return branch from fdc96e3 to d3d6b1d Compare June 19, 2023 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants