Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
5866d0d
Allow refinements of new types
odersky May 24, 2016
d30f441
Allow general recursion in refined types.
odersky Jun 29, 2016
5d03186
Better printing of skolems
odersky May 26, 2016
cdb4a1c
New type lambda scheme for hk types
odersky Jun 29, 2016
850dc6f
Introduce recursive types
odersky Jun 29, 2016
af43d32
Abstract type parameters out from type symbols
odersky Jun 29, 2016
aa7f66d
Disable checkInst in RecType
odersky Jun 29, 2016
08a0ea6
Add printing of type lambda trees
odersky Jun 29, 2016
178e90e
Remove special case in parser
odersky May 27, 2016
4bf43f8
Turn on new hk scheme
odersky Jun 29, 2016
5daae27
Multiple fixes
odersky Jun 29, 2016
e56bd1f
Beta-reduce when simplifying
odersky Jun 2, 2016
e61b80a
Refinement of cycle avoidance
odersky Jun 2, 2016
a7d61c0
Make etaExpandIfHk work for non-symbol type params
odersky Jun 2, 2016
d0f82a5
Don't map info of SkolemTypes in type maps
odersky Jun 3, 2016
c136af1
Fixes to BetaReduce and asMemberOf; add a second betaReduce
odersky Jun 29, 2016
ae1f248
Normalize RecTypes on creation to avoid cycles.
odersky Jun 4, 2016
939d9da
Add a second betaReduce
odersky Jun 4, 2016
c35f817
Adapt widenForMatchSelector to new HK scheme
odersky Jun 5, 2016
e36a36a
Fix typeParams for abstract types under completion
odersky Jun 5, 2016
6a7e466
Avoid accidental creation of hk types
odersky Jun 29, 2016
8e84fb0
Eta-expand unapplied types that have type parameters
odersky Jun 29, 2016
68e73e8
Restrict betaReduce to hk applications
odersky Jun 29, 2016
463e99a
Optionally, check kinds match for & and |
odersky Jun 7, 2016
0a5f839
Avoid infinite recursion when comparing recursive types.
odersky Jun 29, 2016
31af865
Fix condition for lambda abstracting in Namer
odersky Jun 29, 2016
5041e93
Make TypeAccumulators follow LazyRefs
odersky Jun 7, 2016
f1bf78b
Avoid creating dependent function types for closures
odersky Jun 8, 2016
830b724
Change tests
odersky Jun 29, 2016
6414f3b
Deal with nested findMember calls over the same RecType
odersky Jun 9, 2016
e749d83
Various tweaks
odersky Jun 29, 2016
d1f809f
Remove old hk scheme
odersky Jun 29, 2016
60d81f8
Start new, direct HK scheme
odersky Jun 29, 2016
3490e01
Make Constraint#bounds work for aliases
odersky Jun 14, 2016
f6efd99
Fix appliedTo and typeParams, and higher kinded subtyping tests
odersky Jun 29, 2016
97e84e6
Fix printing of type lambda trees and types
odersky Jun 18, 2016
73dd039
Various hk related fixes in types
odersky Jun 29, 2016
02ce995
Refactoring of PolyType and TypeLambda
odersky Jun 29, 2016
c28dd1b
Tweaks to appliedTo
odersky Jun 29, 2016
c1e27a0
Change underlying of HKApply
odersky Jun 29, 2016
98b466c
Ensure wildApprox maintains kinds
odersky Jun 20, 2016
0965e1a
Handle TypeLambdas in findMember
odersky Jun 20, 2016
65c26ba
Avoid cyclic errors when reading nsc.Global
odersky Jun 21, 2016
1e48758
Refactor handling of unpickled type params
odersky Jun 29, 2016
960ea75
Allow for HK types in widenForMatchSelector
odersky Jun 22, 2016
e0db04d
Drop bounds checking for type lambdas
odersky Jun 22, 2016
de5d8fe
Skip typeBounds when computing upperBound
odersky Jun 22, 2016
8805dd4
When comparing types revert eta-expansion as needed
odersky Jun 26, 2016
646bf97
Beta-reduce with wildcard arguments if this is safe.
odersky Jun 29, 2016
09f7ab1
Handle findMember for unreduced hk types.
odersky Jun 27, 2016
9a90e81
Handle WildcardType in liftToClasses
odersky Jun 29, 2016
dca1052
Don't make * types higher-kinded in avoid
odersky Jun 29, 2016
4093e13
Don't dealias when applying types
odersky Jun 29, 2016
bb59931
Fixes to hk comparisons
odersky Jun 29, 2016
dc5be65
Special case for TypeRefs in upperbound
odersky Jun 28, 2016
31ecad5
Drop assertion in TypeMap
odersky Jun 29, 2016
9d9965c
Fix parameter unpickling
odersky Jun 28, 2016
a23c1a4
Fixes for printing under -Ytest-pickler
odersky Jun 29, 2016
6bd7ba9
Remove refinement encoding of hk types
odersky Jun 29, 2016
ae360e9
Handle signatures over uninstantiated type variables
odersky Jun 29, 2016
6abde38
Get rid of BindingKind
odersky Jun 29, 2016
68abba1
Simplify appliedTo
odersky Jun 29, 2016
30e15ab
Cleanup of Signature matching
odersky Jun 30, 2016
b6a8bc7
Revert complications in asMemberOf
odersky Jun 30, 2016
32c0135
Refactorings of GenericTypes and underlying/upperbound
odersky Jun 30, 2016
34a068b
Drop Config.checkKinds
odersky Jun 30, 2016
beff8f8
Avoid orphan parameters in Constraint#replace
odersky Jul 2, 2016
eebb4b0
Fix bug in printing untyped New nodes.
odersky Jul 3, 2016
fd62c7b
Disallow higher-kinded types that simulate general existential types
odersky Jul 7, 2016
4693a78
Less eager removal of type parameters from constraint
odersky Jul 7, 2016
2ddb849
Unify aliases when taking a lub.
odersky Jul 4, 2016
c541ef9
Do the right thing for NoDenotation.mapInfo
odersky Jul 7, 2016
78b2672
Add a case where caching was missing.
odersky Jul 5, 2016
223705d
Refactor Typevar instantiation
odersky Jul 7, 2016
5e90215
Drop compareAliasedRefined
odersky Jul 8, 2016
540b38c
More tests
odersky Jul 9, 2016
a200695
Fix SI-2712
odersky Jul 11, 2016
6d7bc49
Address reviewers comments
odersky Jul 11, 2016
055726e
ExtractAPI: Do not miss value parameters of PolyTypes
smarter Jul 12, 2016
a6a142e
ExtractAPI: Add support for TypeLambdas
smarter Jul 12, 2016
1792c9e
ExtractAPI: Add support for RecType
smarter Jul 12, 2016
cdebd91
Allow definition of new types in refinements
odersky Jul 11, 2016
84a1a7a
Avoid dealiasing on type application
odersky Jul 12, 2016
c7f3b45
Drop restriction to 2nd order hk types
odersky Jul 12, 2016
7df0fa5
Enable test for fixed #1181: Typer should preserve aliases in TypeTrees
smarter Jul 12, 2016
1443fd4
Optimize hk comparisons
odersky Jul 12, 2016
f50cb20
Make rewritings of hk applications configurable
odersky Jul 13, 2016
0ff5354
Simplify variance computations
odersky Jul 13, 2016
18b3080
Allow hk parameters in lower bounds
odersky Jul 13, 2016
82fc27f
Fix bounds checking of hk applied typed
odersky Jul 14, 2016
a737b47
Add test case for hk bounds checking
odersky Jul 14, 2016
894c9fb
Bounds of type lambdas compare contravariantly
odersky Jul 15, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid dealiasing on type application
When applying a type alias of a type lambda, keep the original
application instead of reducing. But reduce anyway if

 - the reduced type is an application where the type
   constructor has the same kind as the original type constructor, or
 - some of the arguments are wildcards.
  • Loading branch information
odersky committed Jul 12, 2016
commit 84a1a7ae7b1e4931fe04a5a21a04bb858e8acebb
12 changes: 7 additions & 5 deletions src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,21 @@ trait ConstraintHandling {
* to refer to such a lambda parameter because the lambda parameter is
* not visible where `A` is defined. Consequently, we need to
* approximate the bound so that the lambda parameter does not appear in it.
* Test case in neg/i94-nada.scala. This test crashes with an illegal instance
* error when the rest of the SI-2712 fix is applied but `pruneLambdaParams` is
* If `tp` is an upper bound, we need to approximate with something smaller,
* otherwise something larger.
* Test case in pos/i94-nada.scala. This test crashes with an illegal instance
* error in Test2 when the rest of the SI-2712 fix is applied but `pruneLambdaParams` is
* missing.
*/
def pruneLambdaParams(tp: Type) =
if (comparingLambdas) {
if (comparingLambdas && param.binder.isInstanceOf[PolyType]) {
val approx = new ApproximatingTypeMap {
def apply(t: Type): Type = t match {
case t @ PolyParam(tl: TypeLambda, n) =>
val effectiveVariance = if (fromBelow) -variance else variance
val bounds = tl.paramBounds(n)
if (effectiveVariance > 0) bounds.hi
else if (effectiveVariance < 0 ) bounds.lo
if (effectiveVariance > 0) bounds.lo
else if (effectiveVariance < 0) bounds.hi
else NoType
case _ =>
mapOver(t)
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ object Symbols {

// TypeParamInfo methods
def isTypeParam(implicit ctx: Context) = denot.is(TypeParam)
def paramName(implicit ctx: Context): Name = name
def paramName(implicit ctx: Context) = name.asTypeName
def paramBounds(implicit ctx: Context) = denot.info.bounds
def paramBoundsAsSeenFrom(pre: Type)(implicit ctx: Context) = pre.memberInfo(this).bounds
def paramBoundsOrCompleter(implicit ctx: Context): Type = denot.infoOrCompleter
Expand Down
81 changes: 48 additions & 33 deletions src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ class TypeApplications(val self: Type) extends AnyVal {
*
* TODO: Handle parameterized lower bounds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was on my TODO list also :). So far I've only noticed one method that expects only the upper-bound to be a lambda: adaptHkVariances

*/
def LambdaAbstract(tparams: List[TypeParamInfo])(implicit ctx: Context): Type = {
def LambdaAbstract(tparams: List[Symbol])(implicit ctx: Context): Type = {
def expand(tp: Type) =
TypeLambda(
tpnme.syntheticLambdaParamNames(tparams.length), tparams.map(_.paramVariance))(
tpnme.syntheticLambdaParamNames(tparams.length), tparams.map(_.variance))(
tl => tparams.map(tparam => tl.lifted(tparams, tparam.paramBounds).bounds),
tl => tl.lifted(tparams, tp))
assert(!isHK, self)
Expand Down Expand Up @@ -439,20 +439,13 @@ class TypeApplications(val self: Type) extends AnyVal {
}
}

/** Encode
/** The type representing
*
* T[U1, ..., Un]
*
* where
* @param self = `T`
* @param args = `U1,...,Un`
* performing the following simplifications
*
* 1. If `T` is an eta expansion `[X1,..,Xn] -> C[X1,...,Xn]` of class `C` compute
* `C[U1, ..., Un]` instead.
* 2. If `T` is some other type lambda `[X1,...,Xn] -> S` none of the arguments
* `U1,...,Un` is a wildcard, compute `[X1:=U1, ..., Xn:=Un]S` instead.
* 3. If `T` is a polytype, instantiate it to `U1,...,Un`.
*/
final def appliedTo(args: List[Type])(implicit ctx: Context): Type = /*>|>*/ track("appliedTo") /*<|<*/ {
val typParams = self.typeParams
Expand All @@ -469,30 +462,52 @@ class TypeApplications(val self: Type) extends AnyVal {
}
case nil => t
}
val stripped = self.stripTypeVar
val dealiased = stripped.safeDealias
if (args.isEmpty || ctx.erasedTypes) self
else self.stripTypeVar.safeDealias match {
case self: TypeLambda =>
if (!args.exists(_.isInstanceOf[TypeBounds])) self.instantiate(args)
else {
val reducer = new Reducer(self, args)
val reduced = reducer(self.resType)
if (reducer.allReplaced) reduced
else HKApply(self, args)
}
case self: PolyType =>
self.instantiate(args)
case self: AndOrType =>
self.derivedAndOrType(self.tp1.appliedTo(args), self.tp2.appliedTo(args))
case self: TypeAlias =>
self.derivedTypeAlias(self.alias.appliedTo(args))
case self: TypeBounds =>
self.derivedTypeBounds(self.lo, self.hi.appliedTo(args))
case self: LazyRef =>
LazyRef(() => self.ref.appliedTo(args))
case self: WildcardType =>
self
case self: TypeRef if self.symbol == defn.NothingClass =>
self
else dealiased match {
case dealiased: TypeLambda =>
def tryReduce =
if (!args.exists(_.isInstanceOf[TypeBounds])) {
val reduced = dealiased.instantiate(args)
if (dealiased eq stripped) reduced
else reduced match {
case AppliedType(tycon, args) if variancesConform(typParams, tycon.typeParams) =>
// Reducing is safe for type inference, as kind of type constructor does not change
Copy link
Member

@smarter smarter Jul 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a Config boolean to never apply the reduction so that we can be sure that reducing does not affect semantics?

//println(i"reduced: $reduced instead of ${HKApply(self, args)}")
reduced
case _ =>
// Reducing changes kind, keep hk application instead
//println(i"fallback: ${HKApply(self, args)} instead of $reduced")
HKApply(self, args)
}
}
else dealiased.resType match {
case AppliedType(tycon, args1) if tycon.safeDealias ne tycon =>
dealiased
.derivedTypeLambda(resType = tycon.safeDealias.appliedTo(args1))
.appliedTo(args)
case _ =>
val reducer = new Reducer(dealiased, args)
val reduced = reducer(dealiased.resType)
if (reducer.allReplaced) reduced
else HKApply(dealiased, args)
}
tryReduce
case dealiased: PolyType =>
dealiased.instantiate(args)
case dealiased: AndOrType =>
dealiased.derivedAndOrType(dealiased.tp1.appliedTo(args), dealiased.tp2.appliedTo(args))
case dealiased: TypeAlias =>
dealiased.derivedTypeAlias(dealiased.alias.appliedTo(args))
case dealiased: TypeBounds =>
dealiased.derivedTypeBounds(dealiased.lo, dealiased.hi.appliedTo(args))
case dealiased: LazyRef =>
LazyRef(() => dealiased.ref.appliedTo(args))
case dealiased: WildcardType =>
dealiased
case dealiased: TypeRef if dealiased.symbol == defn.NothingClass =>
dealiased
case _ if typParams.isEmpty || typParams.head.isInstanceOf[LambdaParam] =>
HKApply(self, args)
case dealiased =>
Expand Down
7 changes: 4 additions & 3 deletions src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,10 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
val tparams1 = tparams1a.drop(lengthDiff)
variancesConform(tparams1, tparams) && {
if (lengthDiff > 0)
tycon1b = tycon1a
.appliedTo(args1.take(lengthDiff) ++ tparams1.map(_.paramRef))
.LambdaAbstract(tparams1)
tycon1b = TypeLambda(tparams1.map(_.paramName), tparams1.map(_.paramVariance))(
tl => tparams1.map(tparam => tl.lifted(tparams, tparam.paramBounds).bounds),
tl => tycon1a.appliedTo(args1.take(lengthDiff) ++
tparams1.indices.toList.map(PolyParam(tl, _))))
(ctx.mode.is(Mode.TypevarsMissContext) ||
tryInstantiate(tycon2, tycon1b.ensureHK)) &&
isSubType(tp1, tycon1b.appliedTo(args2))
Expand Down
10 changes: 5 additions & 5 deletions src/dotty/tools/dotc/core/TypeParamInfo.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.dotc.core

import Names.Name
import Names.TypeName
import Contexts.Context
import Types.{Type, TypeBounds}

Expand All @@ -15,22 +15,22 @@ trait TypeParamInfo {
def isTypeParam(implicit ctx: Context): Boolean

/** The name of the type parameter */
def paramName(implicit ctx: Context): Name
def paramName(implicit ctx: Context): TypeName

/** The info of the type parameter */
def paramBounds(implicit ctx: Context): TypeBounds

/** The info of the type parameter as seen from a prefix type.
* For type parameter symbols, this is the `memberInfo` as seen from `prefix`.
* For type lambda parameters, it's the same as `paramBounds` as
* For type lambda parameters, it's the same as `paramBounds` as
* `asSeenFrom` has already been applied to the whole type lambda.
*/
def paramBoundsAsSeenFrom(pre: Type)(implicit ctx: Context): TypeBounds

/** The parameter bounds, or the completer if the type parameter
* is an as-yet uncompleted symbol.
*/
def paramBoundsOrCompleter(implicit ctx: Context): Type
def paramBoundsOrCompleter(implicit ctx: Context): Type

/** The variance of the type parameter */
def paramVariance(implicit ctx: Context): Int
Expand Down
30 changes: 24 additions & 6 deletions src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ object Types {
case _ => this1.symbol eq sym
}
case this1: RefinedOrRecType => this1.parent.isRef(sym)
case this1: HKApply => this1.superType.isRef(sym)
case _ => false
}

Expand Down Expand Up @@ -857,6 +858,10 @@ object Types {
tp.derivedAnnotatedType(tp.tpe.dealias, tp.annot)
case tp: LazyRef =>
tp.ref.dealias
case app @ HKApply(tycon, args) =>
val tycon1 = tycon.dealias
if (tycon1 ne tycon) app.superType.dealias
else this
case _ => this
}

Expand Down Expand Up @@ -2586,7 +2591,7 @@ object Types {
lazy val typeParams: List[LambdaParam] =
paramNames.indices.toList.map(new LambdaParam(this, _))

def derivedTypeLambda(paramNames: List[TypeName], paramBounds: List[TypeBounds], resType: Type)(implicit ctx: Context): Type =
def derivedTypeLambda(paramNames: List[TypeName] = paramNames, paramBounds: List[TypeBounds] = paramBounds, resType: Type)(implicit ctx: Context): Type =
Copy link
Member

@smarter smarter Jul 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other derivedFoo method returns a Foo, but this one may return a TypeAlias or a TypeBounds instead of a TypeLambda, why is that? Could a different name be chosen to avoid confusion?

resType match {
case resType @ TypeAlias(alias) =>
resType.derivedTypeAlias(duplicate(paramNames, paramBounds, alias))
Expand Down Expand Up @@ -2640,12 +2645,21 @@ object Types {
abstract case class HKApply(tycon: Type, args: List[Type])
extends CachedProxyType with ValueType {

private var validSuper: Period = Nowhere
private var cachedSuper: Type = _

override def underlying(implicit ctx: Context): Type = tycon

override def superType(implicit ctx: Context): Type = tycon match {
case tp: TypeLambda => defn.AnyType
case tp: TypeProxy => tp.superType.applyIfParameterized(args)
case _ => defn.AnyType
override def superType(implicit ctx: Context): Type = {
if (ctx.period != validSuper) {
cachedSuper = tycon match {
case tp: TypeLambda => defn.AnyType
case tp: TypeProxy => tp.superType.applyIfParameterized(args)
case _ => defn.AnyType
}
validSuper = ctx.period
}
cachedSuper
}
/*
def lowerBound(implicit ctx: Context): Type = tycon.stripTypeVar match {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an early commented attempt that can be deleted

Expand Down Expand Up @@ -2760,7 +2774,11 @@ object Types {
else bounds(paramNum)
}
// no customized hashCode/equals needed because cycle is broken in PolyType
override def toString = s"PolyParam($paramName)"
override def toString =
try s"PolyParam($paramName)"
catch {
case ex: IndexOutOfBoundsException => s"PolyParam(<bad index: $paramNum>)"
}

override def computeHash = doHash(paramNum, binder.identityHash)

Expand Down
2 changes: 1 addition & 1 deletion tests/neg/boundspropagation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ object test4 {
}

class Test5 {
"": ({ type U = this.type })#U // error // error
"": ({ type U = this.type })#U // error
}
11 changes: 0 additions & 11 deletions tests/neg/i94-nada.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/pos/i94-nada.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Test2 {
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
println(flatMap(Right(1), {x: Int => Right(x)}))
println(flatMap(Left(1), {x: Int => Left(x)}))
}
trait Test3 {
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/t2712-6.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test

object Tags {
type Tagged[A, T] = {type Tag = T; type Self = A}

type @@[T, Tag] = Tagged[T, Tag]

trait Disjunction

def meh[M[_], A](ma: M[A]): M[A] = ma
meh(null: Int @@ Disjunction)//.asInstanceOf[Int @@ Disjunction])
}