From 3422fdb0adec4c90e5a16c9002fa0a68eeb6a612 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 4 Nov 2020 15:22:08 +0100 Subject: [PATCH] Add support for Scala3 3.0.0-M1 --- build.sbt | 9 +- project/plugins.sbt | 2 +- .../pprint/SourceTypePrinter.scala | 239 +++++++++--------- .../mdoc/internal/pprint/TypePrinter.scala | 6 +- .../tests/worksheets/WorksheetSuite.scala | 29 ++- 5 files changed, 140 insertions(+), 145 deletions(-) rename runtime/src/main/{scala-0.26 => scala-3.0.0-M1}/pprint/SourceTypePrinter.scala (87%) diff --git a/build.sbt b/build.sbt index 958f561a1..39719c0d5 100644 --- a/build.sbt +++ b/build.sbt @@ -3,7 +3,7 @@ import scala.collection.mutable def scala212 = "2.12.12" def scala211 = "2.11.12" def scala213 = "2.13.3" -def scala3 = List("0.26.0", "0.27.0-RC1") +def scala3 = List("3.0.0-M1") def scalajs = "1.1.1" def scalajsBinaryVersion = "1" @@ -20,7 +20,8 @@ val isScala213 = Def.setting { } val isScala3 = Def.setting { - VersionNumber(scalaVersion.value).matchesSemVer(SemanticSelector("<=1.0.0 || >=3.0.0")) + // doesn't work well with >= 3.0.0 for `3.0.0-M1` + VersionNumber(scalaVersion.value).matchesSemVer(SemanticSelector("<=1.0.0 || >=2.99.0")) } val isScalaJs1 = Def.setting { @@ -109,7 +110,7 @@ lazy val sharedSettings = List( val V = new { val scalameta = "4.3.24" - val munit = "0.7.14" + val munit = "0.7.16" val coursier = "1.0.1" } @@ -180,7 +181,7 @@ lazy val mdoc = project libraryDependencies ++= crossSetting( scalaVersion.value, if3 = List( - "ch.epfl.lamp" %% "dotty-compiler" % scalaVersion.value, + "org.scala-lang" %% "scala3-compiler" % scalaVersion.value, ("org.scalameta" %% "scalameta" % V.scalameta) .excludeAll(excludePprint) .withDottyCompat(scalaVersion.value), diff --git a/project/plugins.sbt b/project/plugins.sbt index 0dc446c7c..c5552ef13 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -3,7 +3,7 @@ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0") addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.3") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.1.1") addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.18.0") -addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.4") +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.5") libraryDependencies ++= List( "org.jsoup" % "jsoup" % "1.12.1", diff --git a/runtime/src/main/scala-0.26/pprint/SourceTypePrinter.scala b/runtime/src/main/scala-3.0.0-M1/pprint/SourceTypePrinter.scala similarity index 87% rename from runtime/src/main/scala-0.26/pprint/SourceTypePrinter.scala rename to runtime/src/main/scala-3.0.0-M1/pprint/SourceTypePrinter.scala index e2e30cb97..32146248d 100644 --- a/runtime/src/main/scala-0.26/pprint/SourceTypePrinter.scala +++ b/runtime/src/main/scala-3.0.0-M1/pprint/SourceTypePrinter.scala @@ -12,61 +12,62 @@ import scala.tasty.reflect.Printer * This is code is copied from Dotty and until https://github.com/lampepfl/dotty/issues/9716 * is fixed it's the best way to print types we have. */ -class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlight: SyntaxHighlight) extends Printer[R] { - import tasty._ +class SourceTypePrinter[R <: Reflection & Singleton](val reflect: R)(syntaxHighlight: SyntaxHighlight) extends Printer[R] { + import reflect._ import syntaxHighlight._ - def showTree(tree: Tree)(using ctx: Context): String = + def showTree(tree: Tree): String = (new Buffer).printTree(tree).result() - def showTypeOrBounds(tpe: TypeOrBounds)(using ctx: Context): String = - (new Buffer).printTypeOrBound(tpe)(using None).result() - - def showConstant(const: Constant)(using ctx: Context): String = + def showType(tpe: TypeRepr): String = + (new Buffer).printType(tpe)(using None).result() + + def showConstant(const: Constant): String = (new Buffer).printConstant(const).result() - def showSymbol(symbol: Symbol)(using ctx: Context): String = + def showSymbol(symbol: Symbol): String = symbol.fullName - def showFlags(flags: Flags)(using ctx: Context): String = { + def showFlags(flags: Flags): String = { val flagList = List.newBuilder[String] - if (flags.is(Flags.Private)) flagList += "private" - if (flags.is(Flags.Protected)) flagList += "protected" if (flags.is(Flags.Abstract)) flagList += "abstract" - if (flags.is(Flags.Final)) flagList += "final" - if (flags.is(Flags.Sealed)) flagList += "sealed" + if (flags.is(Flags.Artifact)) flagList += "artifact" if (flags.is(Flags.Case)) flagList += "case" - if (flags.is(Flags.Implicit)) flagList += "implicit" + if (flags.is(Flags.CaseAccessor)) flagList += "caseAccessor" + if (flags.is(Flags.Contravariant)) flagList += "contravariant" + if (flags.is(Flags.Covariant)) flagList += "covariant" + if (flags.is(Flags.Enum)) flagList += "enum" if (flags.is(Flags.Erased)) flagList += "erased" - if (flags.is(Flags.Lazy)) flagList += "lazy" - if (flags.is(Flags.Override)) flagList += "override" + if (flags.is(Flags.ExtensionMethod)) flagList += "extension" + if (flags.is(Flags.FieldAccessor)) flagList += "accessor" + if (flags.is(Flags.Final)) flagList += "final" + if (flags.is(Flags.HasDefault)) flagList += "hasDefault" + if (flags.is(Flags.Implicit)) flagList += "implicit" if (flags.is(Flags.Inline)) flagList += "inline" - if (flags.is(Flags.Macro)) flagList += "macro" if (flags.is(Flags.JavaDefined)) flagList += "javaDefined" - if (flags.is(Flags.Static)) flagList += "javaStatic" - if (flags.is(Flags.Object)) flagList += "object" - if (flags.is(Flags.Trait)) flagList += "trait" + if (flags.is(Flags.Lazy)) flagList += "lazy" if (flags.is(Flags.Local)) flagList += "local" - if (flags.is(Flags.Synthetic)) flagList += "synthetic" - if (flags.is(Flags.Artifact)) flagList += "artifact" + if (flags.is(Flags.Macro)) flagList += "macro" + if (flags.is(Flags.ModuleClass)) flagList += "moduleClass" if (flags.is(Flags.Mutable)) flagList += "mutable" - if (flags.is(Flags.FieldAccessor)) flagList += "accessor" - if (flags.is(Flags.CaseAcessor)) flagList += "caseAccessor" - if (flags.is(Flags.Covariant)) flagList += "covariant" - if (flags.is(Flags.Contravariant)) flagList += "contravariant" - if (flags.is(Flags.Scala2X)) flagList += "scala2x" - if (flags.is(Flags.HasDefault)) flagList += "hasDefault" - if (flags.is(Flags.StableRealizable)) flagList += "stableRealizable" + if (flags.is(Flags.Object)) flagList += "object" + if (flags.is(Flags.Override)) flagList += "override" + if (flags.is(Flags.Package)) flagList += "package" if (flags.is(Flags.Param)) flagList += "param" if (flags.is(Flags.ParamAccessor)) flagList += "paramAccessor" - if (flags.is(Flags.Enum)) flagList += "enum" - if (flags.is(Flags.ModuleClass)) flagList += "moduleClass" + if (flags.is(Flags.Private)) flagList += "private" if (flags.is(Flags.PrivateLocal)) flagList += "private[this]" - if (flags.is(Flags.Package)) flagList += "package" + if (flags.is(Flags.Protected)) flagList += "protected" + if (flags.is(Flags.Scala2x)) flagList += "scala2x" + if (flags.is(Flags.Sealed)) flagList += "sealed" + if (flags.is(Flags.StableRealizable)) flagList += "stableRealizable" + if (flags.is(Flags.Static)) flagList += "javaStatic" + if (flags.is(Flags.Synthetic)) flagList += "synthetic" + if (flags.is(Flags.Trait)) flagList += "trait" flagList.result().mkString("/*", " ", "*/") } - private class Buffer(using ctx: Context) { + private class Buffer { private[this] val sb: StringBuilder = new StringBuilder @@ -158,7 +159,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } val parents1 = parents.filter { - case Apply(Select(New(tpt), _), _) => tpt.tpe.typeSymbol != ctx.requiredClass("java.lang.Object") + case Apply(Select(New(tpt), _), _) => tpt.tpe.typeSymbol != Symbol.requiredClass("java.lang.Object") case TypeSelect(Select(Ident("_root_"), "scala"), "Product") => false case TypeSelect(Select(Ident("_root_"), "scala"), "Serializable") => false case _ => true @@ -285,7 +286,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case While(cond, body) => (cond, body) match { - case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant(()))) => + case (Block(Block(Nil, body1) :: Nil, Block(Nil, cond1)), Literal(Constant.Unit())) => this += highlightKeyword("do ") printTree(body1) += highlightKeyword(" while ") inParens(printTree(cond1)) @@ -346,8 +347,8 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case This(id) => id match { - case Some(x) => - this += x.name.stripSuffix("$") += "." + case Some(name) => + this += name.stripSuffix("$") += "." case None => } this += "this" @@ -364,7 +365,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += "throw " printTree(expr) - case Apply(fn, args) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.exprQuote") => + case Apply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprQuote") => args.head match { case Block(stats, expr) => this += "'{" @@ -379,12 +380,12 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += "}" } - case TypeApply(fn, args) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.typeQuote") => + case TypeApply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.typeQuote") => this += "'[" printTypeTree(args.head) this += "]" - case Apply(fn, arg :: Nil) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.exprSplice") => + case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprSplice") => this += "${" printTree(arg) this += "}" @@ -423,12 +424,12 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case Super(qual, idOpt) => qual match { - case This(Some(Id(name))) => this += name += "." + case This(Some(name)) => this += name += "." case This(None) => } this += "super" for (id <- idOpt) - inSquare(this += id.name) + inSquare(this += id) this case Typed(term, tpt) => @@ -443,7 +444,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig inParens { printTree(term) this += (if (scala.internal.Chars.isOperatorPart(sb.last)) " : " else ": ") - def printTypeOrAnnots(tpe: Type): Unit = tpe match { + def printTypeOrAnnots(tpe: TypeRepr): Unit = tpe match { case AnnotatedType(tp, annot) if tp == term.tpe => printAnnotation(annot) case AnnotatedType(tp, annot) => @@ -510,11 +511,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case None => this } - - case Return(expr) => - this += "return " - printTree(expr) - + case Repeated(elems, _) => printTrees(elems, ", ") @@ -525,7 +522,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig printTypeTree(hi) case tpt: WildcardTypeTree => - printTypeOrBound(tpt.tpe) + printType(tpt.tpe) case tpt: TypeTree => printTypeTree(tpt) @@ -561,7 +558,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig while (it.hasNext) extractFlatStats(it.next()) extractFlatStats(expansion) - case Literal(Constant(())) => // ignore + case Literal(Constant.Unit()) => // ignore case stat => flatStats += stat } def extractFlatExpr(term: Term): Term = term match { @@ -589,7 +586,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig def printFlatBlock(stats: List[Statement], expr: Term)(using elideThis: Option[Symbol]): Buffer = { val (stats1, expr1) = flatBlock(stats, expr) val stats2 = stats1.filter { - case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner == ctx.requiredClass("scala.internal.Quoted.quoteTypeTag")) + case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.maybeOwner == Symbol.requiredClass("scala.internal.Quoted.quoteTypeTag")) case _ => true } if (stats2.isEmpty) { @@ -657,8 +654,8 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig def printTypeTrees(trees: List[TypeTree], sep: String)(using elideThis: Option[Symbol] = None): Buffer = printList(trees, sep, (t: TypeTree) => printTypeTree(t)) - def printTypes(trees: List[Type], sep: String)(using elideThis: Option[Symbol]): Buffer = { - def printSeparated(list: List[Type]): Unit = list match { + def printTypes(trees: List[TypeRepr], sep: String)(using elideThis: Option[Symbol]): Buffer = { + def printSeparated(list: List[TypeRepr]): Unit = list match { case Nil => case x :: Nil => printType(x) case x :: xs => @@ -723,12 +720,12 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this } - def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(using elideThis: Option[Symbol]): Buffer = { - def printSeparated(list: List[TypeOrBounds]): Unit = list match { + def printTypesOrBounds(types: List[TypeRepr], sep: String)(using elideThis: Option[Symbol]): Buffer = { + def printSeparated(list: List[TypeRepr]): Unit = list match { case Nil => - case x :: Nil => printTypeOrBound(x) + case x :: Nil => printType(x) case x :: xs => - printTypeOrBound(x) + printType(x) this += sep printSeparated(xs) } @@ -766,7 +763,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig argCons.rhs match { case rhs: TypeBoundsTree => printBoundsTree(rhs) case rhs: WildcardTypeTree => - printTypeOrBound(rhs.tpe) + printType(rhs.tpe) case rhs @ LambdaTypeTree(tparams, body) => def printParam(t: Tree /*TypeTree | TypeBoundsTree*/): Unit = t match { case t: TypeBoundsTree => printBoundsTree(t) @@ -855,7 +852,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } printedPrefix |= printProtectedOrPrivate(vdef) if (vdef.symbol.flags.is(Flags.Mutable)) this += highlightValDef("var ") - else if (printedPrefix || !vdef.symbol.flags.is(Flags.CaseAcessor)) this += highlightValDef("val ") + else if (printedPrefix || !vdef.symbol.flags.is(Flags.CaseAccessor)) this += highlightValDef("val ") } } end if @@ -929,7 +926,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case Typed(Ident("_"), tpt) => this += "_: " - printTypeOrBoundsTree(tpt) + printTypeTree(tpt) case v: Term => printTree(v) @@ -943,18 +940,18 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig inline private val qSc = '"' def printConstant(const: Constant): Buffer = const match { - case Constant(()) => this += highlightLiteral("()") - case Constant(null) => this += highlightLiteral("null") - case Constant(v: Boolean) => this += highlightLiteral(v.toString) - case Constant(v: Byte) => this += highlightLiteral(v.toString) - case Constant(v: Short) => this += highlightLiteral(v.toString) - case Constant(v: Int) => this += highlightLiteral(v.toString) - case Constant(v: Long) => this += highlightLiteral(v.toString + "L") - case Constant(v: Float) => this += highlightLiteral(v.toString + "f") - case Constant(v: Double) => this += highlightLiteral(v.toString) - case Constant(v: Char) => this += highlightString(s"${qc}${escapedChar(v)}${qc}") - case Constant(v: String) => this += highlightString(s"${qSc}${escapedString(v)}${qSc}") - case Constant.ClassTag(v) => + case Constant.Unit() => this += highlightLiteral("()") + case Constant.Null() => this += highlightLiteral("null") + case Constant.Boolean(v) => this += highlightLiteral(v.toString) + case Constant.Byte(v) => this += highlightLiteral(v.toString) + case Constant.Short(v) => this += highlightLiteral(v.toString) + case Constant.Int(v) => this += highlightLiteral(v.toString) + case Constant.Long(v) => this += highlightLiteral(v.toString + "L") + case Constant.Float(v) => this += highlightLiteral(v.toString + "f") + case Constant.Double(v) => this += highlightLiteral(v.toString) + case Constant.Char(v) => this += highlightString(s"${qc}${escapedChar(v)}${qc}") + case Constant.String(v) => this += highlightString(s"${qSc}${escapedString(v)}${qSc}") + case Constant.ClassOf(v) => this += "classOf" inSquare(printType(v)) } @@ -966,7 +963,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += " <: " printTypeTree(hi) case tpt: WildcardTypeTree => - printTypeOrBound(tpt.tpe) + printType(tpt.tpe) case tpt: TypeTree => printTypeTree(tpt) } @@ -982,12 +979,12 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig def printTypeTree(tree: TypeTree)(using elideThis: Option[Symbol] = None): Buffer = tree match { case Inferred() => // TODO try to move this logic into `printType` - def printTypeAndAnnots(tpe: Type): Buffer = tpe match { + def printTypeAndAnnots(tpe: TypeRepr): Buffer = tpe match { case AnnotatedType(tp, annot) => printTypeAndAnnots(tp) this += " " printAnnotation(annot) - case tpe: TypeRef if tpe.typeSymbol == ctx.requiredClass("scala.runtime.Null$") || tpe.typeSymbol == ctx.requiredClass("scala.runtime.Nothing$") => + case tpe: TypeRef if tpe.typeSymbol == Symbol.requiredClass("scala.runtime.Null$") || tpe.typeSymbol == Symbol.requiredClass("scala.runtime.Nothing$") => // scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names printType(tpe) case tpe: TermRef if tpe.termSymbol.isClassDef && tpe.termSymbol.name.endsWith("$") => @@ -1030,7 +1027,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case Annotated(tpt, annot) => val Annotation(ref, args) = annot ref.tpe match { - case tpe: TypeRef if tpe.typeSymbol == ctx.requiredClass("scala.annotation.internal.Repeated") => + case tpe: TypeRef if tpe.typeSymbol == Symbol.requiredClass("scala.annotation.internal.Repeated") => val Types.Sequence(tp) = tpt.tpe printType(tp) this += highlightTypeDef("*") @@ -1065,15 +1062,6 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } - def printTypeOrBound(tpe: TypeOrBounds)(using elideThis: Option[Symbol]): Buffer = tpe match { - case tpe@TypeBounds(lo, hi) => - this += "_ >: " - printType(lo) - this += " <: " - printType(hi) - case tpe: Type => printType(tpe) - } - /** Print type * * @param elideThis Shoud printing elide `C.this` for the given class `C`? @@ -1082,7 +1070,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig * Self type annotation and types in parent list should elide current class * prefix `C.this` to avoid type checking errors. */ - def printType(tpe: Type)(using elideThis: Option[Symbol] = None): Buffer = tpe match { + def printType(tpe: TypeRepr)(using elideThis: Option[Symbol] = None): Buffer = tpe match { case ConstantType(const) => printConstant(const) @@ -1103,7 +1091,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig printType(tp) this += ")" inSquare(printTypesOrBounds(args, ", ")) - case tp: TypeRef if tp.typeSymbol == ctx.requiredClass("scala.") => + case tp: TypeRef if tp.typeSymbol == Symbol.requiredClass("scala.") => this += "_*" case _ => printType(tp) @@ -1153,7 +1141,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case TypeLambda(paramNames, tparams, body) => inSquare(printMethodicTypeParams(paramNames, tparams)) this += highlightTypeDef(" => ") - printTypeOrBound(body) + printType(body) case ParamRef(lambda, idx) => lambda match { @@ -1171,45 +1159,50 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case tpe: MethodType => this += "(" printList(tpe.paramNames.zip(tpe.paramTypes), ", ", - (x: (String, Type)) => (this += x._1 += ": ").printType(x._2)) + (x: (String, TypeRepr)) => (this += x._1 += ": ").printType(x._2)) this += ")" printType(tpe.resType) case tpe: PolyType => this += "[" printList(tpe.paramNames.zip(tpe.paramBounds), ", ", - (x: (String, TypeBounds)) => (this += x._1 += " ").printTypeOrBound(x._2)) + (x: (String, TypeBounds)) => (this += x._1 += " ").printType(x._2)) this += "]" printType(tpe.resType) case tpe: TypeLambda => this += "[" printList(tpe.paramNames.zip(tpe.paramBounds), ", ", - (x: (String, TypeBounds)) => (this += x._1 += " ").printTypeOrBound(x._2)) + (x: (String, TypeBounds)) => (this += x._1 += " ").printType(x._2)) this += "] => " printType(tpe.resType) + case tpe@TypeBounds(lo, hi) => + this += "_ >: " + printType(lo) + this += " <: " + printType(hi) + case _ => throw new MatchError(tpe.showExtractors) } def printImportSelector(sel: ImportSelector): Buffer = sel match { - case SimpleSelector(Id(name)) => this += name - case OmitSelector(Id(name)) => this += name += " => _" - case RenameSelector(Id(name), Id(newName)) => this += name += " => " += newName + case SimpleSelector(name) => this += name + case OmitSelector(name) => this += name += " => _" + case RenameSelector(name, newName) => this += name += " => " += newName } - def printDefinitionName(sym: Definition): Buffer = sym match { + def printDefinitionName(tree: Definition): Buffer = tree match { case ValDef(name, _, _) => this += highlightValDef(name) case DefDef(name, _, _, _, _) => this += highlightValDef(name) case ClassDef(name, _, _, _, _, _) => this += highlightTypeDef(name.stripSuffix("$")) case TypeDef(name, _) => this += highlightTypeDef(name) - case PackageDef(name, _) => this += highlightTypeDef(name) } def printAnnotation(annot: Term)(using elideThis: Option[Symbol]): Buffer = { val Annotation(ref, args) = annot - if (annot.symbol.maybeOwner == ctx.requiredClass("scala.internal.quoted.showName")) this + if (annot.symbol.maybeOwner == Symbol.requiredClass("scala.internal.quoted.showName")) this else { this += "@" printTypeTree(ref) @@ -1224,8 +1217,8 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig val annots = definition.symbol.annots.filter { case Annotation(annot, _) => val sym = annot.tpe.typeSymbol - sym != ctx.requiredClass("scala.forceInline") && - sym.maybeOwner != ctx.requiredPackage("scala.annotation.internal") + sym != Symbol.requiredClass("scala.forceInline") && + sym.maybeOwner != Symbol.requiredPackage("scala.annotation.internal") case x => throw new MatchError(x.showExtractors) } printAnnotations(annots) @@ -1233,8 +1226,8 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig else this } - def printRefinement(tpe: Type)(using elideThis: Option[Symbol]): Buffer = { - def printMethodicType(tp: TypeOrBounds): Unit = tp match { + def printRefinement(tpe: TypeRepr)(using elideThis: Option[Symbol]): Buffer = { + def printMethodicType(tp: TypeRepr): Unit = tp match { case tp @ MethodType(paramNames, params, res) => inParens(printMethodicTypeParams(paramNames, params)) printMethodicType(res) @@ -1244,11 +1237,11 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case ByNameType(t) => this += ": " printType(t) - case tp: Type => + case tp: TypeRepr => this += ": " printType(tp) } - def rec(tp: Type): Unit = tp match { + def rec(tp: TypeRepr): Unit = tp match { case Refinement(parent, name, info) => rec(parent) indented { @@ -1260,7 +1253,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case ByNameType(_) | MethodType(_, _, _) | TypeLambda(_, _, _) => this += highlightKeyword("def ") += highlightTypeDef(name) printMethodicType(info) - case info: Type => + case info: TypeRepr => this += highlightKeyword("val ") += highlightValDef(name) printMethodicType(info) } @@ -1273,14 +1266,14 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += lineBreak() += "}" } - def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(using elideThis: Option[Symbol]): Unit = { - def printInfo(info: TypeOrBounds) = info match { + def printMethodicTypeParams(paramNames: List[String], params: List[TypeRepr])(using elideThis: Option[Symbol]): Unit = { + def printInfo(info: TypeRepr) = info match { case info: TypeBounds => printBounds(info) - case info: Type => + case info: TypeRepr => this += ": " printType(info) } - def printSeparated(list: List[(String, TypeOrBounds)]): Unit = list match { + def printSeparated(list: List[(String, TypeRepr)]): Unit = list match { case Nil => case (name, info) :: Nil => this += name @@ -1318,7 +1311,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig def printProtectedOrPrivate(definition: Definition): Boolean = { var prefixWasPrinted = false - def printWithin(within: Type) = within match { + def printWithin(within: TypeRepr) = within match { case TypeRef(_, name) => this += name case _ => printFullClassName(within) } @@ -1344,8 +1337,8 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig prefixWasPrinted } - def printFullClassName(tp: TypeOrBounds): Unit = { - def printClassPrefix(prefix: TypeOrBounds): Unit = prefix match { + def printFullClassName(tp: TypeRepr): Unit = { + def printClassPrefix(prefix: TypeRepr): Unit = prefix match { case TypeRef(prefix2, name) => printClassPrefix(prefix2) this += name += "." @@ -1384,10 +1377,10 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig private[this] val names = collection.mutable.Map.empty[Symbol, String] private[this] val namesIndex = collection.mutable.Map.empty[String, Int] - private def splicedName(sym: Symbol)(using ctx: Context): Option[String] = { - sym.annots.find(_.symbol.owner == ctx.requiredClass("scala.internal.quoted.showName")).flatMap { - case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c) - case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c) + private def splicedName(sym: Symbol): Option[String] = { + sym.annots.find(_.symbol.owner == Symbol.requiredClass("scala.internal.quoted.showName")).flatMap { + case Apply(_, Literal(Constant.String(c)) :: Nil) => Some(c) + case Apply(_, Inlined(_, _, Literal(Constant.String(c))) :: Nil) => Some(c) case annot => None }.orElse { if sym.owner.isClassDef then None @@ -1405,7 +1398,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } private object SpecialOp { - def unapply(arg: Tree)(using ctx: Context): Option[(String, List[Term])] = arg match { + def unapply(arg: Tree): Option[(String, List[Term])] = arg match { case arg @ Apply(fn, args) => fn.tpe match { case tpe @ TermRef(ThisType(TypeRef(_, name)), name2) if name == "" => @@ -1417,7 +1410,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } private object Annotation { - def unapply(arg: Tree)(using ctx: Context): Option[(TypeTree, List[Term])] = arg match { + def unapply(arg: Tree): Option[(TypeTree, List[Term])] = arg match { case New(annot) => Some((annot, Nil)) case Apply(Select(New(annot), ""), args) => Some((annot, args)) case Apply(TypeApply(Select(New(annot), ""), targs), args) => Some((annot, args)) @@ -1429,17 +1422,17 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig private object Types { object Sequence { - def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match { - case AppliedType(seq, (tp: Type) :: Nil) - if seq.typeSymbol == ctx.requiredClass("scala.collection.Seq") || seq.typeSymbol == ctx.requiredClass("scala.collection.immutable.Seq") => + def unapply(tpe: TypeRepr): Option[TypeRepr] = tpe match { + case AppliedType(seq, (tp: TypeRepr) :: Nil) + if seq.typeSymbol == Symbol.requiredClass("scala.collection.Seq") || seq.typeSymbol == Symbol.requiredClass("scala.collection.immutable.Seq") => Some(tp) case _ => None } } object Repeated { - def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match { - case AppliedType(rep, (tp: Type) :: Nil) if rep.typeSymbol == ctx.requiredClass("scala.") => Some(tp) + def unapply(tpe: TypeRepr): Option[TypeRepr] = tpe match { + case AppliedType(rep, (tp: TypeRepr) :: Nil) if rep.typeSymbol == Symbol.requiredClass("scala.") => Some(tp) case _ => None } } @@ -1447,7 +1440,7 @@ class SourceTypePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } object PackageObject { - def unapply(tree: Tree)(using ctx: Context): Option[Tree] = tree match { + def unapply(tree: Tree): Option[Tree] = tree match { case PackageClause(_, ValDef("package", _, _) :: body :: Nil) => Some(body) case _ => None } diff --git a/runtime/src/main/scala-3/mdoc/internal/pprint/TypePrinter.scala b/runtime/src/main/scala-3/mdoc/internal/pprint/TypePrinter.scala index 113fdf768..d29de36c7 100644 --- a/runtime/src/main/scala-3/mdoc/internal/pprint/TypePrinter.scala +++ b/runtime/src/main/scala-3/mdoc/internal/pprint/TypePrinter.scala @@ -15,10 +15,10 @@ object TPrint { object TypePrinter{ def typeString[T](using ctx: QuoteContext, tpe: Type[T]): Expr[TPrint[T]] = { - import ctx.tasty._ + import ctx.reflect._ - val typePrinter = new SourceTypePrinter(ctx.tasty)(SyntaxHighlight.plain) + val typePrinter = new SourceTypePrinter(ctx.reflect)(SyntaxHighlight.plain) - '{ new TPrint[T]{ def render: String = ${ Expr(typePrinter.showTypeOrBounds(tpe.unseal.tpe)) } } } + '{ new TPrint[T]{ def render: String = ${ Expr(typePrinter.showType(tpe.unseal.tpe)) } } } } } diff --git a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala index 1bb4f430b..16eb92429 100644 --- a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala +++ b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala @@ -30,7 +30,8 @@ class WorksheetSuite extends BaseSuite { .getURLs(this.getClass().getClassLoader()) .collect { case url - if url.toString.contains("dotty-library") || url.toString.contains("scala-library") => + if url.toString.contains("scala3-library") || url.toString + .contains("scala-library") => Paths.get(url.toURI()) } .asJava @@ -89,9 +90,9 @@ class WorksheetSuite extends BaseSuite { |... |""".stripMargin, compat = Map( - "0." -> """| // : Stream[Int] = Stre... - |res0: Stream[Int] = Stream(10, ) - |""".stripMargin + "3.0" -> """| // : Stream[Int] = Stre... + |res0: Stream[Int] = Stream(10, ) + |""".stripMargin ) ) @@ -120,11 +121,11 @@ class WorksheetSuite extends BaseSuite { |res0: List[Int] = List(2, 3) |""".stripMargin, compat = Map( - "0.2" -> + "3.0" -> """| // : List[Int] = List(1... |list: List[Int] = List(1, 2, 3) - | // : List[Int @unchecked... - |res0: List[Int @uncheckedVariance] = List(2, 3) + | // : List[Int] = List(2,... + |res0: List[Int] = List(2, 3) |""".stripMargin ) ) @@ -186,10 +187,10 @@ class WorksheetSuite extends BaseSuite { |n: User = User("Susan") |""".stripMargin, compat = Map( - "0." -> """|case class User(name: String) - | // : User = User(Susan) - |n: User = User(Susan) - |""".stripMargin, + "3.0" -> """|case class User(name: String) + | // : User = User(Susan) + |n: User = User(Susan) + |""".stripMargin, "2.13" -> """|case class User(name: String) | // : User = User(name =... |n: User = User(name = "Susan") @@ -209,7 +210,7 @@ class WorksheetSuite extends BaseSuite { | ^^^^^^^^^^^ |""".stripMargin, compat = Map( - "0.2" -> + "3.0" -> """|type-error:2:21: error: Found: ("not found" : String) |Required: Int |val filename: Int = "not found" @@ -234,7 +235,7 @@ class WorksheetSuite extends BaseSuite { |^^^^^^^^^^^^^^^ |""".stripMargin, compat = Map( - "0.2" -> + "3.0" -> """|crash:4:1: error: java.lang.RuntimeException: boom | at repl.MdocSession$App.crash(crash.scala:8) | at repl.MdocSession$App.(crash.scala:16) @@ -258,7 +259,7 @@ class WorksheetSuite extends BaseSuite { |x: String = "foo" |""".stripMargin, compat = Map( - "0." -> + "3.0" -> """| // : String = foo |x: String = foo |""".stripMargin