Skip to content

Commit c771374

Browse files
committed
Fix printing of infix types
- move isInfixType back to RefinedPrinter. Types is already large enough and this is exclusively about printing. - perform argText adaptation of InfixType arguments.
1 parent 8023db1 commit c771374

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ object Types {
149149
false
150150
}
151151

152-
def isInfixType(implicit ctx: Context): Boolean = this match {
153-
case AppliedType(tycon, args) =>
154-
args.length == 2 &&
155-
!Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head)
156-
// TODO: Once we use the 2.12 stdlib, also check the @showAsInfix annotation
157-
case _ => false
158-
}
159-
160152
/** Does this type refer exactly to class symbol `sym`, instead of to a subclass of `sym`?
161153
* Implemented like `isRef`, but follows more types: all type proxies as well as and- and or-types
162154
*/

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,23 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
125125
("implicit " provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
126126
}
127127

128+
def isInfixType(tp: Type): Boolean = tp match {
129+
case AppliedType(tycon, args) =>
130+
args.length == 2 &&
131+
!Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head)
132+
// TODO: Once we use the 2.12 stdlib, also check the @showAsInfix annotation
133+
case _ => false
134+
}
135+
128136
def toTextInfixType(op: Type, args: List[Type]): Text = {
129137
/* SLS 3.2.8: all infix types have the same precedence.
130138
* In A op B op' C, op and op' need the same associativity.
131139
* Therefore, if op is left associative, anything on its right
132140
* needs to be parenthesized if it's an infix type, and vice versa. */
133141
val l :: r :: Nil = args
134142
val isRightAssoc = op.typeSymbol.name.endsWith(":")
135-
val leftArg = if (isRightAssoc && l.isInfixType) "(" ~ toText(l) ~ ")" else toText(l)
136-
val rightArg = if (!isRightAssoc && r.isInfixType) "(" ~ toText(r) ~ ")" else toText(r)
143+
val leftArg = if (isRightAssoc && isInfixType(l)) "(" ~ argText(l) ~ ")" else argText(l)
144+
val rightArg = if (!isRightAssoc && isInfixType(r)) "(" ~ argText(r) ~ ")" else argText(r)
137145

138146
leftArg ~ " " ~ toTextLocal(op) ~ " " ~ rightArg
139147
}
@@ -146,7 +154,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
146154
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
147155
if (defn.isFunctionClass(cls)) return toTextFunction(args, cls.name.isImplicitFunction)
148156
if (defn.isTupleClass(cls)) return toTextTuple(args)
149-
if (tp.isInfixType) return toTextInfixType(tycon, args)
157+
if (isInfixType(tp)) return toTextInfixType(tycon, args)
150158
case EtaExpansion(tycon) =>
151159
return toText(tycon)
152160
case tp: TypeRef =>

0 commit comments

Comments
 (0)