Skip to content

Commit 61d66c3

Browse files
committed
Avoid eta contractions when printing
Don't eta contract if the result type of a lambda is an applied type that's treated specially.
1 parent b22331e commit 61d66c3

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,33 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
192192
}
193193
}
194194

195-
homogenize(tp) match {
195+
def appliedText(tp: Type): Text = tp match
196196
case tp @ AppliedType(tycon, args) =>
197197
val cls = tycon.typeSymbol
198-
if (tycon.isRepeatedParam) toTextLocal(args.head) ~ "*"
199-
else if (defn.isFunctionClass(cls)) toTextFunction(args, cls.name.isContextFunction, cls.name.isErasedFunction)
200-
else if (tp.tupleArity >= 2 && !printDebug) toTextTuple(tp.tupleElementTypes)
201-
else if (isInfixType(tp)) {
198+
if tycon.isRepeatedParam then toTextLocal(args.head) ~ "*"
199+
else if defn.isFunctionClass(cls) then toTextFunction(args, cls.name.isContextFunction, cls.name.isErasedFunction)
200+
else if tp.tupleArity >= 2 && !printDebug then toTextTuple(tp.tupleElementTypes)
201+
else if isInfixType(tp) then
202202
val l :: r :: Nil = args
203203
val opName = tyconName(tycon)
204204
toTextInfixType(tyconName(tycon), l, r) { simpleNameString(tycon.typeSymbol) }
205-
}
206-
else super.toText(tp)
205+
else Str("")
206+
case _ =>
207+
Str("")
207208

209+
homogenize(tp) match {
210+
case tp: AppliedType =>
211+
val refined = appliedText(tp)
212+
if refined.isEmpty then super.toText(tp) else refined
208213
// Since RefinedPrinter, unlike PlainPrinter, can output right-associative type-operators, we must override handling
209214
// of AndType and OrType to account for associativity
210215
case AndType(tp1, tp2) =>
211216
toTextInfixType(tpnme.raw.AMP, tp1, tp2) { toText(tpnme.raw.AMP) }
212217
case OrType(tp1, tp2) =>
213218
toTextInfixType(tpnme.raw.BAR, tp1, tp2) { toText(tpnme.raw.BAR) }
214-
case EtaExpansion(tycon) if !printDebug =>
219+
case tp @ EtaExpansion(tycon)
220+
if !printDebug && appliedText(tp.asInstanceOf[HKLambda].resType).isEmpty =>
221+
// don't eta contract if the application would be printed specially
215222
toText(tycon)
216223
case tp: RefinedType if defn.isFunctionType(tp) && !printDebug =>
217224
toTextDependentFunction(tp.refinedInfo.asInstanceOf[MethodType])

tests/neg/i9958.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i9958.scala:1:30 -----------------------------------------------------------------------------------
2+
1 |val x = summon[[X] =>> (X, X)] // error
3+
| ^
4+
| no implicit argument of type [X] =>> (X, X) was found for parameter x of method summon in object DottyPredef

tests/neg/i9958.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val x = summon[[X] =>> (X, X)] // error

0 commit comments

Comments
 (0)