Skip to content

Fix #7060: Make single-parameter constrained more versatile #7065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ object ProtoTypes {
* for each parameter.
* @return The added type lambda, and the list of created type variables.
*/
def constrained(tl: TypeLambda, owningTree: untpd.Tree, alwaysAddTypeVars: Boolean = false)(implicit ctx: Context): (TypeLambda, List[TypeTree]) = {
def constrained(tl: TypeLambda, owningTree: untpd.Tree, alwaysAddTypeVars: Boolean)(implicit ctx: Context): (TypeLambda, List[TypeTree]) = {
val state = ctx.typerState
val addTypeVars = alwaysAddTypeVars || !owningTree.isEmpty
if (tl.isInstanceOf[PolyType])
assert(!(ctx.typerState.isCommittable && !addTypeVars),
assert(!ctx.typerState.isCommittable || addTypeVars,
s"inconsistent: no typevars were added to committable constraint ${state.constraint}")
Copy link
Member

Choose a reason for hiding this comment

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

If we change the assertion then this message also needs to be updated I think:

Suggested change
s"inconsistent: no typevars were added to committable constraint ${state.constraint}")
s"inconsistent: typevars were added to non-committable constraint ${state.constraint}")

Copy link
Member

Choose a reason for hiding this comment

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

ah nevermind I got confused, the current message is correct.

// hk type lambdas can be added to constraints without typevars during match reduction

Expand All @@ -502,8 +502,13 @@ object ProtoTypes {
(added, tvars)
}

def constrained(tl: TypeLambda, owningTree: untpd.Tree)(implicit ctx: Context): (TypeLambda, List[TypeTree]) =
constrained(tl, owningTree,
alwaysAddTypeVars = tl.isInstanceOf[PolyType] && ctx.typerState.isCommittable)

/** Same as `constrained(tl, EmptyTree)`, but returns just the created type lambda */
Copy link
Member

Choose a reason for hiding this comment

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

The documentation is now misleading, also is there a reason why alwaysAddTypeVars = ctx.typerState.isCommittable is not a good default for the other overload ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would be a good default, except that the default value would refer to the ctx which follows.

def constrained(tl: TypeLambda)(implicit ctx: Context): TypeLambda = constrained(tl, EmptyTree)._1
def constrained(tl: TypeLambda)(implicit ctx: Context): TypeLambda =
constrained(tl, EmptyTree)._1

def newTypeVar(bounds: TypeBounds)(implicit ctx: Context): TypeVar = {
val poly = PolyType(DepParamName.fresh().toTypeName :: Nil)(
Expand Down
19 changes: 19 additions & 0 deletions tests/neg/i7060.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object PostConditions1 {

import PostConditions.{ensure, res, Box}

val v = List(1, 2, 4).sum.ensure(Box(10) => res == 10) // error: not a legal formal parameter
println(v)
}

object PostConditions {

class Box[T](val t: T)

def res[T] given (b: Box[T]): T = b.t

def (e: T) ensure[T](cond: given Box[T] => Boolean): T = {
if (cond given Box(e)) e
else throw new AssertionError("condition not fulfilled")
}
}