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