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

scalac gets confused when mixing java and scala sources where the java-source contains a public static final variable with the same name as a static inner class #4549

Open
scabug opened this issue May 6, 2011 · 11 comments

Comments

@scabug
Copy link

@scabug scabug commented May 6, 2011

Take the class

public class Fisk {
    public static class A {
    }
    public static A A = new A();
}

When calling it from Scala:

    val fisk = new Fisk.A()
    val strupp: Fisk.A = Fisk.A

The compiler fails compiling it:

error: A is already defined as object A
public static A A = new A();

=== What versions of the following are you using? ===

  • Scala: 2.8.1
@scabug
Copy link
Author

@scabug scabug commented May 6, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4549?orig=1
Reporter: Andreas Joseph Krogh (andreak)
Attachments:

@scabug
Copy link
Author

@scabug scabug commented May 6, 2011

Andreas Joseph Krogh (andreak) said:
http://devnet.jetbrains.net/message/5302484SI-5302484

@scabug
Copy link
Author

@scabug scabug commented May 10, 2011

@dragos said:
This is hard to fix, because the Scala compiler considers all static members of A as part of a (fictive) companion object A. Adding a field called A makes the definition ambiguous. Even if you compile this with javac first, you won't be able to access members of A from Scala. For instance:

public class Fisk {
    public static class A {
        public static void foo() {
            System.out.println("foo");
        }

        public int bar = 10;
    }
    public static A A = new A();

    public static void test() {
        int x = Fisk.A.bar;
        Fisk.A.foo();
    }
}
class Foo {
  Fisk.A.bar
  Fisk.A.foo()
}

you get:

{code}
isk.scala:2: error: ambiguous reference to overloaded definition,
both variable A in object Fisk of type Fisk.A
and object A in object Fisk of type object Fisk.A
match expected type ?
Fisk.A.bar
^
Fisk.scala:3: error: ambiguous reference to overloaded definition,
both variable A in object Fisk of type Fisk.A
and object A in object Fisk of type object Fisk.A
match expected type ?
Fisk.A.foo()
^
two errors found
]}}

@scabug
Copy link
Author

@scabug scabug commented May 10, 2011

Andreas Joseph Krogh (andreak) said:
But this JAVA-code works (of course):

	public void stuff() {
		Fisk.A a = new Fisk.A();
		Fisk.A b = Fisk.A;
		int i = Fisk.A.bar;
		Fisk.A.foo();
		b.foo();
		i = b.bar;
	}

This strikes me as strange as Scala is supposed to be, at least to my knowledge, 100% compatible with JAVA, but surely isn't as we've demonstrated here.

Is this something you Scala-guys will take a note of and plan to address, or will it end up on the infamous incompatibility-list?

--
Andreas

@scabug
Copy link
Author

@scabug scabug commented Mar 15, 2013

@adriaanm said:
Un-assigning to foster work stealing, as announced in https://groups.google.com/forum/?fromgroups=#!topic/scala-internals/o8WG4plpNkw

@scabug
Copy link
Author

@scabug scabug commented Jul 10, 2013

@adriaanm said:
Unassigning and rescheduling to M6 as previous deadline was missed.

@scabug
Copy link
Author

@scabug scabug commented Aug 5, 2014

@gkossakowski said:
The 2.11.2 is out so I'm rescheduling the issue for 2.11.3.

@scabug
Copy link
Author

@scabug scabug commented Sep 14, 2014

Konstantin Nikiforov (nikifor) said:
Another use-case to reproduce compiler issue (play-framework stripped hello-world).

@scabug
Copy link
Author

@scabug scabug commented Sep 14, 2014

Konstantin Nikiforov (nikifor) said:
I've attached simple single-line usecase project, that depends on play-framework 2.3.
It compiles on scala-2.10.x without problems, but fails on 2.11.x with messages mostly similar to this issue.

Steps to reproduce: download, unpack, sbt compile. The only source code file is conf/routres. Sbt will download deps, generate router source code into target/scala-2.11/src_managed/main and take a try to compile it. It will fail with:

[info] Compiling 4 Scala sources and 2 Java sources to /target/scala-2.11/classes...
[error] conf/routes: routes is already defined as object routes
[error] conf/routes: routes is already defined as class routes
[error] conf/routes:6: ReverseAssets is already defined as class ReverseAssets
[error] GET  /assets/*file                      controllers.Assets.at(path="/public", file)
[error] conf/routes:6: ReverseAssets is already defined as class ReverseAssets
[error] GET  /assets/*file                      controllers.Assets.at(path="/public", file)
[error] conf/routes:6: ReverseAssets is already defined as class ReverseAssets
[error] GET  /assets/*file                      controllers.Assets.at(path="/public", file)
[error] conf/routes: Routes is already defined as object Routes
[error] 6 errors found
[error] (compile:compile) Compilation failed

target/scala-2.11/src_generated/main contains java-source (in controllers/ subdir) with final static variables and inner static classes. Scala files contain inner classes, but DO NOT calling java-variables and inner-classes directly. So, this usecase slightly differs from original issue description.

scala 2.11.2
sbt 0.13.5
java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.2) (Arch Linux build 7.u65_2.5.2-1-x86_64)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

@scabug
Copy link
Author

@scabug scabug commented Nov 4, 2014

@retronym said:
Updating fix-by version to 2.11.5.

@scabug
Copy link
Author

@scabug scabug commented Feb 26, 2015

@adriaanm said:
Should revisit when we rethink how to emit static members.

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

Successfully merging a pull request may close this issue.

None yet
2 participants