@@ -208,6 +208,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
208208
209209 def DefDef (sym : TermSymbol , tparams : List [TypeSymbol ], vparamss : List [List [TermSymbol ]],
210210 resultType : Type , rhs : Tree )(implicit ctx : Context ): DefDef =
211+ sym.setParamss(tparams, vparamss)
211212 ta.assignType(
212213 untpd.DefDef (
213214 sym.name,
@@ -223,15 +224,27 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
223224 def DefDef (sym : TermSymbol , rhsFn : List [List [Tree ]] => Tree )(implicit ctx : Context ): DefDef =
224225 polyDefDef(sym, Function .const(rhsFn))
225226
227+ /** A DefDef with given method symbol `sym`.
228+ * @rhsFn A function from type parameter types and term parameter references
229+ * to the method's right-hand side.
230+ * Parameter symbols are taken from the `paramss` field of `sym`, or
231+ * are freshly generated if `paramss` is empty.
232+ */
226233 def polyDefDef (sym : TermSymbol , rhsFn : List [Type ] => List [List [Tree ]] => Tree )(implicit ctx : Context ): DefDef = {
227- val (tparams, mtp) = sym.info match {
234+
235+ val (tparams, existingParamss, mtp) = sym.info match {
228236 case tp : PolyType =>
229- val tparams = ctx.newTypeParams(sym, tp.paramNames, EmptyFlags , tp.instantiateParamInfos(_))
230- (tparams, tp.instantiate(tparams map (_.typeRef)))
231- case tp => (Nil , tp)
237+ val (tparams, existingParamss) = sym.paramss match
238+ case tparams :: vparamss =>
239+ assert(tparams.hasSameLengthAs(tp.paramNames) && tparams.head.isType)
240+ (tparams.asInstanceOf [List [TypeSymbol ]], vparamss)
241+ case _ =>
242+ (ctx.newTypeParams(sym, tp.paramNames, EmptyFlags , tp.instantiateParamInfos(_)), Nil )
243+ (tparams, existingParamss, tp.instantiate(tparams map (_.typeRef)))
244+ case tp => (Nil , sym.paramss, tp)
232245 }
233246
234- def valueParamss (tp : Type ): (List [List [TermSymbol ]], Type ) = tp match {
247+ def valueParamss (tp : Type , existingParamss : List [ List [ Symbol ]] ): (List [List [TermSymbol ]], Type ) = tp match {
235248 case tp : MethodType =>
236249 val isParamDependent = tp.isParamDependent
237250 val previousParamRefs = if (isParamDependent) mutable.ListBuffer [TermRef ]() else null
@@ -254,14 +267,23 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
254267 makeSym(origInfo)
255268 }
256269
257- val params = tp.paramNames.lazyZip(tp.paramInfos).map(valueParam)
258- val (paramss, rtp) = valueParamss(tp.instantiate(params map (_.termRef)))
270+ val (params, existingParamss1) =
271+ if tp.paramInfos.isEmpty then (Nil , existingParamss)
272+ else existingParamss match
273+ case vparams :: existingParamss1 =>
274+ assert(vparams.hasSameLengthAs(tp.paramNames) && vparams.head.isTerm)
275+ (vparams.asInstanceOf [List [TermSymbol ]], existingParamss1)
276+ case _ =>
277+ (tp.paramNames.lazyZip(tp.paramInfos).map(valueParam), Nil )
278+ val (paramss, rtp) =
279+ valueParamss(tp.instantiate(params map (_.termRef)), existingParamss1)
259280 (params :: paramss, rtp)
260281 case tp => (Nil , tp.widenExpr)
261282 }
262- val (vparamss, rtp) = valueParamss(mtp)
283+ val (vparamss, rtp) = valueParamss(mtp, existingParamss )
263284 val targs = tparams map (_.typeRef)
264285 val argss = vparamss.nestedMap(vparam => Ident (vparam.termRef))
286+ sym.setParamss(tparams, vparamss)
265287 DefDef (sym, tparams, vparamss, rtp, rhsFn(targs)(argss))
266288 }
267289
0 commit comments