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 gives spurious type mismatch errors when calling java with source #7380

Closed
scabug opened this issue Apr 17, 2013 · 3 comments
Closed

Comments

@scabug
Copy link

scabug commented Apr 17, 2013

In Java, I have:

// java/p/UseIt.java
package p;

import q.Z;

public class UseIt {
    public static Z getZ() {
        return new Z();
    }
    
    public static q.Z zFullyQualified() {
        return getZ();
    }
}
// java/p/Z.java
package p;
public class Z {}
// java/q/Z.java}
package q;
public class Z {}

In Scala:

// scala/SomeClass.scala
class SomeClass {
  val z: q.Z = p.UseIt.zFullyQualified
  val z2: q.Z = p.UseIt.getZ //error here
}

When compiling, there is an error:

src/scala/SomeClass.scala:3: error: type mismatch;
 found   : p.Z
 required: q.Z
  val z2: q.Z = p.UseIt.getZ //error here
                        ^
one error found

This works when the return type in Java is fully qualified.

I think what's causing this is a difference in precedence/shadowing between Scala and Java. In Java, although UseIt is in package p and has access to p.Z, the import q.Z shadows it:
JLS 6.4.1:
{quote}A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:

  • any top level type named n declared in another compilation unit of p{quote}

In the SLS chapter 2, assuming a "compilation unit" includes all classes in a package regardless of which file they are in, the class p.Z has a higher precedence than the import q.Z. I assume that when the Scala compiler reads in Java and does the Scala conversion, it ends up mistakenly using the scoping rules from Scala instead of Java.

@scabug
Copy link
Author

scabug commented Apr 17, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7380?orig=1
Reporter: Donny Nadolny (dnadolny)
Affected Versions: 2.9.2, 2.10.1
Duplicates #7232

@scabug
Copy link
Author

scabug commented Apr 17, 2013

@retronym said:
I'm pretty sure this is a duplicate of #7232, I'll just confirm that.

@scabug
Copy link
Author

scabug commented Apr 17, 2013

@retronym said (edited on Apr 17, 2013 9:01:10 PM UTC):
Yep, same bug. Your project compiles with 2.10.2-SNAPSHOT. 2.10.2-RC1 is scheduled for for mid-May.

~/Downloads/ScalaJavaMix sbt -sbt-create -scala-version 2.10.1 compile
Detected sbt version 0.12.1
Using /Users/jason/.sbt/0.12.1 as sbt dir, -sbt-dir to override.
[info] Loading global plugins from /Users/jason/.sbt/0.12.1/plugins
[info] Set current project to default-c9f4e7 (in build file:/Users/jason/Downloads/ScalaJavaMix/)
[info] Defining *:scala-version
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to default-c9f4e7 (in build file:/Users/jason/Downloads/ScalaJavaMix/)
[info] Updating {file:/Users/jason/Downloads/ScalaJavaMix/}default-c9f4e7...
[info] Resolving org.scala-lang#scala-library;2.10.1 ...
[info] Done updating.
[info] Compiling 1 Scala source and 4 Java sources to /Users/jason/Downloads/ScalaJavaMix/target/scala-2.10/classes...
[error] /Users/jason/Downloads/ScalaJavaMix/src/main/scala/my/test/bl/handler/BusinessHandler.scala:6: value getAssignedUsr is not a member of my.test.bl.bo.Drawer
[error]         System.out.println(drw.getAssignedUsr())
[error]                                ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 2 s, completed Apr 17, 2013 10:58:49 PM

~/Downloads/ScalaJavaMix sbt -sbt-create -scala-home /code/scala2/build/pack compile
Detected sbt version 0.12.1
Using /Users/jason/.sbt/0.12.1 as sbt dir, -sbt-dir to override.
[info] Loading global plugins from /Users/jason/.sbt/0.12.1/plugins
[info] Set current project to default-c9f4e7 (in build file:/Users/jason/Downloads/ScalaJavaMix/)
[info] Defining {.}/*:scala-home
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to default-c9f4e7 (in build file:/Users/jason/Downloads/ScalaJavaMix/)
[success] Total time: 0 s, completed Apr 17, 2013 10:59:04 PM

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

No branches or pull requests

2 participants