Skip to content
Draft
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
19 changes: 17 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,29 @@ class PlainPrinter(_ctx: Context) extends Printer {
def toTextPrefixOf(tp: NamedType): Text = controlled {
homogenize(tp.prefix) match {
case NoPrefix => ""
case tp: SingletonType => toTextRef(tp) ~ "."
case tp => trimPrefix(toTextLocal(tp)) ~ "#"
case prefix: SingletonType => toTextRef(prefix) ~ "."
case prefix =>
// Use "." for Java nested classes (e.g., java.util.Map.Entry)
// Use "#" for Scala type projections (e.g., Outer#Inner)
val separator = if (isJavaNestedClass(tp)) "." else "#"
trimPrefix(toTextLocal(prefix)) ~ separator
}
}

protected def isEmptyPrefix(sym: Symbol): Boolean =
sym.isEffectiveRoot || sym.isAnonymousClass || sym.name.isReplWrapperName

/** Check if tp represents a Java nested class that should use "." separator. */
protected def isJavaNestedClass(tp: NamedType)(using Context): Boolean = {
val sym = tp.symbol
sym.exists &&
sym.is(JavaDefined) &&
sym.isClass &&
sym.owner.exists &&
sym.owner.is(JavaDefined) &&
sym.owner.isClass
}

/** String representation of a definition's type following its name,
* if symbol is completed, ": ?" otherwise.
*/
Expand Down
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,14 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
else if (name.isTypeName) typeText(txt)
else txt
case tree @ Select(qual, name) =>
if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name))
if (qual.isType) {
// Use "." for Java nested classes, "#" for Scala type projections
val separator = tree.typeOpt match {
case tp: NamedType if isJavaNestedClass(tp) => "."
case _ => "#"
}
toTextLocal(qual) ~ separator ~ typeText(toText(name))
}
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) `provided` (name != nme.CONSTRUCTOR || printDebug))
case tree: This =>
optDotPrefix(tree) ~ keywordStr("this") ~ idText(tree)
Expand Down
21 changes: 21 additions & 0 deletions tests/neg/i24711-java-nested-types.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- [E007] Type Mismatch Error: tests/neg/i24711-java-nested-types.scala:2:20 -------------------------------------------
2 | val test1: Int = (??? : java.util.Map.Entry[String, Int]) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: java.util.Map.Entry[String, Int]
| Required: Int
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg/i24711-java-nested-types.scala:4:38 -------------------------------------------
4 | val test2: Int = java.util.Map.entry("key", 1) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: java.util.Map.Entry[String, Int]
| Required: Int
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg/i24711-java-nested-types.scala:7:20 -------------------------------------------
7 | val test4: Int = (??? : Outer#Inner) // error
| ^^^^^^^^^^^^^^^^^
| Found: Test.this.Outer#Inner
| Required: Int
|
| longer explanation available when compiling with `-explain`
8 changes: 8 additions & 0 deletions tests/neg/i24711-java-nested-types.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Test {
val test1: Int = (??? : java.util.Map.Entry[String, Int]) // error

val test2: Int = java.util.Map.entry("key", 1) // error

trait Outer { type Inner }
val test4: Int = (??? : Outer#Inner) // error
}
Loading