Skip to content

[java] Fix method calls in type resolution#3061

Merged
adangel merged 13 commits into
pmd:masterfrom
oowekyala:typeres-fix-method-calls
Jan 21, 2021
Merged

[java] Fix method calls in type resolution#3061
adangel merged 13 commits into
pmd:masterfrom
oowekyala:typeres-fix-method-calls

Conversation

@oowekyala

@oowekyala oowekyala commented Jan 15, 2021

Copy link
Copy Markdown
Member

Describe the PR

Fix a bug in type resolution, by which expressions like a.m() have type a and not whatever the return type of m is. The fixed issues are instances of this same problem. It was not introduced in type resolution by 6.30.0, only made discoverable because UseEqualsToCompareStrings was updated to use type resolution in #2934.

This could be caused by unresolved local variables, though given how rotten the typeres code is I'm not sure. Other tests are failing now...


update

With these changes, in a call chain, the return type of method calls is now set on the PrimarySuffix that corresponds to the arguments. Previously, it was the PrimarySuffix for the method name, or the PrimaryPrefix, if the receiver is an ambiguous name (ASTName). This was inconsistent and hid the type of the receiver in that case.

Eg str.contains("") had the following type info (in the best case, whereas with the bug of #2976 the PrimaryExpression has the incorrect type string):

+ PrimaryExpression (type =  boolean)
  + PrimaryPrefix
     + Name(image = "str.contains", type = boolean)
  + PrimarySuffix
     + Arguments

where nothing hints that the receiver is of type string. Now it's

+ PrimaryExpression (type =  boolean)
  + PrimaryPrefix
     + Name(image = "str.contains", type = String)    <- this is the receiver type
  + PrimarySuffix (type = boolean)
     + Arguments

I think this is the best we can do on master. I don't want to add tests that will be thrown away when merging 7.0.x, I spent already too much time on this, so this only adds tests to the rules, not the typeres code.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by travis)
  • Added (in-code) documentation (if needed)

Consider things accessible if we can't resolve this class.
In particular, local variables were inaccessible, which is absurd.
@oowekyala oowekyala added this to the 6.31.0 milestone Jan 15, 2021
@oowekyala oowekyala changed the title [core] Fix method calls in type resolution [java] Fix method calls in type resolution Jan 15, 2021
The primary prefix now has the type of the receiver,
when it contains the name of a method call
This made tests fail
@ghost

ghost commented Jan 16, 2021

Copy link
Copy Markdown
1 Message
📖 This changeset changes 16 violations,
introduces 1156 new violations, 0 new errors and 0 new configuration errors,
removes 276 violations, 0 errors and 0 configuration errors.
Full report
This changeset changes 0 violations,
introduces 308 new violations, 1 new errors and 0 new configuration errors,
removes 407 violations, 0 errors and 0 configuration errors.
Full report
This changeset changes 0 violations,
introduces 308 new violations, 1 new errors and 0 new configuration errors,
removes 407 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

@adangel

adangel commented Jan 16, 2021

Copy link
Copy Markdown
Member

Awesome, thanks for working on this! 🚀

Also add correct imports for GuardLogStatement tests
The test classes have not been compiled, that's why the typeres
can't always resolve the classes.
@uhafner

uhafner commented Feb 15, 2021

Copy link
Copy Markdown

Does this fix also help for false negatives when comparing enumeration values? I still get warnings with 6.31.0 (see https://github.com/jenkinsci/warnings-ng-plugin/pull/774/checks?check_run_id=1804343680).

@oowekyala

Copy link
Copy Markdown
Member Author

@uhafner This looks like the result of #2934 (merged for pmd 6.30). I don't think this PR is related

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