Skip to content

Commit 3144259

Browse files
committed
SI-9074 Fix generic substitution with package objects, overloading
Takes a leaf out of dotty's book [1] and makes `asSeenFrom` transparently change the prefix from the package class to the package object when needed. This fixes generic subsitution during overload resolution, as reported in SI-9074. This subsumes the former fix for SI-6225, which is removed here. [1] scala/scala3#282
1 parent b810c92 commit 3144259

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,12 +807,7 @@ trait Contexts { self: Analyzer =>
807807
val qual = imp.qual
808808

809809
val qualSym = qual.tpe.typeSymbol
810-
val pre =
811-
if (qualSym.isPackageClass)
812-
// SI-6225 important if the imported symbol is inherited by the the package object.
813-
qualSym.packageObject.typeOfThis
814-
else
815-
qual.tpe
810+
val pre = qual.tpe
816811
def collect(sels: List[ImportSelector]): List[ImplicitInfo] = sels match {
817812
case List() =>
818813
List()

src/compiler/scala/tools/nsc/typechecker/Implicits.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,7 @@ trait Implicits {
10121012
}
10131013
case None =>
10141014
if (pre.isStable && !pre.typeSymbol.isExistentiallyBound) {
1015-
val pre1 =
1016-
if (sym.isPackageClass) sym.packageObject.typeOfThis
1017-
else singleType(pre, companionSymbolOf(sym, context))
1015+
val pre1 = pre memberType companionSymbolOf(sym, context)
10181016
val infos = pre1.implicitMembers.iterator.map(mem => new ImplicitInfo(mem.name, pre1, mem)).toList
10191017
if (infos.nonEmpty)
10201018
infoMap += (sym -> infos)

src/interactive/scala/tools/nsc/interactive/Global.scala

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
148148
override def forInteractive = true
149149
override protected def synchronizeNames = true
150150

151-
override def newAsSeenFromMap(pre: Type, clazz: Symbol): AsSeenFromMap =
152-
new InteractiveAsSeenFromMap(pre, clazz)
153-
154-
class InteractiveAsSeenFromMap(pre: Type, clazz: Symbol) extends AsSeenFromMap(pre, clazz) {
155-
/** The method formerly known as 'instParamsRelaxed' goes here if it's still necessary,
156-
* which it is currently supposed it is not.
157-
*
158-
* If it is, change AsSeenFromMap method correspondingTypeArgument to call an overridable
159-
* method rather than aborting in the failure case.
160-
*/
161-
}
162-
163151
/** A map of all loaded files to the rich compilation units that correspond to them.
164152
*/
165153
val unitOfFile = mapAsScalaMapConverter(new ConcurrentHashMap[AbstractFile, RichCompilationUnit] {

src/reflect/scala/reflect/internal/tpe/TypeMaps.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,12 @@ private[internal] trait TypeMaps {
433433
(pre eq NoType) || (pre eq NoPrefix) || !isPossiblePrefix(clazz)
434434
)
435435

436-
def newAsSeenFromMap(pre: Type, clazz: Symbol): AsSeenFromMap =
437-
new AsSeenFromMap(pre, clazz)
436+
final def newAsSeenFromMap(pre: Type, clazz: Symbol): AsSeenFromMap = {
437+
val prePackageObjectExpanded = if (pre.typeSymbolDirect.hasPackageFlag && !clazz.hasPackageFlag)
438+
pre.packageObject.typeOfThis
439+
else pre
440+
new AsSeenFromMap(prePackageObjectExpanded, clazz)
441+
}
438442

439443
/** A map to compute the asSeenFrom method.
440444
*/

test/files/pos/t9074.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package blam {
2+
3+
package foo {
4+
5+
trait F[T] {
6+
def f(d: Double, t: T): T = ???
7+
def f(d: Int, t: T): T = ???
8+
def f(d: String, t: T): T = ???
9+
10+
def g[A](a: T): T = ???
11+
def g(a: Int) = ???
12+
}
13+
}
14+
15+
package object foo extends foo.F[Double] {
16+
override def f(d: Double, t: Double): Double = ???
17+
}
18+
}
19+
20+
object Test {
21+
import blam._
22+
foo.f("3", 4.0)
23+
foo.g[Any](1d) : Double
24+
}

test/files/pos/t9074b.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Echo [T] {
2+
def echo(t: T): Unit
3+
}
4+
5+
trait IntEcho extends Echo[Int] {
6+
def echo(t: Int) = println(t)
7+
}
8+
9+
object echo extends IntEcho
10+
package object echo1 extends IntEcho
11+
12+
object App extends App {
13+
echo.echo(1)
14+
echo1.echo(1)
15+
}

0 commit comments

Comments
 (0)