8000 SI-4700 Types with symbolic names print in infix by default · Pull Request #5589 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

SI-4700 Types with symbolic names print in infix by default #5589

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 3 commits into from Feb 16, 2017
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
27 changes: 27 additions & 0 deletions src/library/scala/annotation/showAsInfix.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scala.annotation

/**
* This annotation configures how Scala prints two-parameter generic types.
*
* By default, types with symbolic names are printed infix; while types without
* them are printed using the regular generic type syntax.
*
* Example of usage:
{{{
scala> class Map[T, U]
defined class Map

scala> def foo: Int Map Int = ???
foo: Map[Int,Int]

scala> @showAsInfix class Map[T, U]
defined class Map

scala> def foo: Int Map Int = ???
foo: Int Map Int
}}}
*
* @param enabled whether to show this type as an infix type operator.
* @since 2.12.2
*/
class showAsInfix(enabled: Boolean = true) extends annotation.StaticAnnotation
5 changes: 3 additions & 2 deletions src/reflect/scala/reflect/internal/AnnotationInfos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
/** Check whether any of the arguments mention a symbol */
def refsSymbol(sym: Symbol) = hasArgWhich(_.symbol == sym)

def stringArg(index: Int) = constantAtIndex(index) map (_.stringValue)
def intArg(index: Int) = constantAtIndex(index) map (_.intValue)
def stringArg(index: Int) = constantAtIndex(index) map (_.stringValue)
def intArg(index: Int) = constantAtIndex(index) map (_.intValue)
def booleanArg(index: Int) = constantAtIndex(index) map (_.booleanValue)
def symbolArg(index: Int) = argAtIndex(index) collect {
case Apply(fun, Literal(str) :: Nil) if fun.symbol == definitions.Symbol_apply =>
newTermName(str.stringValue)
Expand Down
2 changes: 2 additions & 0 deletions src/reflect/scala/reflect/internal/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,8 @@ trait Definitions extends api.StandardDefinitions {
case _ => false
}

lazy val ShowAsInfixAnnotationClass = rootMirror.getClassIfDefined("scala.annotation.showAsInfix")

// todo: reconcile with javaSignature!!!
def signature(tp: Type): String = {
def erasure(tp: Type): Type = tp match {
Expand Down
30 changes: 30 additions & 0 deletions src/reflect/scala/reflect/internal/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ trait Types
/** Is this type completed (i.e. not a lazy type)? */
def isComplete: Boolean = true

/** Should this be printed as an infix type (@showAsInfix class &&[T, U])? */
def isShowAsInfixType: Boolean = false

/** If this is a lazy type, assign a new type to `sym`. */
def complete(sym: Symbol) {}

Expand Down Expand Up @@ -2097,6 +2100,15 @@ trait Types
trivial = fromBoolean(!sym.isTypeParameter && pre.isTrivial && areTrivialTypes(args))
toBoolean(trivial)
}

/* It only makes sense to show 2-ary type constructors infix.
* By default we do only if it's a symbolic name. */
override def isShowAsInfixType: Boolean =
hasLength(args, 2) &&
sym.getAnnotation(ShowAsInfixAnnotationClass)
.map(_ booleanArg 0 getOrElse true)
.getOrElse(!Character.isUnicodeIdentifierStart(sym.decodedName.head))

private[Types] def invalidateTypeRefCaches(): Unit = {
parentsCache = null
parentsPeriod = NoPeriod
Expand Down Expand Up @@ -2322,6 +2334,22 @@ trait Types
case arg :: Nil => s"($arg,)"
case _ => args.mkString("(", ", ", ")")
}
private def infixTypeString: String = {
/* SLS 3.2.8: all infix types have the same precedence.
* In A op B op' C, op and op' need the same associativity.
* Therefore, if op is left associative, anything on its right
* needs to be parenthesized if it's an infix type, and vice versa. */
// we should only get here after `isShowInfixType` says we have 2 args
val l :: r :: Nil = args

val isRightAssoc = typeSymbol.decodedName endsWith ":"

val lstr = if (isRightAssoc && l.isShowAsInfixType) s"($l)" else l.toString

val rstr = if (!isRightAssoc && r.isShowAsInfixType) s"($r)" else r.toString

s"$lstr ${sym.decodedName} $rstr"
}
private def customToString = sym match {
case RepeatedParamClass | JavaRepeatedParamClass => args.head + "*"
case ByNameParamClass => "=> " + args.head
Expand All @@ -2345,6 +2373,8 @@ trait Types
xs.init.mkString("(", ", ", ")") + " => " + xs.last
}
}
else if (isShowAsInfixType)
infixTypeString
else if (isTupleTypeDirect(this))
tupleTypeString
else if (sym.isAliasType && prefixChain.exists(_.termSymbol.isSynthetic) && (this ne dealias))
Expand Down
1 change: 1 addition & 0 deletions src/reflect/scala/reflect/runtime/JavaUniverseForce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse =>
definitions.hijackedCoreClasses
definitions.symbolsNotPresentInBytecode
definitions.isPossibleSyntheticParent
definitions.ShowAsInfixAnnotationClass
definitions.abbrvTag
definitions.numericWeight
definitions.boxedModule
Expand Down
44 changes: 44 additions & 0 deletions test/files/run/t4700.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

scala> import scala.annotation.showAsInfix
import scala.annotation.showAsInfix

scala> class &&[T,U]
defined class $amp$amp

scala> def foo: Int && Boolean = ???
foo: Int && Boolean

scala> def foo: Int && Boolean && String = ???
foo: Int && Boolean && String

scala> def foo: Int && (Boolean && String) = ???
foo: Int && (Boolean && String)

scala> @showAsInfix type Mappy[T, U] = Map[T, U]
defined type alias Mappy

scala> def foo: Int Mappy (Boolean && String) = ???
foo: Int Mappy (Boolean && String)

scala> @showAsInfix(false) class ||[T,U]
defined class $bar$bar

scala> def foo: Int || Boolean = ???
foo: ||[Int,Boolean]

scala> class &:[L, R]
defined class $amp$colon

scala> def foo: Int &: String = ???
foo: Int &: String

scala> def foo: Int &: Boolean &: String = ???
foo: Int &: Boolean &: String

scala> def foo: (Int && String) &: Boolean = ???
foo: (Int && String) &: Boolean

scala> def foo: Int && (Boolean &: String) = ???
foo: Int && (Boolean &: String)

scala> :quit
22 changes: 22 additions & 0 deletions test/files/run/t4700.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.tools.nsc.interpreter._
import scala.tools.partest.ReplTest

object Test extends ReplTest {
def code = """
|import scala.annotation.showAsInfix
|class &&[T,U]
|def foo: Int && Boolean = ???
|def foo: Int && Boolean && String = ???
|def foo: Int && (Boolean && String) = ???
|@showAsInfix type Mappy[T, U] = Map[T, U]
|def foo: Int Mappy (Boolean && String) = ???
|@showAsInfix(false) class ||[T,U]
|def foo: Int || Boolean = ???
|class &:[L, R]
|def foo: Int &: String = ???
|def foo: Int &: Boolean &: String = ???
|def foo: (Int && String) &: Boolean = ???
|def foo: Int && (Boolean &: String) = ???
|""".stripMargin
}

0