From 4e8a1a641ccd76f92f5c6281dcf1f8a2a9b968f0 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 17 Oct 2022 08:27:50 +0200 Subject: [PATCH] Hide ConstraintHandling within GadtConstraint --- .../tools/dotc/core/GadtConstraint.scala | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/GadtConstraint.scala b/compiler/src/dotty/tools/dotc/core/GadtConstraint.scala index c961baa01b97..f1e644fbe5d8 100644 --- a/compiler/src/dotty/tools/dotc/core/GadtConstraint.scala +++ b/compiler/src/dotty/tools/dotc/core/GadtConstraint.scala @@ -13,24 +13,19 @@ import printing._ import scala.annotation.internal.sharable object GadtConstraint: - @sharable val empty = - new GadtConstraint(OrderingConstraint.empty, SimpleIdentityMap.empty, SimpleIdentityMap.empty, false) + @sharable val empty: GadtConstraint = + new ProperGadtConstraint(OrderingConstraint.empty, SimpleIdentityMap.empty, SimpleIdentityMap.empty, false) /** Represents GADT constraints currently in scope */ -final class GadtConstraint private( +sealed trait GadtConstraint ( private var myConstraint: Constraint, private var mapping: SimpleIdentityMap[Symbol, TypeVar], private var reverseMapping: SimpleIdentityMap[TypeParamRef, Symbol], private var wasConstrained: Boolean -) extends ConstraintHandling with Showable { - import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr} +) extends Showable { + this: ConstraintHandling => - def this() = this( - myConstraint = new OrderingConstraint(SimpleIdentityMap.empty, SimpleIdentityMap.empty, SimpleIdentityMap.empty, SimpleIdentitySet.empty), - mapping = SimpleIdentityMap.empty, - reverseMapping = SimpleIdentityMap.empty, - wasConstrained = false - ) + import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr} /** Exposes ConstraintHandling.subsumes */ def subsumes(left: GadtConstraint, right: GadtConstraint, pre: GadtConstraint)(using Context): Boolean = { @@ -195,7 +190,7 @@ final class GadtConstraint private( def symbols: List[Symbol] = mapping.keys - def fresh: GadtConstraint = new GadtConstraint(myConstraint, mapping, reverseMapping, wasConstrained) + def fresh: GadtConstraint = new ProperGadtConstraint(myConstraint, mapping, reverseMapping, wasConstrained) /** Restore the state from other [[GadtConstraint]], probably copied using [[fresh]] */ def restore(other: GadtConstraint): Unit = @@ -263,3 +258,10 @@ final class GadtConstraint private( /** Provides more information than toText, by showing the underlying Constraint details. */ def debugBoundsDescription(using Context): String = i"$this\n$constraint" } + +private class ProperGadtConstraint ( + myConstraint: Constraint, + mapping: SimpleIdentityMap[Symbol, TypeVar], + reverseMapping: SimpleIdentityMap[TypeParamRef, Symbol], + wasConstrained: Boolean, +) extends ConstraintHandling with GadtConstraint(myConstraint, mapping, reverseMapping, wasConstrained)