Skip to content

Commit

Permalink
Don't check bounds of Java applications in Java units
Browse files Browse the repository at this point in the history
Something goes wrong when we try to do mutually recursive F-bounds checking in Java units.
I am not sure what exactly. But in any case, Scala should not try to do Java's typechecking.
So we now check applications in Java units only if they refer to Scala classes.

I added a test for scala#7494, which was originally addressed by scala#9370, the PR which introduced the Java bounds
checking. Adding the test ensures that the new restrictions still glag the original error.

Fixes scala#17763
  • Loading branch information
odersky committed Jun 25, 2023
1 parent ceca748 commit 5948a8c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ object Checking {
val checker = new TypeTraverser:
def traverse(tp: Type) =
tp match
case AppliedType(tycon, argTypes) =>
case AppliedType(tycon, argTypes)
if !(tycon.typeSymbol.is(JavaDefined) && ctx.compilationUnit.isJava) =>
// Don't check bounds in Java units that refer to Java type constructors.
// Scala is not obliged to do Java type checking and in fact i17763 goes wrong
// if we attempt to check bounds of F-bounded mutually recursive Java interfaces.
// Do check all bounds in Scala units and those bounds in Java units that
// occur in applications of Scala type constructors.
checkAppliedType(
untpd.AppliedTypeTree(TypeTree(tycon), argTypes.map(TypeTree(_)))
.withType(tp).withSpan(tpt.span.toSynthetic),
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i7494/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class J {
// for java, D is D<T extends Object>
public static D<String> getDS() { // error
return new D<String>(DS);
}
}

10 changes: 10 additions & 0 deletions tests/neg/i7494/S.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class A

class D[T >: A](v: T) {
def getV(): T = v // ensure T is correctly inferred
}

object S {
// J.getDS() : D[String] is inferred but not checked
val dv: String = J.getDS().getV()
}
2 changes: 2 additions & 0 deletions tests/pos/i17763/CopyableBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public interface CopyableBuilder<B extends CopyableBuilder<B, T>, T extends ToCopyableBuilder<B, T>> {
}
1 change: 1 addition & 0 deletions tests/pos/i17763/Crash.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Crash
2 changes: 2 additions & 0 deletions tests/pos/i17763/ToCopyableBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public interface ToCopyableBuilder<B extends CopyableBuilder<B, T>, T extends ToCopyableBuilder<B, T>> {
}

0 comments on commit 5948a8c

Please sign in to comment.