Skip to content

Commit 151bf8a

Browse files
committed
Fix #7060: Make single-parameter constrained more versatile
Make single-parameter `constrained` more versatile by using a smarter default value for `alwaysAddTypeVars`.
1 parent c9d2f34 commit 151bf8a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-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)(

0 commit comments

Comments
 (0)