Skip to content

Commit 1591c14

Browse files
committed
Make specialization pick up opportunities when the
specialized method has additional (non-specialized) type parameters. This fix comes from Stefan's desire to specialize HLists (see corresponding test). review by @Prokopec
1 parent cebc49b commit 1591c14

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,10 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
12751275
.format(tree, symbol.tpe, tree.tpe, env, specializedName(symbol, env)))
12761276
if (!env.isEmpty) { // a method?
12771277
val specCandidates = qual.tpe.member(specializedName(symbol, env))
1278-
val specMember = specCandidates suchThat (s => doesConform(symbol, tree.tpe, s.tpe, env))
1278+
val specMember = specCandidates suchThat { s =>
1279+
doesConform(symbol, tree.tpe, qual.tpe.memberType(s), env)
1280+
}
1281+
12791282
log("[specSym] found: " + specCandidates.tpe + ", instantiated as: " + tree.tpe)
12801283
log("[specSym] found specMember: " + specMember)
12811284
if (specMember ne NoSymbol)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class HCons$mcI$sp
2+
class HCons$mcI$sp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** Test contributed by Stefan Zeiger showing that HLists can be
2+
* specialized.
3+
*/
4+
5+
sealed trait HList {
6+
type Self <: HList
7+
8+
type |: [E] = HCons[E, Self]
9+
10+
final def |: [@specialized E](elem: E): |: [E] = new HCons[E, Self](elem, this.asInstanceOf[Self])
11+
12+
def m[@specialized E, T <: AnyRef](x: E): T = null.asInstanceOf[T]
13+
}
14+
15+
final class HCons[@specialized H, T <: HList](val head: H, val tail: T) extends HList {
16+
type Self = HCons[H, T]
17+
}
18+
19+
final object HNil extends HList {
20+
type Self = HNil.type
21+
}
22+
23+
object Test extends App {
24+
val l1 = new HCons(42, "foo" |: HNil)
25+
println(l1.getClass)
26+
27+
val l2 = 42 |: "abc" |: HNil
28+
println(l2.getClass)
29+
}

0 commit comments

Comments
 (0)