Skip to content

Commit e8bc462

Browse files
committed
Fix printing of function type trees
1 parent ddb9c01 commit e8bc462

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

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

+34-10
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,29 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
159159
argStr ~ " " ~ arrow(isGiven) ~ " " ~ argText(args.last)
160160
}
161161

162-
def toTextDependentFunction(appType: MethodType): Text =
163-
"("
164-
~ keywordText("erased ").provided(appType.isErasedMethod)
165-
~ paramsText(appType)
166-
~ ") "
167-
~ arrow(appType.isImplicitMethod)
168-
~ " "
169-
~ toText(appType.resultType)
162+
def toTextMethodAsFunction(info: Type): Text = info match
163+
case info: MethodType =>
164+
changePrec(GlobalPrec) {
165+
"("
166+
~ keywordText("erased ").provided(info.isErasedMethod)
167+
~ ( if info.isParamDependent || info.isResultDependent
168+
then paramsText(info)
169+
else argsText(info.paramInfos)
170+
)
171+
~ ") "
172+
~ arrow(info.isImplicitMethod)
173+
~ " "
174+
~ toTextMethodAsFunction(info.resultType)
175+
}
176+
case info: PolyType =>
177+
changePrec(GlobalPrec) {
178+
"["
179+
~ paramsText(info)
180+
~ "] => "
181+
~ toTextMethodAsFunction(info.resultType)
182+
}
183+
case _ =>
184+
toText(info)
170185

171186
def isInfixType(tp: Type): Boolean = tp match
172187
case AppliedType(tycon, args) =>
@@ -230,8 +245,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
230245
if !printDebug && appliedText(tp.asInstanceOf[HKLambda].resType).isEmpty =>
231246
// don't eta contract if the application would be printed specially
232247
toText(tycon)
233-
case tp: RefinedType if defn.isFunctionType(tp) && !printDebug =>
234-
toTextDependentFunction(tp.refinedInfo.asInstanceOf[MethodType])
248+
case tp: RefinedType
249+
if (defn.isFunctionType(tp) || (tp.parent.typeSymbol eq defn.PolyFunctionClass))
250+
&& !printDebug =>
251+
toTextMethodAsFunction(tp.refinedInfo)
235252
case tp: TypeRef =>
236253
if (tp.symbol.isAnonymousClass && !showUniqueIds)
237254
toText(tp.info)
@@ -245,6 +262,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
245262
case ErasedValueType(tycon, underlying) =>
246263
"ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
247264
case tp: ClassInfo =>
265+
if tp.cls.derivesFrom(defn.PolyFunctionClass) then
266+
tp.member(nme.apply).info match
267+
case info: PolyType => return toTextMethodAsFunction(info)
268+
case _ =>
248269
toTextParents(tp.parents) ~~ "{...}"
249270
case JavaArrayType(elemtp) =>
250271
toText(elemtp) ~ "[]"
@@ -511,6 +532,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
511532
changePrec(OrTypePrec) { toText(args(0)) ~ " | " ~ atPrec(OrTypePrec + 1) { toText(args(1)) } }
512533
else if (tpt.symbol == defn.andType && args.length == 2)
513534
changePrec(AndTypePrec) { toText(args(0)) ~ " & " ~ atPrec(AndTypePrec + 1) { toText(args(1)) } }
535+
else if defn.isFunctionClass(tpt.symbol)
536+
&& tpt.isInstanceOf[TypeTree] && tree.hasType && !printDebug
537+
then changePrec(GlobalPrec) { toText(tree.typeOpt) }
514538
else args match
515539
case arg :: _ if arg.isTerm =>
516540
toTextLocal(tpt) ~ "(" ~ Text(args.map(argText), ", ") ~ ")"

0 commit comments

Comments
 (0)