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

Compilation error "type Proxy is ambiguous" #1354

Closed
kriegaex opened this issue Apr 1, 2022 · 4 comments
Closed

Compilation error "type Proxy is ambiguous" #1354

kriegaex opened this issue Apr 1, 2022 · 4 comments
Assignees
Milestone

Comments

@kriegaex
Copy link

kriegaex commented Apr 1, 2022

JDK 11, Groovy 2.5.16, GrEclipse 3.7.1, GrEclipse Batch 2.6.16-02.

An MCVE is here. Just clone it and run mvn test. You will see:

[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ Groovy_GrEclipseMavenAmbiguousTypeProxy ---
[INFO] Changes detected - recompiling the module!
[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files
[INFO] Compiling in a forked process using C:\Users\alexa\.m2\repository\org\codehaus\groovy\groovy-eclipse-batch\2.5.16-02\groovy-eclipse-batch-2.5.16-02.jar
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing groovy-eclipse compiler:
----------
1. ERROR in C:\Users\alexa\Documents\java-src\Groovy_GrEclipseMavenAmbiguousTypeProxy\src\test\groovy\HttpConnectionMockTest.groovy (at line 24)
	private Proxy proxy
	        ^^^^^
The type Proxy is ambiguous
----------
2. ERROR in C:\Users\alexa\Documents\java-src\Groovy_GrEclipseMavenAmbiguousTypeProxy\src\test\groovy\HttpConnectionMockTest.groovy (at line 28)
	UnderTest(Proxy proxy, ProxySettingDTO proxySettingDTO, ConnectionManager connectionManager) {
	          ^^^^^
The type Proxy is ambiguous
----------
3. ERROR in C:\Users\alexa\Documents\java-src\Groovy_GrEclipseMavenAmbiguousTypeProxy\src\test\groovy\HttpConnectionMockTest.groovy (at line 50)
	URLConnection openConnection(URL url, Proxy proxy) {
	                                      ^^^^^
The type Proxy is ambiguous
----------
3 problems (3 errors)

[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

The error goes away if I import either of these here

//import java.net.Proxy     // <- This is what IntelliJ IDEA resolves
//import groovy.util.Proxy

IntelliJ IDEA shows both of them greyed out and removes the imports, because both seem to be imported by default in Groovy already. Other classes of that name which are visible from JRE or dependencies are net.sf.cglib.proxy.Proxy, org.graalvm.compiler.nodes.spi.Proxy, java.lang.reflect.Proxy, but those are not automatically imported, so they are fine.

I am not sure if this is a Groovy compiler or core problem, but it is kind of suboptimal that two identically named classes are imported by default. The error goes away if I import one of the two on top, but always marked as redundant and removed when reorganising imports, which is dangerous, but understandable, because the IDE correctly identifies them both as available.

kriegaex added a commit to kriegaex/GebSpockSamples that referenced this issue Apr 1, 2022
Due to groovy/groovy-eclipse#1354, without an
explicit import for java.net.Proxy, there will be a compilation error:

"The type Proxy is ambiguous"
kriegaex added a commit to kriegaex/GebSpockSamples that referenced this issue Apr 1, 2022
Due to groovy/groovy-eclipse#1354, without an
explicit import for java.net.Proxy, there will be a compilation error:

"The type Proxy is ambiguous"
@eric-milles eric-milles self-assigned this Apr 1, 2022
@eric-milles eric-milles added this to the v4.6.0 milestone Apr 1, 2022
@eric-milles
Copy link
Member

eric-milles commented Apr 1, 2022

I have a fix for the editor to retain the import(s) necessary for name disambiguation. With that out of the way, the ambiguous reference error from the compiler is in keeping with ecj's handling of the situation. You can use a fully-qualified name if you don't want to keep fighting with the editor's import handling.

@kriegaex
Copy link
Author

kriegaex commented Apr 2, 2022

Thanks for the Eclipse IDE fix. I do not use Eclipse, though.

Follow-up questions:

  1. Would I have to create a similar issue for IntelliJ IDEA, so their Groovy editor requires and retains imports necessary for disambiguation? Using a full name is going to yield IDE optimisation hints to import the class, which then is going to be greyed out again, so I want to get this fixed, not work around it forever.
  2. Can you briefly explain how a situation like this can occur in the first place? In Java, it would not be possible to have ambigous imports, AFAIK. How can Groovy import classes by default which have conflicting names without running into problems? Well, there are problems later, of course, when trying to compile. But actually, at first my test ran fine locally in the IDE, only in Maven it failed. Only after some more edits, it started failing in the IDE, too. So it does not seem to be guaranteed that there is always a compile error.

@eric-milles
Copy link
Member

eric-milles commented Apr 2, 2022

Yes, I would think IDEA would require similar handling. Not sure what they do for Java in the same situation. Although implicit imports add a bit of nuance.

The situation can occur in Java as well. The classic example is List:

import java.awt.*;
import java.util.*;
...
List // ambiguous reference

Eclipse wants to ensure that it makes no difference if the imports are reordered. Groovy adds extra default star imports beyond java.lang. And you may add imports through compiler configuration as well. So with two extra sources for implicit imports, there are more chance for a situation like List above without explicit import statements for the user to reason about.

The groovy compiler resolves type references starting with explicit imports followed by star imports. This is handled by ResolveVisitor and StaticImportVisitor does the same for variable expressions. Once a name like List or Proxy is found no further star imports are checked. This may be considered a bug or just a difference between groovyc and javac/ecj. Groovy-Eclipse is embedded within ecj/jdt, so sometimes -- like in this case -- a java rule overrides a groovy one.

I think it is safer to stick with the ambiguous reference error versus suppressing it. There have been instances where the Groovy default imports were reordered for performance (to reduce lookups) and this kind of change could destabilize an application that relies on order of star imports.

@kriegaex
Copy link
Author

kriegaex commented Apr 3, 2022

For reference: I just created IDEA-291430

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

No branches or pull requests

2 participants