Skip to content

Commit e8536f0

Browse files
committed
Fix #1786: support use package object in fun call
1 parent 7775aab commit e8536f0

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
363363
tree.withType(ownType)
364364
}
365365

366-
checkValue(tree1, pt)
366+
checkValue(adaptPackageObject(tree1, pt), pt)
367367
}
368368

369369
private def typedSelect(tree: untpd.Select, pt: Type, qual: Tree)(implicit ctx: Context): Select =
370370
healNonvariant(
371-
checkValue(assignType(cpy.Select(tree)(qual, tree.name), qual), pt),
371+
checkValue(adaptPackageObject(assignType(cpy.Select(tree)(qual, tree.name), qual), pt), pt),
372372
pt)
373373

374374
/** Let `tree = p.n` where `p: T`. If tree's type is an unsafe instantiation
@@ -389,6 +389,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
389389
}
390390
else tree
391391

392+
/** Adapt e.g. `scala.meta { ... }` to `scala.meta.package { ... }` */
393+
private def adaptPackageObject[T <: Tree](tree: T, pt: Type)(implicit ctx: Context): T = tree.tpe match {
394+
case tp: NamedType if tp.symbol.is(Package) && pt.isInstanceOf[ApplyingProto] =>
395+
val pkgObj = tp.symbol.moduleClass.denot.asInstanceOf[PackageClassDenotation].packageObj
396+
if (pkgObj.exists) tree.withType(pkgObj.valRef).asInstanceOf[T] else tree
397+
case _ =>
398+
tree
399+
}
400+
392401
def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = track("typedSelect") {
393402
def typeSelectOnTerm(implicit ctx: Context): Tree = {
394403
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))

tests/neg/i1786.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package scala
2+
3+
package object meta {
4+
def apply(x: Int): Int = x * x
5+
}
6+
7+
class Test {
8+
def f(a: Any): Any = f(meta) // error
9+
def g(a: Any): Any = f(scala.meta) // error
10+
11+
meta { 5 + 4 }
12+
13+
scala.meta { 3 }
14+
15+
val m1 = meta // error
16+
val m2 = scala.meta // error
17+
}

tests/pos/i1786.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scala
2+
3+
package object meta {
4+
def apply(x: Int): Int = x * x
5+
}
6+
7+
class Test {
8+
meta { 5 + 4 }
9+
10+
scala.meta { 3 }
11+
12+
scala.meta.`package` { 3 }
13+
14+
// val m1 = meta // error
15+
// val m2 = scala.meta // error
16+
val m3 = scala.meta.`package`
17+
val m4 = meta.`package`
18+
}

0 commit comments

Comments
 (0)