Skip to content

Commit d179a2c

Browse files
committed
Fix #7060: Make single-parameter constrained more versatile
1 parent 2c12b93 commit d179a2c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ object ProtoTypes {
503503
}
504504

505505
/** 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
506+
def constrained(tl: TypeLambda)(implicit ctx: Context): TypeLambda =
507+
constrained(tl, EmptyTree, alwaysAddTypeVars = ctx.typerState.isCommittable)._1
507508

508509
def newTypeVar(bounds: TypeBounds)(implicit ctx: Context): TypeVar = {
509510
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)