Skip to content

Commit a1042c1

Browse files
authored
Merge pull request #12362 from dotty-staging/fix-infix-type-printing
Print infix types as infix
2 parents 782f57e + 4cfbd36 commit a1042c1

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Diff for: compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package printing
34

45
import core._
@@ -166,13 +167,17 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
166167
~ " "
167168
~ toText(appType.resultType)
168169

169-
def isInfixType(tp: Type): Boolean = tp match {
170+
def isInfixType(tp: Type): Boolean = tp match
170171
case AppliedType(tycon, args) =>
171-
args.length == 2 &&
172-
tycon.typeSymbol.getAnnotation(defn.ShowAsInfixAnnot).map(_.argumentConstant(0).forall(_.booleanValue))
173-
.getOrElse(!Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head))
172+
args.length == 2
173+
&& {
174+
val sym = tycon.typeSymbol
175+
sym.is(Infix)
176+
|| sym.getAnnotation(defn.ShowAsInfixAnnot)
177+
.exists(_.argumentConstant(0).forall(_.booleanValue))
178+
|| !Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head)
179+
}
174180
case _ => false
175-
}
176181

177182
def tyconName(tp: Type): Name = tp.typeSymbol.name
178183
def checkAssocMismatch(tp: Type, isRightAssoc: Boolean) = tp match {

Diff for: compiler/test-resources/type-printer/infix

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def foo: Int Mappy Boolean && String
4040
scala> @scala.annotation.showAsInfix(false) class ||[T,U]
4141
// defined class ||
4242
scala> def foo: Int || Boolean = ???
43-
def foo: ||[Int, Boolean]
43+
def foo: Int || Boolean
4444
scala> def foo: Int && Boolean & String = ???
4545
def foo: Int && Boolean & String
4646
scala> def foo: (Int && Boolean) & String = ???

Diff for: tests/neg/print-infix-type.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/print-infix-type.scala:8:29 ---------------------------------------------------
2+
8 | val x: over[String, Int] = f // error
3+
| ^
4+
| Found: Int over String
5+
| Required: String over Int
6+
7+
longer explanation available when compiling with `-explain`

Diff for: tests/neg/print-infix-type.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object A:
2+
3+
opaque infix type over[A, B] = (A, B)
4+
def f: over[Int, String] = (1, "")
5+
6+
object B:
7+
import A.*
8+
val x: over[String, Int] = f // error

0 commit comments

Comments
 (0)