Skip to content

Properly erase Java intersections #8946

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

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ object TypeOps:
case _: AppliedType | _: MatchType =>
val normed = tp.tryNormalize
if (normed.exists) normed else mapOver
case tp: MethodicType =>
tp // See documentation of `Types#simplified`
case _ =>
mapOver
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,13 @@ object Types {
* what was a union or intersection of type variables might be a simpler type
* after the type variables are instantiated. Finally, it
* maps poly params in the current constraint set back to their type vars.
*
* NOTE: Simplifying an intersection type might change its erasure (for
* example, the Java erasure of `Object & Serializable` is `Object`,
* but its simplification is `Serializable`). This means that simplification
* should never be used in a `MethodicType`, because that could
* lead to a different `signature`. Since this isn't very useful anyway,
* this method handles this by never simplifying inside a `MethodicType`.
*/
def simplified(implicit ctx: Context): Type = TypeOps.simplify(this, null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ class ClassfileParser(
if (sig(index) != ':') // guard against empty class bound
ts += objToAny(sig2type(tparams, skiptvs))
}
TypeBounds.upper(ts.foldLeft(NoType: Type)(_ & _) orElse defn.AnyType)
val bound = if ts.isEmpty then defn.AnyType else ts.reduceLeft(AndType.apply)
TypeBounds.upper(bound)
}

var tparams = classTParams
Expand Down
3 changes: 3 additions & 0 deletions tests/run/java-intersection/A_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class A_1 {
public <T extends Object & java.io.Serializable> void foo(T x) {}
}
7 changes: 7 additions & 0 deletions tests/run/java-intersection/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def main(args: Array[String]): Unit = {
val a = new A_1
val x = new java.io.Serializable {}
a.foo(x)
}
}