Skip to content

Commit 63e1bc9

Browse files
committed
Merge pull request #282 from dotty-staging/fix/i0239-package-objects
Fix #239 - handling of package objects
2 parents 981a218 + 413bceb commit 63e1bc9

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ trait TypeOps { this: Context =>
2222
case SuperType(thispre, _) => thispre
2323
case _ => pre
2424
}
25+
else if ((pre.termSymbol is Package) && !(thiscls is Package))
26+
toPrefix(pre.select(nme.PACKAGE), cls, thiscls)
2527
else
2628
toPrefix(pre.baseTypeRef(cls).normalizedPrefix, cls.owner, thiscls)
2729
}

src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi
123123
normalizeType {
124124
val qual = tree.qualifier
125125
qual.symbol.moduleClass.denot match {
126-
case pkg: PackageClassDenotation if tree.symbol.maybeOwner.isPackageObject =>
126+
case pkg: PackageClassDenotation if !tree.symbol.maybeOwner.is(Package) =>
127127
cpy.Select(tree)(qual select pkg.packageObj.symbol, tree.name)
128128
case _ =>
129129
tree

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ trait TypeAssigner {
114114
def ensureAccessible(tpe: Type, superAccess: Boolean, pos: Position)(implicit ctx: Context): Type = {
115115

116116
def tryInsertPackageObj(tpe: NamedType, d: Denotation): Type = {
117-
def tryInsert: Type =
118-
if (!(d.symbol.maybeOwner is Package)) {
119-
val symOwner = d.alternatives.head.symbol.owner
120-
if (symOwner.isPackageObject) tpe.derivedSelect(symOwner.thisType)
121-
else tpe
122-
} else tpe
117+
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
118+
case pkgCls: PackageClassDenotation if !(d.symbol.maybeOwner is Package) =>
119+
tpe.derivedSelect(pkgCls.packageObj.valRef)
120+
case _ =>
121+
tpe
122+
}
123123
tpe.prefix match {
124-
case pre: ThisType if pre.cls is Package => tryInsert
125-
case pre: TermRef if pre.symbol is Package => tryInsert
124+
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
125+
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
126126
case _ => tpe
127127
}
128128
}

tests/pos/i0239.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package p {
2+
class C[A] {
3+
implicit def foo: M[A] = ???
4+
}
5+
6+
object `package` extends C[String]
7+
8+
object test0 {
9+
def compute[A](implicit m: M[A]): A = ???
10+
val v = compute
11+
val v1: String = v
12+
}
13+
}
14+
15+
trait M[A]
16+
17+
object test1 {
18+
19+
def compute[A](implicit m: M[A]): A = ???
20+
21+
import p._
22+
val v = compute
23+
val v1: String = v
24+
}

tests/pos/i239-packageObj.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package p {
2+
class C[A] { def foo: A = ??? }
3+
4+
object `package` extends C[String]
5+
}
6+
7+
object test {
8+
9+
val x: String = p.foo
10+
11+
}

0 commit comments

Comments
 (0)