@@ -932,11 +932,19 @@ class Typer extends Namer
932932 isContextual = funFlags.is(Given ), isErased = funFlags.is(Erased ))
933933
934934 /** Typechecks dependent function type with given parameters `params` */
935- def typedDependent (params : List [ValDef ])(implicit ctx : Context ): Tree =
935+ def typedDependent (params : List [untpd.ValDef ])(implicit ctx : Context ): Tree =
936+ val fixThis = new untpd.UntypedTreeMap :
937+ // pretype all references of this in outer context,
938+ // so that they do not refer to the refined type being constructed
939+ override def transform (tree : untpd.Tree )(using Context ): untpd.Tree = tree match
940+ case This (id) => untpd.TypedSplice (typedExpr(tree)(using ctx.outer))
941+ case _ => super .transform(tree)
942+
936943 val params1 =
937944 if funFlags.is(Given ) then params.map(_.withAddedFlags(Given ))
938945 else params
939- val appDef0 = untpd.DefDef (nme.apply, Nil , List (params1), body, EmptyTree ).withSpan(tree.span)
946+ val params2 = params1.map(fixThis.transformSub)
947+ val appDef0 = untpd.DefDef (nme.apply, Nil , List (params2), body, EmptyTree ).withSpan(tree.span)
940948 index(appDef0 :: Nil )
941949 val appDef = typed(appDef0).asInstanceOf [DefDef ]
942950 val mt = appDef.symbol.info.asInstanceOf [MethodType ]
@@ -947,10 +955,11 @@ class Typer extends Namer
947955 val tycon = TypeTree (funCls.typeRef)
948956 val core = AppliedTypeTree (tycon, typeArgs)
949957 RefinedTypeTree (core, List (appDef), ctx.owner.asClass)
958+ end typedDependent
950959
951960 args match {
952961 case ValDef (_, _, _) :: _ =>
953- typedDependent(args.asInstanceOf [List [ValDef ]])(
962+ typedDependent(args.asInstanceOf [List [untpd. ValDef ]])(
954963 ctx.fresh.setOwner(ctx.newRefinedClassSymbol(tree.span)).setNewScope)
955964 case _ =>
956965 typed(cpy.AppliedTypeTree (tree)(untpd.TypeTree (funCls.typeRef), args :+ body), pt)
0 commit comments