Content-Length: 552426 | pFad | http://github.com/scala/scala/commit/175256ba51746574d3f6711682ae9ba592b048d2

65 Handle AntiPolyType in Symbol.alternatives · scala/scala@175256b · GitHub
Skip to content

Commit 175256b

Browse files
committed
Handle AntiPolyType in Symbol.alternatives
1 parent f57d4eb commit 175256b

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,36 +1537,37 @@ trait Infer extends Checkable {
15371537
def inferPolyAlternatives(tree: Tree, argtypes: List[Type]): Unit = {
15381538
val OverloadedType(pre, alts) = tree.tpe: @unchecked
15391539
// Alternatives with a matching length type parameter list
1540-
val matchingLength = tree.symbol filter (alt => sameLength(alt.typeParams, argtypes))
1541-
def allMonoAlts = alts forall (_.typeParams.isEmpty)
1542-
def errorKind = matchingLength match {
1540+
val matchingLength = tree.symbol.filter(alt => sameLength(alt.typeParams, argtypes))
1541+
def allMonoAlts = alts.forall(_.typeParams.isEmpty)
1542+
def errorKind = matchingLength match {
15431543
case NoSymbol if allMonoAlts => PolyAlternativeErrorKind.NoParams // no polymorphic method alternative
15441544
case NoSymbol => PolyAlternativeErrorKind.WrongNumber // wrong number of tparams
15451545
case _ => PolyAlternativeErrorKind.ArgsDoNotConform // didn't conform to bounds
15461546
}
15471547
def fail() = PolyAlternativeError(tree, argtypes, matchingLength, errorKind)
1548-
def finish(sym: Symbol, tpe: Type) = tree setSymbol sym setType tpe
1548+
def finish(sym: Symbol, tpe: Type) = tree.setSymbol(sym).setType(tpe)
15491549
// Alternatives which conform to bounds
15501550
def checkWithinBounds(sym: Symbol): Unit = sym.alternatives match {
15511551
case Nil => if (!argtypes.exists(_.isErroneous)) fail()
1552-
case alt :: Nil => finish(alt, pre memberType alt)
1552+
case alt :: Nil => finish(alt, pre.memberType(alt))
15531553
case alts @ hd :: _ =>
15541554
log(s"Attaching AntiPolyType-carrying overloaded type to $sym")
15551555
// Multiple alternatives which are within bounds; spin up an
15561556
// overloaded type which carries an "AntiPolyType" as a prefix.
1557-
val tparams = new AsSeenFromMap(pre, hd.owner) mapOver hd.typeParams
1558-
val bounds = tparams map (_.tpeHK) // see e.g., #1236
1557+
val tparams = new AsSeenFromMap(pre, hd.owner).mapOver(hd.typeParams)
1558+
val bounds = tparams.map(_.tpeHK) // scala/bug#1236
15591559
val tpe = PolyType(tparams, OverloadedType(AntiPolyType(pre, bounds), alts))
1560-
finish(sym setInfo tpe, tpe)
1560+
finish(sym.setInfo(tpe), tpe)
15611561
}
15621562
matchingLength.alternatives match {
15631563
case Nil => fail()
1564-
case alt :: Nil => finish(alt, pre memberType alt)
1564+
case alt :: Nil => finish(alt, pre.memberType(alt))
15651565
case _ =>
1566-
checkWithinBounds(matchingLength.filter { alt =>
1566+
val conforming = matchingLength.filter { alt =>
15671567
isWithinBounds(pre, alt.owner, alt.typeParams, argtypes) &&
15681568
kindsConform(alt.typeParams, argtypes, pre, alt.owner)
1569-
})
1569+
}
1570+
checkWithinBounds(conforming)
15701571
}
15711572
}
15721573
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4515,7 +4515,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
45154515
val isClassOf = fun.symbol.rawname == nme.classOf && currentRun.runDefinitions.isPredefClassOf(fun.symbol)
45164516
if (isJava && fun.symbol.isTerm) args.foreach(_.modifyType(rawToExistential)) // e.g. List.class, parsed as classOf[List]
45174517
val targs = mapList(args)(_.tpe)
4518-
checkBounds(tree, NoPrefix, NoSymbol, tparams, targs, "")
4518+
checkBounds(tree, NoPrefix, NoSymbol, tparams, targs, prefix = "")
45194519
if (isClassOf)
45204520
typedClassOf(tree, args.head, noGen = true)
45214521
else {

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,11 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
20352035
// ------ overloaded alternatives ------------------------------------------------------
20362036

20372037
def alternatives: List[Symbol] =
2038-
if (isOverloaded) info.asInstanceOf[OverloadedType].alternatives
2038+
if (isOverloaded) info match {
2039+
case OverloadedType(pre, alternatives) => alternatives
2040+
case PolyType(typeParams, OverloadedType(AntiPolyType(pre, targs), alternatives)) => alternatives
2041+
case _ => this :: Nil
2042+
}
20392043
else this :: Nil
20402044

20412045
def filter(cond: Symbol => Boolean): Symbol =

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import TypeConstants._
7171
// [tparams]result where result is a (Nullary)MethodType or ClassInfoType
7272
7373
// The remaining types are not used after phase `typer`.
74-
case OverloadedType(pre, tparams, alts) =>
74+
case OverloadedType(pre, alternatives) =>
7575
// all alternatives of an overloaded ident
7676
case AntiPolyType(pre, targs) =>
7777
// rarely used, disappears when combined with a PolyType
@@ -2936,8 +2936,7 @@ trait Types
29362936

29372937
object MethodType extends MethodTypeExtractor
29382938

2939-
// TODO: rename so it's more appropriate for the type that is for a method without argument lists
2940-
// ("nullary" erroneously implies it has an argument list with zero arguments, it actually has zero argument lists)
2939+
// Nullary not nilary, i.e., parameterlistless not merely parameterless
29412940
case class NullaryMethodType(override val resultType: Type) extends Type with NullaryMethodTypeApi {
29422941
override def isTrivial = resultType.isTrivial && (resultType eq resultType.withoutAnnotations)
29432942
override def prefix: Type = resultType.prefix
@@ -4162,6 +4161,8 @@ trait Types
41624161
case TypeRef(pre, sym, Nil) => copyTypeRef(tycon, pre, sym, args)
41634162
case TypeRef(pre, sym, bogons) => devWarning(s"Dropping $bogons from $tycon in appliedType.") ; copyTypeRef(tycon, pre, sym, args)
41644163
case PolyType(tparams, restpe) => restpe.instantiateTypeParams(tparams, args)
4164+
case MethodType(params, resultType) => resultType
4165+
case NullaryMethodType(resultType) => resultType
41654166
case ExistentialType(tparams, restpe) => newExistentialType(tparams, appliedType(restpe, args))
41664167
case st: SingletonType => appliedType(st.widen, args) // @M TODO: what to do? see bug1
41674168
case RefinedType(parents, decls) => RefinedType(parents map (appliedType(_, args)), decls) // @PP: Can this be right?

test/files/neg/t10628.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
t10628.scala:8: error: overloaded method a with alternatives:
2+
Int <and>
3+
Int
4+
cannot be applied to (Int)
5+
def f = A.a[Int][String](0)
6+
^
7+
1 error

test/files/neg/t10628.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
object A {
3+
def a[F](x: Int) = 0
4+
def a[F](x: String) = 0
5+
}
6+
7+
class C {
8+
def f = A.a[Int][String](0)
9+
}

test/files/pos/t10628.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
class Apply {
3+
def apply[A](x: Int) = 1
4+
}
5+
6+
object A {
7+
def a[F] = new Apply
8+
def a[F](x: String) = 0
9+
}
10+
11+
class C {
12+
def f = A.a[String]("a") // 0
13+
def g = A.a[String](2) // 1
14+
def k = A.a[String][Int](3)
15+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/scala/scala/commit/175256ba51746574d3f6711682ae9ba592b048d2

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy