Skip to content

Commit 8536064

Browse files
authored
Backport "Add Stable Presentation Compiler" to LTS (#19061)
Backports #17528 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 8af6da5 + 0147f07 commit 8536064

File tree

135 files changed

+29662
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+29662
-51
lines changed

NOTICE.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,19 @@ major authors were omitted by oversight.
8989
details.
9090

9191
* dotty.tools.dotc.coverage: Coverage instrumentation utilities have been
92-
adapted from the scoverage plugin for scala 2 [5], which is under the
92+
adapted from the scoverage plugin for scala 2 [4], which is under the
9393
Apache 2.0 license.
9494

95+
* dooty.tools.pc: Presentation compiler implementation adapted from
96+
scalameta/metals [5] mtags module, which is under the Apache 2.0 license.
97+
9598
* The Dotty codebase contains parts which are derived from
96-
the ScalaPB protobuf library [4], which is under the Apache 2.0 license.
99+
the ScalaPB protobuf library [6], which is under the Apache 2.0 license.
97100

98101

99102
[1] https://github.com/scala/scala
100103
[2] https://github.com/adriaanm/scala/tree/sbt-api-consolidate/src/compiler/scala/tools/sbt
101104
[3] https://github.com/sbt/sbt/tree/0.13/compile/interface/src/main/scala/xsbt
102-
[4] https://github.com/lampepfl/dotty/pull/5783/files
103-
[5] https://github.com/scoverage/scalac-scoverage-plugin
105+
[4] https://github.com/scoverage/scalac-scoverage-plugin
106+
[5] https://github.com/scalameta/metals
107+
[6] https://github.com/lampepfl/dotty/pull/5783/files

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ val `scala3-bench-run` = Build.`scala3-bench-run`
2828
val dist = Build.dist
2929
val `community-build` = Build.`community-build`
3030
val `sbt-community-build` = Build.`sbt-community-build`
31+
val `scala3-presentation-compiler` = Build.`scala3-presentation-compiler`
32+
val `scala3-presentation-compiler-bootstrapped` = Build.`scala3-presentation-compiler-bootstrapped`
3133

3234
val sjsSandbox = Build.sjsSandbox
3335
val sjsJUnitTests = Build.sjsJUnitTests

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import core._
55
import Contexts._
66
import SymDenotations.ClassDenotation
77
import Symbols._
8+
import Comments.Comment
89
import util.{FreshNameCreator, SourceFile, NoSource}
910
import util.Spans.Span
1011
import ast.{tpd, untpd}
@@ -69,6 +70,9 @@ class CompilationUnit protected (val source: SourceFile) {
6970
/** Can this compilation unit be suspended */
7071
def isSuspendable: Boolean = true
7172

73+
/** List of all comments present in this compilation unit */
74+
var comments: List[Comment] = Nil
75+
7276
/** Suspends the compilation unit by thowing a SuspendException
7377
* and recording the suspended compilation unit
7478
*/

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
145145
(fromSource ++ fromClassPath).distinct
146146
}
147147

148-
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, toSource(uri, sourceCode))
148+
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, SourceFile.virtual(uri, sourceCode))
149149

150150
def run(uri: URI, source: SourceFile): List[Diagnostic] = {
151151
import typer.ImportInfo._
@@ -297,9 +297,6 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
297297
cleanupTree(tree)
298298
}
299299

300-
private def toSource(uri: URI, sourceCode: String): SourceFile =
301-
SourceFile.virtual(Paths.get(uri).toString, sourceCode)
302-
303300
/**
304301
* Initialize this driver and compiler.
305302
*
@@ -323,7 +320,7 @@ object InteractiveDriver {
323320
else
324321
try
325322
// We don't use file.file here since it'll be null
326-
// for the VirtualFiles created by InteractiveDriver#toSource
323+
// for the VirtualFiles created by SourceFile#virtual
327324
// TODO: To avoid these round trip conversions, we could add an
328325
// AbstractFile#toUri method and implement it by returning a constant
329326
// passed as a parameter to a constructor of VirtualFile

compiler/src/dotty/tools/dotc/parsing/ParserPhase.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Parser extends Phase {
3030
val p = new Parsers.Parser(unit.source)
3131
// p.in.debugTokenStream = true
3232
val tree = p.parse()
33+
ctx.compilationUnit.comments = p.in.comments
3334
if (p.firstXmlPos.exists && !firstXmlPos.exists)
3435
firstXmlPos = p.firstXmlPos
3536
tree

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ object Scanners {
227227
*/
228228
private var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
229229

230-
/* A Buffer for comment positions */
231-
private val commentPosBuf = new mutable.ListBuffer[Span]
230+
/* A Buffer for comments */
231+
private val commentBuf = new mutable.ListBuffer[Comment]
232232

233-
/** Return a list of all the comment positions */
234-
def commentSpans: List[Span] = commentPosBuf.toList
233+
/** Return a list of all the comments */
234+
def comments: List[Comment] = commentBuf.toList
235235

236236
private def addComment(comment: Comment): Unit = {
237237
val lookahead = lookaheadReader()
@@ -246,7 +246,7 @@ object Scanners {
246246
def getDocComment(pos: Int): Option[Comment] = docstringMap.get(pos)
247247

248248
/** A buffer for comments */
249-
private val commentBuf = CharBuffer(initialCharBufferSize)
249+
private val currentCommentBuf = CharBuffer(initialCharBufferSize)
250250

251251
def toToken(identifier: SimpleName): Token =
252252
def handleMigration(keyword: Token): Token =
@@ -523,7 +523,7 @@ object Scanners {
523523
*
524524
* The following tokens can start an indentation region:
525525
*
526-
* : = => <- if then else while do try catch
526+
* : = => <- if then else while do try catch
527527
* finally for yield match throw return with
528528
*
529529
* Inserting an INDENT starts a new indentation region with the indentation of the current
@@ -1019,7 +1019,7 @@ object Scanners {
10191019

10201020
private def skipComment(): Boolean = {
10211021
def appendToComment(ch: Char) =
1022-
if (keepComments) commentBuf.append(ch)
1022+
if (keepComments) currentCommentBuf.append(ch)
10231023
def nextChar() = {
10241024
appendToComment(ch)
10251025
Scanner.this.nextChar()
@@ -1047,9 +1047,9 @@ object Scanners {
10471047
def finishComment(): Boolean = {
10481048
if (keepComments) {
10491049
val pos = Span(start, charOffset - 1, start)
1050-
val comment = Comment(pos, commentBuf.toString)
1051-
commentBuf.clear()
1052-
commentPosBuf += pos
1050+
val comment = Comment(pos, currentCommentBuf.toString)
1051+
currentCommentBuf.clear()
1052+
commentBuf += comment
10531053

10541054
if (comment.isDocComment)
10551055
addComment(comment)
@@ -1065,7 +1065,7 @@ object Scanners {
10651065
else if (ch == '*') { nextChar(); skipComment(); finishComment() }
10661066
else {
10671067
// This was not a comment, remove the `/` from the buffer
1068-
commentBuf.clear()
1068+
currentCommentBuf.clear()
10691069
false
10701070
}
10711071
}

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
179179
if (printWithoutPrefix.contains(tp.symbol))
180180
toText(tp.name)
181181
else
182-
toTextPrefix(tp.prefix) ~ selectionString(tp)
182+
toTextPrefixOf(tp) ~ selectionString(tp)
183183
case tp: TermParamRef =>
184184
ParamRefNameString(tp) ~ lambdaHash(tp.binder) ~ ".type"
185185
case tp: TypeParamRef =>
@@ -353,7 +353,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
353353
def toTextRef(tp: SingletonType): Text = controlled {
354354
tp match {
355355
case tp: TermRef =>
356-
toTextPrefix(tp.prefix) ~ selectionString(tp)
356+
toTextPrefixOf(tp) ~ selectionString(tp)
357357
case tp: ThisType =>
358358
nameString(tp.cls) + ".this"
359359
case SuperType(thistpe: SingletonType, _) =>
@@ -375,15 +375,6 @@ class PlainPrinter(_ctx: Context) extends Printer {
375375
}
376376
}
377377

378-
/** The string representation of this type used as a prefix, including separator */
379-
def toTextPrefix(tp: Type): Text = controlled {
380-
homogenize(tp) match {
381-
case NoPrefix => ""
382-
case tp: SingletonType => toTextRef(tp) ~ "."
383-
case tp => trimPrefix(toTextLocal(tp)) ~ "#"
384-
}
385-
}
386-
387378
def toTextCaptureRef(tp: Type): Text =
388379
homogenize(tp) match
389380
case tp: TermRef if tp.symbol == defn.captureRoot => Str("cap")
@@ -393,6 +384,15 @@ class PlainPrinter(_ctx: Context) extends Printer {
393384
protected def isOmittablePrefix(sym: Symbol): Boolean =
394385
defn.unqualifiedOwnerTypes.exists(_.symbol == sym) || isEmptyPrefix(sym)
395386

387+
/** The string representation of type prefix, including separator */
388+
def toTextPrefixOf(tp: NamedType): Text = controlled {
389+
homogenize(tp.prefix) match {
390+
case NoPrefix => ""
391+
case tp: SingletonType => toTextRef(tp) ~ "."
392+
case tp => trimPrefix(toTextLocal(tp)) ~ "#"
393+
}
394+
}
395+
396396
protected def isEmptyPrefix(sym: Symbol): Boolean =
397397
sym.isEffectiveRoot || sym.isAnonymousClass || sym.name.isReplWrapperName
398398

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package printing
44

55
import core._
66
import Texts._, ast.Trees._
7-
import Types.{Type, SingletonType, LambdaParam},
7+
import Types.{Type, SingletonType, LambdaParam, NamedType},
88
Symbols.Symbol, Scopes.Scope, Constants.Constant,
99
Names.Name, Denotations._, Annotations.Annotation, Contexts.Context
1010
import typer.Implicits.*
@@ -101,7 +101,7 @@ abstract class Printer {
101101
def toTextRef(tp: SingletonType): Text
102102

103103
/** Textual representation of a prefix of some reference, ending in `.` or `#` */
104-
def toTextPrefix(tp: Type): Text
104+
def toTextPrefixOf(tp: NamedType): Text
105105

106106
/** Textual representation of a reference in a capture set */
107107
def toTextCaptureRef(tp: Type): Text

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,22 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
117117
}
118118
}
119119

120-
override def toTextPrefix(tp: Type): Text = controlled {
120+
override def toTextPrefixOf(tp: NamedType): Text = controlled {
121121
def isOmittable(sym: Symbol) =
122122
if printDebug then false
123123
else if homogenizedView then isEmptyPrefix(sym) // drop <root> and anonymous classes, but not scala, Predef.
124124
else if sym.isPackageObject then isOmittablePrefix(sym.owner)
125125
else isOmittablePrefix(sym)
126-
tp match {
127-
case tp: ThisType if isOmittable(tp.cls) =>
126+
127+
tp.prefix match {
128+
case thisType: ThisType if isOmittable(thisType.cls) =>
128129
""
129-
case tp @ TermRef(pre, _) =>
130-
val sym = tp.symbol
131-
if sym.isPackageObject && !homogenizedView && !printDebug then toTextPrefix(pre)
130+
case termRef @ TermRef(pre, _) =>
131+
val sym = termRef.symbol
132+
if sym.isPackageObject && !homogenizedView && !printDebug then toTextPrefixOf(termRef)
132133
else if (isOmittable(sym)) ""
133-
else super.toTextPrefix(tp)
134-
case _ => super.toTextPrefix(tp)
134+
else super.toTextPrefixOf(tp)
135+
case _ => super.toTextPrefixOf(tp)
135136
}
136137
}
137138

@@ -427,8 +428,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
427428
case id @ Ident(name) =>
428429
val txt = tree.typeOpt match {
429430
case tp: NamedType if name != nme.WILDCARD =>
430-
val pre = if (tp.symbol.is(JavaStatic)) tp.prefix.widen else tp.prefix
431-
toTextPrefix(pre) ~ withPos(selectionString(tp), tree.sourcePos)
431+
toTextPrefixOf(tp) ~ withPos(selectionString(tp), tree.sourcePos)
432432
case _ =>
433433
toText(name)
434434
}

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ object SyntaxHighlighting {
8383
}
8484
}
8585

86-
for (span <- scanner.commentSpans)
87-
highlightPosition(span, CommentColor)
86+
for (comment <- scanner.comments)
87+
highlightPosition(comment.span, CommentColor)
8888

8989
object TreeHighlighter extends untpd.UntypedTreeTraverser {
9090
import untpd._

0 commit comments

Comments
 (0)