-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
// hk type lambdas can be added to constraints without typevars during match reduction | ||
|
||
|
@@ -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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation is now misleading, also is there a reason why There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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)( | ||
|
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") | ||
} | ||
} |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.