error-prone icon indicating copy to clipboard operation
error-prone copied to clipboard

Avoid ambiguous imports with `@UseImportPolicy(STATIC_IMPORT_ALWAYS)`

Open Stephan202 opened this issue 2 years ago • 6 comments

These changes prevent Refaster rules from introducing a static import that conflicts with an existing static import. Without these changes the new test case fails with the following error:

com/google/errorprone/refaster/testdata/StaticImportClashTemplate.java:27: error: reference to reverseOrder is ambiguous
    return reverseOrder();
           ^
  both method <T>reverseOrder() in java.util.Comparator and method <T>reverseOrder() in java.util.Collections match

Stephan202 avatar Dec 07 '22 17:12 Stephan202

Rebased and resolved conflicts.

Stephan202 avatar Mar 25 '23 08:03 Stephan202

We still encounter this problem every now and then. Could you take a look at this @cushon?

rickie avatar May 10 '23 13:05 rickie

Sorry for the delay here.

I tried importing this, and ran into some issues with templates we have for cleanups related to https://github.com/google/truth. Truth deliberately declares overloads of assertThat in different classes, with the expectation that they will all get static-imported and the most specific one will be selected, e.g.:

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;

In general I'm not sure there's a good way for refaster to know if a particular static import is going to be ambiguous or not, and I'd like to keep the assertThat templates.

Do you have any ideas for dealing with that case?

cushon avatar May 15 '23 15:05 cushon

Wow... I didn't even know this was supported. :exploding_head:

It might be possible to resolve the conflicting symbol and check whether it'd be ambiguous. But that does sound like the kind of rabbit hole for which one should set aside a few hours. I'll make a note and try to get back to this.

Stephan202 avatar May 15 '23 20:05 Stephan202

Tnx for the ping on this one. Can't say exactly when I'll have time for a closer look, but I'll try to bump it on the TODO list :+1:

Stephan202 avatar Sep 13 '23 05:09 Stephan202

(Reopening because I see that GitHub closed this PR base on imperfect heuritics. That said, this isn't at the top of my TODO list at the moment.)

Stephan202 avatar Dec 28 '24 17:12 Stephan202