Skip to content

Commit acc93bb

Browse files
committed
Fix printing of poly function types
1 parent c226577 commit acc93bb

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,25 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
158158
argStr ~ " " ~ arrow(isGiven) ~ " " ~ argText(args.last)
159159
}
160160

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

170181
def isInfixType(tp: Type): Boolean = tp match
171182
case AppliedType(tycon, args) =>
@@ -229,8 +240,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
229240
if !printDebug && appliedText(tp.asInstanceOf[HKLambda].resType).isEmpty =>
230241
// don't eta contract if the application would be printed specially
231242
toText(tycon)
232-
case tp: RefinedType if defn.isFunctionType(tp) && !printDebug =>
233-
toTextDependentFunction(tp.refinedInfo.asInstanceOf[MethodType])
243+
case tp: RefinedType
244+
if (defn.isFunctionType(tp) || (tp.parent.typeSymbol eq defn.PolyFunctionClass))
245+
&& !printDebug =>
246+
toTextMethodAsFunction(tp.refinedInfo)
234247
case tp: TypeRef =>
235248
if (tp.symbol.isAnonymousClass && !showUniqueIds)
236249
toText(tp.info)
@@ -244,6 +257,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
244257
case ErasedValueType(tycon, underlying) =>
245258
"ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
246259
case tp: ClassInfo =>
260+
if tp.cls.derivesFrom(defn.PolyFunctionClass) then
261+
tp.member(nme.apply).info match
262+
case info: PolyType => return toTextMethodAsFunction(info)
263+
case _ =>
247264
toTextParents(tp.parents) ~~ "{...}"
248265
case JavaArrayType(elemtp) =>
249266
toText(elemtp) ~ "[]"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/polymorphic-functions1.scala:1:53 ---------------------------------------------
2+
1 |val f: [T] => (x: T) => x.type = [T] => (x: Int) => x // error
3+
| ^
4+
| Found: [T] => (Int) => Int
5+
| Required: [T] => (x: T) => x.type
6+
7+
longer explanation available when compiling with `-explain`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val f: [T] => (x: T) => x.type = [T] => (x: Int) => x // error

0 commit comments

Comments
 (0)