Skip to content

Commit 360dcad

Browse files
authored
Merge pull request #6441 from dotty-staging/fix-#4509
Fix #4509: Eta-expand as erased contextual
2 parents 9a3db9f + 363050d commit 360dcad

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,10 @@ object desugar {
11591159
Function(param :: Nil, Block(vdefs, body))
11601160
}
11611161

1162-
def makeContextualFunction(formals: List[Type], body: Tree)(implicit ctx: Context): Tree = {
1163-
val params = makeImplicitParameters(formals.map(TypeTree), Given)
1164-
new FunctionWithMods(params, body, Modifiers(Implicit | Given))
1162+
def makeContextualFunction(formals: List[Type], body: Tree, isErased: Boolean)(implicit ctx: Context): Tree = {
1163+
val mods = if (isErased) Given | Erased else Given
1164+
val params = makeImplicitParameters(formals.map(TypeTree), mods)
1165+
new FunctionWithMods(params, body, Modifiers(Implicit | mods))
11651166
}
11661167

11671168
/** Add annotation to tree:

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
232232
case dummyTreeOfType(tp) :: Nil if !(tp isRef defn.NullClass) => "null: " ~ toText(tp)
233233
case _ => toTextGlobal(args, ", ")
234234
}
235-
return "[applied to " ~ (Str("given ") provided tp.isContextual) ~ "(" ~ argsText ~ ") returning " ~ toText(resultType) ~ "]"
235+
return "[applied to " ~ (Str("given ") provided tp.isContextual) ~ (Str("erased ") provided tp.isErasedMethod) ~ "(" ~ argsText ~ ") returning " ~ toText(resultType) ~ "]"
236236
case IgnoredProto(ignored) =>
237237
return "?" ~ (("(ignored: " ~ toText(ignored) ~ ")") provided ctx.settings.verbose.value)
238238
case tp @ PolyProto(targs, resType) =>
@@ -529,10 +529,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
529529
case Function(args, body) =>
530530
var implicitSeen: Boolean = false
531531
var contextual: Boolean = false
532+
var isErased: Boolean = false
532533
def argToText(arg: Tree) = arg match {
533534
case arg @ ValDef(name, tpt, _) =>
534535
val implicitText =
535536
if ((arg.mods is Given)) { contextual = true; "" }
537+
else if ((arg.mods is Erased)) { isErased = true; "" }
536538
else if ((arg.mods is Implicit) && !implicitSeen) { implicitSeen = true; keywordStr("implicit ") }
537539
else ""
538540
implicitText ~ toText(name) ~ optAscription(tpt)
@@ -545,6 +547,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
545547
}
546548
changePrec(GlobalPrec) {
547549
(keywordText("given ") provided contextual) ~
550+
(keywordText("erased ") provided isErased) ~
548551
argsText ~ " => " ~ toText(body)
549552
}
550553
case InfixOp(l, op, r) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ class Typer extends Namer
21842184

21852185
protected def makeContextualFunction(tree: untpd.Tree, pt: Type)(implicit ctx: Context): Tree = {
21862186
val defn.FunctionOf(formals, _, true, _) = pt.dropDependentRefinement
2187-
val ifun = desugar.makeContextualFunction(formals, tree)
2187+
val ifun = desugar.makeContextualFunction(formals, tree, defn.isErasedFunctionType(pt))
21882188
typr.println(i"make contextual function $tree / $pt ---> $ifun")
21892189
typed(ifun, pt)
21902190
}

tests/pos/i4509.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Main {
2+
def fun[T](op: given erased (Int) => T) = op given 0
3+
fun { }
4+
}

0 commit comments

Comments
 (0)