Skip to content

Commit b5df0a3

Browse files
authored
Merge pull request #7065 from dotty-staging/fix-#7060
Fix #7060: Make single-parameter `constrained` more versatile
2 parents 840a122 + bf0095d commit b5df0a3

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,11 @@ object ProtoTypes {
479479
* for each parameter.
480480
* @return The added type lambda, and the list of created type variables.
481481
*/
482-
def constrained(tl: TypeLambda, owningTree: untpd.Tree, alwaysAddTypeVars: Boolean = false)(implicit ctx: Context): (TypeLambda, List[TypeTree]) = {
482+
def constrained(tl: TypeLambda, owningTree: untpd.Tree, alwaysAddTypeVars: Boolean)(implicit ctx: Context): (TypeLambda, List[TypeTree]) = {
483483
val state = ctx.typerState
484484
val addTypeVars = alwaysAddTypeVars || !owningTree.isEmpty
485485
if (tl.isInstanceOf[PolyType])
486-
assert(!(ctx.typerState.isCommittable && !addTypeVars),
486+
assert(!ctx.typerState.isCommittable || addTypeVars,
487487
s"inconsistent: no typevars were added to committable constraint ${state.constraint}")
488488
// hk type lambdas can be added to constraints without typevars during match reduction
489489

@@ -502,8 +502,13 @@ object ProtoTypes {
502502
(added, tvars)
503503
}
504504

505+
def constrained(tl: TypeLambda, owningTree: untpd.Tree)(implicit ctx: Context): (TypeLambda, List[TypeTree]) =
506+
constrained(tl, owningTree,
507+
alwaysAddTypeVars = tl.isInstanceOf[PolyType] && ctx.typerState.isCommittable)
508+
505509
/** Same as `constrained(tl, EmptyTree)`, but returns just the created type lambda */
506-
def constrained(tl: TypeLambda)(implicit ctx: Context): TypeLambda = constrained(tl, EmptyTree)._1
510+
def constrained(tl: TypeLambda)(implicit ctx: Context): TypeLambda =
511+
constrained(tl, EmptyTree)._1
507512

508513
def newTypeVar(bounds: TypeBounds)(implicit ctx: Context): TypeVar = {
509514
val poly = PolyType(DepParamName.fresh().toTypeName :: Nil)(

tests/neg/i7060.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object PostConditions1 {
2+
3+
import PostConditions.{ensure, res, Box}
4+
5+
val v = List(1, 2, 4).sum.ensure(Box(10) => res == 10) // error: not a legal formal parameter
6+
println(v)
7+
}
8+
9+
object PostConditions {
10+
11+
class Box[T](val t: T)
12+
13+
def res[T] given (b: Box[T]): T = b.t
14+
15+
def (e: T) ensure[T](cond: given Box[T] => Boolean): T = {
16+
if (cond given Box(e)) e
17+
else throw new AssertionError("condition not fulfilled")
18+
}
19+
}

0 commit comments

Comments
 (0)