Skip to content

Commit 0b1ed87

Browse files
rochalaWojciechMazur
authored andcommitted
Add missing -Yexplicit-nulls for presentation compiler (#18776)
With a new definition of untyped trees, it is now required to have `-Yexplicit-nulls` flag in modules that use them in order to have proper type checking. This PR adds the missing flag. Without the flag, it was possible to first assign untyped trees to typed trees, and secondly use extension methods for typed trees which can be seen at `KeywordsCompletions.scala` with usage of `untpdTree.filterSubtrees`. It is also blocked by: #18775 I can also make a workaround in the unmanaged module, but it will require a dependency on nightly version / a new release from metals. [Cherry-picked e149e4c]
1 parent 18f7408 commit 0b1ed87

File tree

68 files changed

+332
-314
lines changed

Some content is hidden

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

68 files changed

+332
-314
lines changed

presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object AutoImports:
2323
def renameConfigMap(config: PresentationCompilerConfig)(using
2424
Context
2525
): Map[Symbol, String] =
26-
config.symbolPrefixes.asScala.flatMap { (from, to) =>
26+
config.symbolPrefixes().nn.asScala.flatMap { (from, to) =>
2727
val pkg = SemanticdbSymbols.inverseSemanticdbSymbol(from)
2828
val rename = to.stripSuffix(".").stripSuffix("#")
2929
List(pkg, pkg.map(_.moduleClass)).flatten
@@ -246,7 +246,7 @@ object AutoImports:
246246
// see WorksheetProvider.worksheetScala3AdjustmentsForPC
247247
val indent =
248248
if pos.source.path.isWorksheet &&
249-
editPos.getStart().getCharacter() == 0
249+
editPos.getStart().nn.getCharacter() == 0
250250
then indent0.drop(2)
251251
else indent0
252252
val topPadding =

presentation-compiler/src/main/dotty/tools/pc/AutoImportsProvider.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ final class AutoImportsProvider(
2929
)(using ReportContext):
3030

3131
def autoImports(isExtension: Boolean): List[AutoImportsResult] =
32-
val uri = params.uri
32+
val uri = params.uri().nn
33+
val text = params.text().nn
3334
val filePath = Paths.get(uri)
34-
driver.run(
35-
uri,
36-
SourceFile.virtual(filePath.toString, params.text)
37-
)
38-
val unit = driver.currentCtx.run.units.head
35+
driver.run(uri, SourceFile.virtual(filePath.toString, text))
36+
37+
val unit = driver.currentCtx.run.nn.units.head
3938
val tree = unit.tpdTree
4039

4140
val pos = driver.sourcePosition(params)
@@ -81,7 +80,7 @@ final class AutoImportsProvider(
8180
val generator =
8281
AutoImports.generator(
8382
correctedPos,
84-
params.text,
83+
text,
8584
tree,
8685
unit.comments,
8786
indexedContext.importContext,

presentation-compiler/src/main/dotty/tools/pc/CompilerSearchVisitor.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CompilerSearchVisitor(
1717
)(using ctx: Context, reports: ReportContext)
1818
extends SymbolSearchVisitor:
1919

20-
val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName)
20+
val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName().nn).nn
2121

2222
private def isAccessible(sym: Symbol): Boolean = try
2323
sym != NoSymbol && sym.isPublic && sym.isStatic
@@ -68,7 +68,7 @@ class CompilerSearchVisitor(
6868
.split("\\$")
6969

7070
val added =
71-
try toSymbols(pkg, innerPath.toList).filter(visitSymbol)
71+
try toSymbols(pkg, innerPath.nn.toList.map(_.nn)).filter(visitSymbol)
7272
catch
7373
case NonFatal(e) =>
7474
logger.log(Level.WARNING, e.getMessage(), e)
@@ -95,6 +95,6 @@ class CompilerSearchVisitor(
9595
override def isCancelled: Boolean = false
9696

9797
private def normalizePackage(pkg: String): String =
98-
pkg.replace("/", ".").stripSuffix(".")
98+
pkg.replace("/", ".").nn.stripSuffix(".")
9999

100100
end CompilerSearchVisitor

presentation-compiler/src/main/dotty/tools/pc/CompletionItemResolver.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object CompletionItemResolver extends ItemResolver:
3030
.orElse(
3131
search.symbolDocumentation(gsym.companion)
3232
) match
33-
case Some(info) if item.getDetail != null =>
33+
case Some(info) if item.getDetail() != null =>
3434
enrichDocs(
3535
item,
3636
info,
@@ -50,7 +50,7 @@ object CompletionItemResolver extends ItemResolver:
5050
Context
5151
): String =
5252
def docs(gsym: Symbol): String =
53-
search.symbolDocumentation(gsym).fold("")(_.docstring())
53+
search.symbolDocumentation(gsym).fold("")(_.docstring().nn)
5454
val gsymDoc = docs(gsym)
5555
def keyword(gsym: Symbol): String =
5656
if gsym.isClass then "class"
@@ -60,7 +60,7 @@ object CompletionItemResolver extends ItemResolver:
6060
else ""
6161
val companion = gsym.companion
6262
if companion == NoSymbol || gsym.is(JavaDefined) then
63-
if gsymDoc.isEmpty then
63+
if gsymDoc.isEmpty() then
6464
if gsym.isAliasType then
6565
fullDocstring(gsym.info.metalsDealias.typeSymbol, search)
6666
else if gsym.is(Method) then
@@ -73,8 +73,8 @@ object CompletionItemResolver extends ItemResolver:
7373
else gsymDoc
7474
else
7575
val companionDoc = docs(companion)
76-
if companionDoc.isEmpty then gsymDoc
77-
else if gsymDoc.isEmpty then companionDoc
76+
if companionDoc.isEmpty() then gsymDoc
77+
else if gsymDoc.isEmpty() then companionDoc
7878
else
7979
List(
8080
s"""|### ${keyword(companion)} ${companion.name}

presentation-compiler/src/main/dotty/tools/pc/ConvertToNamedArgumentsProvider.scala

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ final class ConvertToNamedArgumentsProvider(
2323
):
2424

2525
def convertToNamedArguments: Either[String, List[l.TextEdit]] =
26-
val uri = params.uri
26+
val uri = params.uri().nn
27+
val text = params.text().nn
2728
val filePath = Paths.get(uri)
28-
driver.run(
29-
uri,
30-
SourceFile.virtual(filePath.toString, params.text)
31-
)
32-
val unit = driver.currentCtx.run.units.head
29+
driver.run(uri, SourceFile.virtual(filePath.toString, text))
30+
31+
val unit = driver.currentCtx.run.nn.units.head
3332
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
3433
val pos = driver.sourcePosition(params)
3534
val trees = driver.openedTrees(uri)

presentation-compiler/src/main/dotty/tools/pc/ExtractMethodProvider.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ final class ExtractMethodProvider(
3838
extends ExtractMethodUtils:
3939

4040
def extractMethod(): List[TextEdit] =
41-
val text = range.text()
42-
val uri = range.uri
41+
val text = range.text().nn
42+
val uri = range.uri().nn
4343
val filePath = Paths.get(uri)
4444
val source = SourceFile.virtual(filePath.toString, text)
4545
driver.run(uri, source)
46-
val unit = driver.currentCtx.run.units.head
46+
val unit = driver.currentCtx.run.nn.units.head
4747
val pos = driver.sourcePosition(range).startPos
4848
val path =
4949
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
@@ -145,7 +145,7 @@ final class ExtractMethodProvider(
145145
val oldIndentLen = head.startPos.startColumnPadding.length()
146146
val toExtract =
147147
textToExtract(
148-
range.text(),
148+
text,
149149
head.startPos.start,
150150
expr.endPos.end,
151151
newIndent,

presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ object HoverProvider:
3232
driver: InteractiveDriver,
3333
search: SymbolSearch
3434
)(implicit reportContext: ReportContext): ju.Optional[HoverSignature] =
35-
val uri = params.uri
36-
val sourceFile = SourceFile.virtual(params.uri, params.text)
35+
val uri = params.uri().nn
36+
val text = params.text().nn
37+
val sourceFile = SourceFile.virtual(uri, text)
3738
driver.run(uri, sourceFile)
3839

3940
given ctx: Context = driver.currentCtx
@@ -54,7 +55,7 @@ object HoverProvider:
5455
then
5556
def report =
5657
val posId =
57-
if path.isEmpty || path.head.sourcePos == null || !path.head.sourcePos.exists
58+
if path.isEmpty || !path.head.sourcePos.exists
5859
then pos.start
5960
else path.head.sourcePos.start
6061
Report(
@@ -77,7 +78,7 @@ object HoverProvider:
7778
)
7879
end report
7980
reportContext.unsanitized.create(report, ifVerbose = true)
80-
ju.Optional.empty()
81+
ju.Optional.empty().nn
8182
else
8283
val skipCheckOnName =
8384
!pos.isPoint // don't check isHoveringOnName for RangeHover
@@ -125,7 +126,7 @@ object HoverProvider:
125126

126127
val docString = symbolTpes
127128
.flatMap(symTpe => search.symbolDocumentation(symTpe._1))
128-
.map(_.docstring)
129+
.map(_.docstring())
129130
.mkString("\n")
130131
printer.expressionType(exprTpw) match
131132
case Some(expressionType) =>
@@ -143,9 +144,9 @@ object HoverProvider:
143144
docstring = Some(docString),
144145
forceExpressionType = forceExpressionType
145146
)
146-
)
147+
).nn
147148
case _ =>
148-
ju.Optional.empty
149+
ju.Optional.empty().nn
149150
end match
150151
end match
151152
end if
@@ -188,7 +189,7 @@ object HoverProvider:
188189

189190
refTpe.flatMap(findRefinement).asJava
190191
case _ =>
191-
ju.Optional.empty()
192+
ju.Optional.empty().nn
192193

193194
end HoverProvider
194195

presentation-compiler/src/main/dotty/tools/pc/IndexedContext.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.core.Contexts.*
77
import dotty.tools.dotc.core.Flags.*
88
import dotty.tools.dotc.core.NameOps.moduleClassName
99
import dotty.tools.dotc.core.Names.*
10+
import dotty.tools.dotc.core.Scopes.EmptyScope
1011
import dotty.tools.dotc.core.Symbols.*
1112
import dotty.tools.dotc.core.Types.*
1213
import dotty.tools.dotc.typer.ImportInfo
@@ -82,7 +83,6 @@ object IndexedContext:
8283

8384
def apply(ctx: Context): IndexedContext =
8485
ctx match
85-
case null => Empty
8686
case NoContext => Empty
8787
case _ => LazyWrapper(using ctx)
8888

@@ -205,14 +205,14 @@ object IndexedContext:
205205
val (symbols, renames) =
206206
if ctx.isImportContext then
207207
val (syms, renames) =
208-
fromImportInfo(ctx.importInfo)
208+
fromImportInfo(ctx.importInfo.nn)
209209
.map((sym, rename) => (sym, rename.map(r => sym -> r.decoded)))
210210
.unzip
211211
(syms, renames.flatten.toMap)
212212
else if ctx.owner.isClass then
213213
val site = ctx.owner.thisType
214214
(accesibleMembers(site), Map.empty)
215-
else if ctx.scope != null then (ctx.scope.toList, Map.empty)
215+
else if ctx.scope != EmptyScope then (ctx.scope.toList, Map.empty)
216216
else (List.empty, Map.empty)
217217

218218
val initial = Map.empty[String, List[Symbol]]

presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala

+15-16
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ final class InferredTypeProvider(
6262
adjustOpt: Option[AdjustTypeOpts] = None
6363
): List[TextEdit] =
6464
val retryType = adjustOpt.isEmpty
65-
val uri = params.uri
66-
val filePath = Paths.get(uri)
65+
val uri = params.uri().nn
66+
val filePath = Paths.get(uri).nn
6767

68-
val sourceText = adjustOpt.map(_.text).getOrElse(params.text)
68+
val sourceText = adjustOpt.map(_.text).getOrElse(params.text().nn)
6969
val source =
70-
SourceFile.virtual(filePath.toString, sourceText)
70+
SourceFile.virtual(filePath.toString(), sourceText)
7171
driver.run(uri, source)
72-
val unit = driver.currentCtx.run.units.head
72+
val unit = driver.currentCtx.run.nn.units.head
7373
val pos = driver.sourcePosition(params)
7474
val path =
7575
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
@@ -78,15 +78,15 @@ final class InferredTypeProvider(
7878
val indexedCtx = IndexedContext(locatedCtx)
7979
val autoImportsGen = AutoImports.generator(
8080
pos,
81-
params.text,
81+
sourceText,
8282
unit.tpdTree,
8383
unit.comments,
8484
indexedCtx,
8585
config
8686
)
8787

8888
def removeType(nameEnd: Int, tptEnd: Int) =
89-
sourceText.substring(0, nameEnd) +
89+
sourceText.substring(0, nameEnd).nn +
9090
sourceText.substring(tptEnd + 1, sourceText.length())
9191

9292
def optDealias(tpe: Type): Type =
@@ -134,7 +134,7 @@ final class InferredTypeProvider(
134134
def baseEdit(withParens: Boolean): TextEdit =
135135
val keywordOffset = if isParam then 0 else 4
136136
val endPos =
137-
findNamePos(params.text, vl, keywordOffset).endPos.toLsp
137+
findNamePos(sourceText, vl, keywordOffset).endPos.toLsp
138138
adjustOpt.foreach(adjust => endPos.setEnd(adjust.adjustedEndPos))
139139
new TextEdit(
140140
endPos,
@@ -148,11 +148,10 @@ final class InferredTypeProvider(
148148
toCheckFor: Char,
149149
blockStartPos: SourcePosition
150150
) =
151-
val text = params.text
152-
val isParensFunction: Boolean = text(applyEndingPos) == toCheckFor
151+
val isParensFunction: Boolean = sourceText(applyEndingPos) == toCheckFor
153152

154153
val alreadyHasParens =
155-
text(blockStartPos.start) == '('
154+
sourceText(blockStartPos.start) == '('
156155

157156
if isParensFunction && !alreadyHasParens then
158157
new TextEdit(blockStartPos.toLsp, "(") :: baseEdit(withParens =
@@ -188,7 +187,7 @@ final class InferredTypeProvider(
188187
Some(
189188
AdjustTypeOpts(
190189
removeType(vl.namePos.end, tpt.sourcePos.end - 1),
191-
tpt.sourcePos.toLsp.getEnd()
190+
tpt.sourcePos.toLsp.getEnd().nn
192191
)
193192
)
194193
)
@@ -227,7 +226,7 @@ final class InferredTypeProvider(
227226
Some(
228227
AdjustTypeOpts(
229228
removeType(lastColon, tpt.sourcePos.end - 1),
230-
tpt.sourcePos.toLsp.getEnd()
229+
tpt.sourcePos.toLsp.getEnd().nn
231230
)
232231
)
233232
)
@@ -256,8 +255,8 @@ final class InferredTypeProvider(
256255
val firstEnd = patterns(0).endPos.end
257256
val secondStart = patterns(1).startPos.start
258257
val hasDot = params
259-
.text()
260-
.substring(firstEnd, secondStart)
258+
.text().nn
259+
.substring(firstEnd, secondStart).nn
261260
.exists(_ == ',')
262261
if !hasDot then
263262
val leftParen = new TextEdit(body.startPos.toLsp, "(")
@@ -309,7 +308,7 @@ final class InferredTypeProvider(
309308
val end = if withBacktick then idx + 1 else idx
310309
val pos = tree.source.atSpan(Span(start, end, start))
311310
Some(pos)
312-
case None if idx < text.length =>
311+
case None if idx < text.length() =>
313312
val ch = text.charAt(idx)
314313
if ch == realName.head then
315314
lookup(idx + 1, Some((idx, realName.tail)), withBacktick)

presentation-compiler/src/main/dotty/tools/pc/MetalsDriver.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MetalsDriver(
4848

4949
override def run(uri: URI, sourceCode: String): List[Diagnostic] =
5050
val diags =
51-
if alreadyCompiled(uri, sourceCode.toCharArray()) then Nil
51+
if alreadyCompiled(uri, sourceCode.toCharArray().nn) then Nil
5252
else super.run(uri, sourceCode)
5353
lastCompiledURI = uri
5454
diags

presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,23 @@ abstract class PcCollector[T](
3333
params: VirtualFileParams
3434
):
3535
private val caseClassSynthetics: Set[Name] = Set(nme.apply, nme.copy)
36-
val uri = params.uri()
37-
val filePath = Paths.get(uri)
38-
val sourceText = params.text
36+
val uri = params.uri().nn
37+
val filePath = Paths.get(uri).nn
38+
val sourceText = params.text().nn
3939
val source =
40-
SourceFile.virtual(filePath.toString, sourceText)
40+
SourceFile.virtual(filePath.toString(), sourceText)
4141
driver.run(uri, source)
4242
given ctx: Context = driver.currentCtx
4343

44-
val unit = driver.currentCtx.run.units.head
44+
val unit = driver.currentCtx.run.nn.units.head
4545
val compilatonUnitContext = ctx.fresh.setCompilationUnit(unit)
4646
val offset = params match
4747
case op: OffsetParams => op.offset()
4848
case _ => 0
4949
val offsetParams =
5050
params match
5151
case op: OffsetParams => op
52-
case _ =>
53-
CompilerOffsetParams(params.uri(), params.text(), 0, params.token())
52+
case _ => CompilerOffsetParams(uri, sourceText, 0, params.token().nn)
5453
val pos = driver.sourcePosition(offsetParams)
5554
val rawPath =
5655
Interactive

0 commit comments

Comments
 (0)