Skip to content

Print infix types as infix #12362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package printing

import core._
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/type-printer/infix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ???
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/print-infix-type.check
Original file line number Diff line number Diff line change
@@ -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`
8 changes: 8 additions & 0 deletions tests/neg/print-infix-type.scala
Original file line number Diff line number Diff line change
@@ -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