From c2928329660b7595682ddc4096e67950b58df2fe Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 7 May 2021 10:08:47 +0200 Subject: [PATCH 1/2] Print infix types as infix No showAsInfix annotation needed for them to be printed in infix form. --- .../tools/dotc/printing/RefinedPrinter.scala | 17 +++++++++++------ tests/neg/print-infix-type.check | 7 +++++++ tests/neg/print-infix-type.scala | 8 ++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 tests/neg/print-infix-type.check create mode 100644 tests/neg/print-infix-type.scala diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 541cb64ecc41..d51a28a2c51f 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -1,4 +1,5 @@ -package dotty.tools.dotc +package dotty.tools +package dotc package printing import core._ @@ -166,13 +167,17 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { ~ " " ~ toText(appType.resultType) - def isInfixType(tp: Type): Boolean = tp match { + def isInfixType(tp: Type): Boolean = tp match case AppliedType(tycon, args) => - args.length == 2 && - tycon.typeSymbol.getAnnotation(defn.ShowAsInfixAnnot).map(_.argumentConstant(0).forall(_.booleanValue)) - .getOrElse(!Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head)) + args.length == 2 + && { + val sym = tycon.typeSymbol + sym.is(Infix) + || sym.getAnnotation(defn.ShowAsInfixAnnot) + .exists(_.argumentConstant(0).forall(_.booleanValue)) + || !Character.isUnicodeIdentifierStart(tycon.typeSymbol.name.toString.head) + } case _ => false - } def tyconName(tp: Type): Name = tp.typeSymbol.name def checkAssocMismatch(tp: Type, isRightAssoc: Boolean) = tp match { diff --git a/tests/neg/print-infix-type.check b/tests/neg/print-infix-type.check new file mode 100644 index 000000000000..bb96c0cbb5de --- /dev/null +++ b/tests/neg/print-infix-type.check @@ -0,0 +1,7 @@ +-- [E007] Type Mismatch Error: tests/neg/print-infix-type.scala:8:29 --------------------------------------------------- +8 | val x: over[String, Int] = f // error + | ^ + | Found: Int over String + | Required: String over Int + +longer explanation available when compiling with `-explain` diff --git a/tests/neg/print-infix-type.scala b/tests/neg/print-infix-type.scala new file mode 100644 index 000000000000..7ed71e34964d --- /dev/null +++ b/tests/neg/print-infix-type.scala @@ -0,0 +1,8 @@ +object A: + + opaque infix type over[A, B] = (A, B) + def f: over[Int, String] = (1, "") + +object B: + import A.* + val x: over[String, Int] = f // error From 4cfbd36d7918840d75928bcd557492b816245bee Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 7 May 2021 16:08:56 +0200 Subject: [PATCH 2/2] Fix test --- compiler/test-resources/type-printer/infix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test-resources/type-printer/infix b/compiler/test-resources/type-printer/infix index caac75e62fe2..a7904ae9ec43 100644 --- a/compiler/test-resources/type-printer/infix +++ b/compiler/test-resources/type-printer/infix @@ -40,7 +40,7 @@ def foo: Int Mappy Boolean && String scala> @scala.annotation.showAsInfix(false) class ||[T,U] // defined class || scala> def foo: Int || Boolean = ??? -def foo: ||[Int, Boolean] +def foo: Int || Boolean scala> def foo: Int && Boolean & String = ??? def foo: Int && Boolean & String scala> def foo: (Int && Boolean) & String = ???