Skip to content

Commit dee65e2

Browse files
committed
Fix #6745: Fix handling of this in dependent function types
1 parent c6b681c commit dee65e2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/pos/i6745.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trait Foo { self =>
2+
type M
3+
def apply(prog: (h: this.type) => h.M): M = prog(this)
4+
}

0 commit comments

Comments
 (0)