@@ -1038,37 +1038,8 @@ class Typer extends Namer
1038
1038
assignType(cpy.Match (tree)(sel, cases1), sel, cases1)
1039
1039
}
1040
1040
1041
- /** gadtSyms = "all type parameters of enclosing methods that appear
1042
- * non-variantly in the selector type" todo: should typevars
1043
- * which appear with variances +1 and -1 (in different
1044
- * places) be considered as well?
1045
- */
1046
- def gadtSyms (selType : Type )(implicit ctx : Context ): Set [Symbol ] = trace(i " GADT syms of $selType" , gadts) {
1047
- val accu = new TypeAccumulator [Set [Symbol ]] {
1048
- def apply (tsyms : Set [Symbol ], t : Type ): Set [Symbol ] = {
1049
- val tsyms1 = t match {
1050
- case tr : TypeRef if (tr.symbol is TypeParam ) && tr.symbol.owner.isTerm && variance == 0 =>
1051
- tsyms + tr.symbol
1052
- case _ =>
1053
- tsyms
1054
- }
1055
- foldOver(tsyms1, t)
1056
- }
1057
- }
1058
- accu(Set .empty, selType)
1059
- }
1060
-
1061
- /** Context with fresh GADT bounds for all gadtSyms */
1062
- def gadtContext (gadtSyms : Set [Symbol ])(implicit ctx : Context ): Context = {
1063
- val gadtCtx = ctx.fresh.setFreshGADTBounds
1064
- for (sym <- gadtSyms)
1065
- if (! gadtCtx.gadt.contains(sym)) gadtCtx.gadt.addEmptyBounds(sym)
1066
- gadtCtx
1067
- }
1068
-
1069
1041
def typedCases (cases : List [untpd.CaseDef ], selType : Type , pt : Type )(implicit ctx : Context ): List [CaseDef ] = {
1070
- val gadts = gadtSyms(selType)
1071
- cases.mapconserve(typedCase(_, selType, pt, gadts))
1042
+ cases.mapconserve(typedCase(_, selType, pt))
1072
1043
}
1073
1044
1074
1045
/** - strip all instantiated TypeVars from pattern types.
@@ -1096,9 +1067,9 @@ class Typer extends Namer
1096
1067
}
1097
1068
1098
1069
/** Type a case. */
1099
- def typedCase (tree : untpd.CaseDef , selType : Type , pt : Type , gadtSyms : Set [ Symbol ] )(implicit ctx : Context ): CaseDef = track(" typedCase" ) {
1070
+ def typedCase (tree : untpd.CaseDef , selType : Type , pt : Type )(implicit ctx : Context ): CaseDef = track(" typedCase" ) {
1100
1071
val originalCtx = ctx
1101
- val gadtCtx = gadtContext(gadtSyms)
1072
+ val gadtCtx : Context = ctx.fresh.setFreshGADTBounds
1102
1073
1103
1074
def caseRest (pat : Tree )(implicit ctx : Context ) = {
1104
1075
val pat1 = indexPattern(tree).transform(pat)
@@ -1535,19 +1506,38 @@ class Typer extends Namer
1535
1506
if (sym is Implicit ) checkImplicitConversionDefOK(sym)
1536
1507
val tpt1 = checkSimpleKinded(typedType(tpt))
1537
1508
1538
- var rhsCtx = ctx
1539
- if (sym.isConstructor && ! sym.isPrimaryConstructor && tparams1.nonEmpty) {
1540
- // for secondary constructors we need a context that "knows"
1541
- // that their type parameters are aliases of the class type parameters.
1542
- // See pos/i941.scala
1543
- rhsCtx = ctx.fresh.setFreshGADTBounds
1544
- (tparams1, sym.owner.typeParams).zipped.foreach { (tdef, tparam) =>
1545
- val tr = tparam.typeRef
1546
- rhsCtx.gadt.addBound(tdef.symbol, tr, isUpper = false )
1547
- rhsCtx.gadt.addBound(tdef.symbol, tr, isUpper = true )
1509
+ val rhsCtx : Context = {
1510
+ var _result : FreshContext = null
1511
+ def resultCtx (): FreshContext = {
1512
+ if (_result == null ) _result = ctx.fresh
1513
+ _result
1514
+ }
1515
+
1516
+ if (tparams1.nonEmpty) {
1517
+ resultCtx().setFreshGADTBounds
1518
+ if (! sym.isConstructor) {
1519
+ // if we're _not_ in a constructor, allow constraining type parameters
1520
+ tparams1.foreach { tdef =>
1521
+ val tb @ TypeBounds (lo, hi) = tdef.symbol.info.bounds
1522
+ resultCtx().gadt.addBound(tdef.symbol, lo, isUpper = false )
1523
+ resultCtx().gadt.addBound(tdef.symbol, hi, isUpper = true )
1524
+ }
1525
+ } else if (! sym.isPrimaryConstructor) {
1526
+ // otherwise, for secondary constructors we need a context that "knows"
1527
+ // that their type parameters are aliases of the class type parameters.
1528
+ // See pos/i941.scala
1529
+ (tparams1, sym.owner.typeParams).zipped.foreach { (tdef, tparam) =>
1530
+ val tr = tparam.typeRef
1531
+ resultCtx().gadt.addBound(tdef.symbol, tr, isUpper = false )
1532
+ resultCtx().gadt.addBound(tdef.symbol, tr, isUpper = true )
1533
+ }
1534
+ }
1548
1535
}
1536
+
1537
+ if (sym.isInlineMethod) resultCtx().addMode(Mode .InlineableBody )
1538
+
1539
+ if (_result ne null ) _result else ctx
1549
1540
}
1550
- if (sym.isInlineMethod) rhsCtx = rhsCtx.addMode(Mode .InlineableBody )
1551
1541
val rhs1 = typedExpr(ddef.rhs, tpt1.tpe)(rhsCtx)
1552
1542
1553
1543
if (sym.isInlineMethod) PrepareInlineable .registerInlineInfo(sym, ddef.rhs, _ => rhs1)
0 commit comments