Skip to content

Use flexible types in dotty #21608

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
May 14, 2025
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
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/MainGenericCompiler.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dotty.tools

import scala.language.unsafeNulls

import scala.annotation.tailrec
import scala.io.Source
import scala.util.Try
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dotty.tools

import scala.language.unsafeNulls

import scala.annotation.tailrec
import scala.io.Source
import scala.util.Try
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/AsmUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package dotty.tools
package backend
package jvm

import scala.language.unsafeNulls

import scala.tools.asm.tree.{AbstractInsnNode}
import java.io.PrintWriter
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}
Expand Down
14 changes: 6 additions & 8 deletions compiler/src/dotty/tools/backend/jvm/ClassfileWriters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import dotty.tools.io.PlainFile.toPlainFile
import BTypes.InternalName
import dotty.tools.io.JarArchive

import scala.language.unsafeNulls

/** !!! This file is now copied in `dotty.tools.io.FileWriters` in a more general way that does not rely upon
* `PostProcessorFrontendAccess`, this should probably be changed to wrap that class instead.
*
Expand Down Expand Up @@ -54,11 +52,11 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess) {
def close(): Unit

protected def classRelativePath(className: InternalName, suffix: String = ".class"): String =
className.replace('.', '/').nn + suffix
className.replace('.', '/') + suffix
}

object ClassfileWriter {
private def getDirectory(dir: String): Path = Paths.get(dir).nn
private def getDirectory(dir: String): Path = Paths.get(dir)

def apply(): ClassfileWriter = {
val jarManifestMainClass: Option[String] = compilerSettings.mainClass.orElse {
Expand Down Expand Up @@ -137,7 +135,7 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess) {
new JarEntryWriter(jarFile, jarManifestMainClass, jarCompressionLevel)
}
else if (file.isVirtual) new VirtualFileWriter(file)
else if (file.isDirectory) new DirEntryWriter(file.file.toPath.nn)
else if (file.isDirectory) new DirEntryWriter(file.file.nn.toPath)
else throw new IllegalStateException(s"don't know how to handle an output of $file [${file.getClass}]")
}

Expand All @@ -151,7 +149,7 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess) {
val jarWriter: JarOutputStream = {
import scala.util.Properties.*
val manifest = new Manifest
val attrs = manifest.getMainAttributes.nn
val attrs = manifest.getMainAttributes
attrs.put(MANIFEST_VERSION, "1.0")
attrs.put(ScalaCompilerVersion, versionNumberString)
mainClass.foreach(c => attrs.put(MAIN_CLASS, c))
Expand Down Expand Up @@ -184,7 +182,7 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess) {
// important detail here, even on Windows, Zinc expects the separator within the jar
// to be the system default, (even if in the actual jar file the entry always uses '/').
// see https://github.com/sbt/zinc/blob/dcddc1f9cfe542d738582c43f4840e17c053ce81/internal/compiler-bridge/src/main/scala/xsbt/JarUtils.scala#L47
val pathInJar =
val pathInJar =
if File.separatorChar == '/' then relativePath
else relativePath.replace('/', File.separatorChar)
PlainFile.toPlainFile(Paths.get(s"${file.absolutePath}!$pathInJar"))
Expand Down Expand Up @@ -293,5 +291,5 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess) {
}

/** Can't output a file due to the state of the file system. */
class FileConflictException(msg: String, cause: Throwable = null) extends IOException(msg, cause)
class FileConflictException(msg: String, cause: Throwable | Null = null) extends IOException(msg, cause)
}
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/backend/jvm/PostProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PostProcessor(val frontendAccess: PostProcessorFrontendAccess, val bTypes:
setInnerClasses(classNode)
serializeClass(classNode)
catch
case e: java.lang.RuntimeException if e.getMessage != null && e.getMessage.nn.contains("too large!") =>
case e: java.lang.RuntimeException if e.getMessage != null && e.getMessage.contains("too large!") =>
backendReporting.error(em"Could not write class $internalName because it exceeds JVM code size limits. ${e.getMessage}")
null
case ex: Throwable =>
Expand All @@ -58,8 +58,8 @@ class PostProcessor(val frontendAccess: PostProcessorFrontendAccess, val bTypes:
}

private def warnCaseInsensitiveOverwrite(clazz: GeneratedClass) = {
val name = clazz.classNode.name.nn
val lowerCaseJavaName = name.nn.toLowerCase
val name = clazz.classNode.name
val lowerCaseJavaName = name.toLowerCase
val clsPos = clazz.position
caseInsensitively.putIfAbsent(lowerCaseJavaName, (name, clsPos)) match {
case null => ()
Expand All @@ -71,7 +71,7 @@ class PostProcessor(val frontendAccess: PostProcessorFrontendAccess, val bTypes:
val locationAddendum =
if pos1.source.path == pos2.source.path then ""
else s" (defined in ${pos2.source.file.name})"
def nicify(name: String): String = name.replace('/', '.').nn
def nicify(name: String): String = name.replace('/', '.')
if name1 == name2 then
backendReporting.error(
em"${nicify(name1)} and ${nicify(name2)} produce classes that overwrite one another", pos1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ object PostProcessorFrontendAccess {
override def backendReporting: BackendReporting = {
val local = localReporter.get()
if local eq null then directBackendReporting
else local.nn
else local
}

override object directBackendReporting extends BackendReporting {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Bench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Bench extends Driver:
println(s"time elapsed: ${times(curRun)}ms")
if ctx.settings.Xprompt.value || waitAfter == curRun + 1 then
print("hit <return> to continue >")
System.in.nn.read()
System.in.read()
reporter

def extractNumArg(args: Array[String], name: String, default: Int = 1): (Int, Array[String]) = {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Driver {
finish(compiler, run)
catch
case ex: FatalError =>
report.error(ex.getMessage.nn) // signals that we should fail compilation.
report.error(ex.getMessage) // signals that we should fail compilation.
case ex: Throwable if ctx.usedBestEffortTasty =>
report.bestEffortError(ex, "Some best-effort tasty files were not able to be read.")
throw ex
Expand Down Expand Up @@ -117,7 +117,7 @@ class Driver {
.distinct
val ctx1 = ctx.fresh
val fullClassPath =
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator.nn)
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator)
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
else ctx

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ extends ImplicitRunInfo, ConstraintRunInfo, cc.CaptureRunInfo {
try
trackProgress(_.cancel())
finally
Thread.currentThread().nn.interrupt()
Thread.currentThread().interrupt()

private def doAdvancePhase(currentPhase: Phase, wasRan: Boolean)(using Context): Unit =
trackProgress: progress =>
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/cc/CaptureSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ object CaptureSet:
extends Var(owner, initialElems):

// For debugging: A trace where a set was created. Note that logically it would make more
// sense to place this variable in BiMapped, but that runs afoul of the initializatuon checker.
// val stack = if debugSets && this.isInstanceOf[BiMapped] then (new Throwable).getStackTrace().nn.take(20) else null
// sense to place this variable in Mapped, but that runs afoul of the initialization checker.
// val stack = if debugSets && this.isInstanceOf[Mapped] then (new Throwable).getStackTrace().take(20) else null

/** The variable from which this variable is derived */
def source: Var
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/cc/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
CapturingType(parent2, ann.tree.toCaptureSet)
catch case ex: IllegalCaptureRef =>
if !tptToCheck.isEmpty then
report.error(em"Illegal capture reference: ${ex.getMessage.nn}", tptToCheck.srcPos)
report.error(em"Illegal capture reference: ${ex.getMessage}", tptToCheck.srcPos)
parent2
else if ann.symbol == defn.UncheckedCapturesAnnot then
makeUnchecked(apply(parent))
Expand Down Expand Up @@ -938,7 +938,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
val refs =
try refTree.toCaptureRefs
catch case ex: IllegalCaptureRef =>
report.error(em"Illegal capture reference: ${ex.getMessage.nn}", refTree.srcPos)
report.error(em"Illegal capture reference: ${ex.getMessage}", refTree.srcPos)
Nil
for ref <- refs do
def pos =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package dotty.tools
package dotc.classpath

import scala.language.unsafeNulls

import java.net.URL
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.ArraySeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ClassPathFactory {
for
file <- files
a <- ClassPath.expandManifestPath(file.absolutePath)
path = java.nio.file.Paths.get(a.toURI()).nn
path = java.nio.file.Paths.get(a.toURI())
if Files.exists(path)
yield
newClassPath(AbstractFile.getFile(path))
Expand All @@ -82,7 +82,7 @@ class ClassPathFactory {
if (file.isJarOrZip)
ZipAndJarSourcePathFactory.create(file)
else if (file.isDirectory)
new DirectorySourcePath(file.file)
new DirectorySourcePath(file.file.nn)
else
sys.error(s"Unsupported sourcepath element: $file")
}
Expand All @@ -94,7 +94,7 @@ object ClassPathFactory {
if (file.isJarOrZip)
ZipAndJarClassPathFactory.create(file)
else if (file.isDirectory)
new DirectoryClassPath(file.file)
new DirectoryClassPath(file.file.nn)
else
sys.error(s"Unsupported classpath element: $file")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
package dotty.tools.dotc.classpath

import scala.language.unsafeNulls

import java.io.{File => JFile}
import java.net.{URI, URL}
import java.nio.file.{FileSystems, Files}
Expand Down Expand Up @@ -119,7 +117,7 @@ trait JFileDirectoryLookup[FileEntryType <: ClassRepresentation] extends Directo
protected def toAbstractFile(f: JFile): AbstractFile = f.toPath.toPlainFile
protected def isPackage(f: JFile): Boolean = f.isPackage

assert(dir != null, "Directory file in DirectoryFileLookup cannot be null")
assert(dir.asInstanceOf[JFile | Null] != null, "Directory file in DirectoryFileLookup cannot be null")

def asURLs: Seq[URL] = Seq(dir.toURI.toURL)
def asClassPathStrings: Seq[String] = Seq(dir.getPath)
Expand Down Expand Up @@ -216,7 +214,7 @@ final class JrtClassPath(fs: java.nio.file.FileSystem) extends ClassPath with No
final class CtSymClassPath(ctSym: java.nio.file.Path, release: Int) extends ClassPath with NoSourcePaths {
import java.nio.file.Path, java.nio.file.*

private val fileSystem: FileSystem = FileSystems.newFileSystem(ctSym, null: ClassLoader)
private val fileSystem: FileSystem = FileSystems.newFileSystem(ctSym, null: ClassLoader | Null)
private val root: Path = fileSystem.getRootDirectories.iterator.next
private val roots = Files.newDirectoryStream(root).iterator.asScala.toList

Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/classpath/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package dotty.tools
package dotc.classpath

import scala.language.unsafeNulls

import java.io.{File => JFile, FileFilter}
import java.net.URL
import dotty.tools.io.AbstractFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ case class VirtualDirectoryClassPath(dir: VirtualDirectory) extends ClassPath wi
type F = AbstractFile

// From AbstractFileClassLoader
private final def lookupPath(base: AbstractFile)(pathParts: Seq[String], directory: Boolean): AbstractFile = {
var file: AbstractFile = base
private final def lookupPath(base: AbstractFile)(pathParts: Seq[String], directory: Boolean): AbstractFile | Null = {
var file: AbstractFile | Null = base
val dirParts = pathParts.init.iterator
while (dirParts.hasNext) {
val dirPart = dirParts.next
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/config/CommandLineParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ object CommandLineParser:
def expandArg(arg: String): List[String] =
val path = Paths.get(arg.stripPrefix("@"))
if !Files.exists(path) then
System.err.nn.println(s"Argument file ${path.nn.getFileName} could not be found")
System.err.println(s"Argument file ${path.getFileName} could not be found")
Nil
else
def stripComment(s: String) = s.indexOf('#') match { case -1 => s case i => s.substring(0, i) }
val lines = Files.readAllLines(path).nn
val params = lines.asScala.map(stripComment).filter(!_.nn.isEmpty).mkString(" ")
val lines = Files.readAllLines(path)
val params = lines.asScala.map(stripComment).filter(!_.isEmpty).mkString(" ")
tokenize(params)

class ParseException(msg: String) extends RuntimeException(msg)
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/config/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import core.Contexts.{Context, ctx}
object Printers {

class Printer {
def println(msg: => String): Unit = System.out.nn.println(msg)
def println(msg: => String): Unit = System.out.println(msg)
}

object noPrinter extends Printer {
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dotty.tools.dotc
package config

import scala.language.unsafeNulls
import dotty.tools.dotc.config.PathResolver.Defaults
import dotty.tools.dotc.config.Settings.{Setting, SettingGroup, SettingCategory, Deprecation}
import dotty.tools.dotc.config.SourceVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package config
import Settings.Setting.ChoiceWithHelp
import dotty.tools.backend.jvm.BackendUtils.classfileVersionMap
import dotty.tools.io.{AbstractFile, Directory, JDK9Reflectors, PlainDirectory, NoAbstractFile}
import scala.language.unsafeNulls

object ScalaSettingsProperties:

Expand Down
10 changes: 4 additions & 6 deletions compiler/src/dotty/tools/dotc/config/ScalaVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package dotty.tools
package dotc.config

import scala.language.unsafeNulls

import scala.annotation.internal.sharable
import scala.util.{Try, Success, Failure}

Expand Down Expand Up @@ -82,20 +80,20 @@ case class SpecificScalaVersion(major: Int, minor: Int, rev: Int, build: ScalaBu
"The minor and revision parts are optional."
))

def toInt(s: String) = s match {
def toInt(s: String | Null) = s match {
case null | "" => 0
case _ => s.toInt
case _ => s.nn.toInt
}

def isInt(s: String) = Try(toInt(s)).isSuccess

import ScalaBuild.*

def toBuild(s: String) = s match {
def toBuild(s: String | Null) = s match {
case null | "FINAL" => Final
case s if (s.toUpperCase.startsWith("RC") && isInt(s.substring(2))) => RC(toInt(s.substring(2)))
case s if (s.toUpperCase.startsWith("M") && isInt(s.substring(1))) => Milestone(toInt(s.substring(1)))
case _ => Development(s)
case _ => Development(s.nn)
}

try versionString match {
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/config/Settings.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dotty.tools.dotc
package config

import scala.language.unsafeNulls

import core.Contexts.*

import dotty.tools.io.{AbstractFile, Directory, JarArchive, PlainDirectory}
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package dotty.tools
package dotc
package core

import scala.language.unsafeNulls

import ast.{ untpd, tpd }
import Symbols.*, Contexts.*
import util.{SourceFile, ReadOnlyMap}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object Decorators {
if name.length != 0 then name.getChars(0, name.length, chars, s.length)
termName(chars, 0, len)
case name: TypeName => s.concat(name.toTermName)
case _ => termName(s.concat(name.toString).nn)
case _ => termName(s.concat(name.toString))

def indented(width: Int): String =
val padding = " " * width
Expand Down Expand Up @@ -289,10 +289,10 @@ object Decorators {
case NonFatal(ex)
if !ctx.settings.YshowPrintErrors.value =>
s"... (cannot display due to ${ex.className} ${ex.getMessage}) ..."
case _ => String.valueOf(x).nn
case _ => String.valueOf(x)

/** Returns the simple class name of `x`. */
def className: String = if x == null then "<null>" else x.getClass.getSimpleName.nn
def className: String = if x == null then "<null>" else x.getClass.getSimpleName

extension [T](x: T)
def assertingErrorsReported(using Context): T = {
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/core/MacroClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ object MacroClassLoader {
ctx.setProperty(MacroClassLoaderKey, makeMacroClassLoader(using ctx))

private def makeMacroClassLoader(using Context): ClassLoader = trace("new macro class loader") {
import scala.language.unsafeNulls

val entries = ClassPath.expandPath(ctx.settings.classpath.value, expandStar=true)
val urls = entries.map(cp => java.nio.file.Paths.get(cp).toUri.toURL).toArray
val out = Option(ctx.settings.outputDir.value.toURL) // to find classes in case of suspended compilation
Expand Down
Loading
Loading