Skip to content

Commit 1f1e369

Browse files
paulpxeno-by
authored andcommitted
Made Symbol#associatedFile always return non-null
So we don't have to clutter everything with null checks.
1 parent 348c8fa commit 1f1e369

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

src/compiler/scala/tools/nsc/doc/base/MemberLookupBase.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ trait MemberLookupBase {
8888
// (4) if we still haven't found anything, create a tooltip
8989
Tooltip(query)
9090
case List(l) => l
91-
case links =>
91+
case links =>
9292
val chosen = chooseLink(links)
9393
def linkToString(link: LinkTo) = {
9494
val chosenInfo =
@@ -203,11 +203,11 @@ trait MemberLookupBase {
203203
def findExternalLink(sym: Symbol, name: String): Option[LinkToExternal] = {
204204
val sym1 =
205205
if (sym == AnyClass || sym == AnyRefClass || sym == AnyValClass || sym == NothingClass) ListClass
206-
else if (sym.isPackage)
206+
else if (sym.isPackage)
207207
/* Get package object which has associatedFile ne null */
208208
sym.info.member(newTermName("package"))
209209
else sym
210-
Option(sym1.associatedFile) flatMap (_.underlyingSource) flatMap { src =>
210+
sym1.associatedFile.underlyingSource flatMap { src =>
211211
val path = src.path
212212
settings.extUrlMapping get path map { url =>
213213
LinkToExternal(name, url + "#" + name)

src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import scala.tools.nsc.util.{ ClassPath }
1212
import classfile.ClassfileParser
1313
import scala.reflect.internal.MissingRequirementError
1414
import scala.reflect.internal.util.Statistics
15-
import scala.tools.nsc.io.{ AbstractFile }
15+
import scala.reflect.io.{ AbstractFile, NoAbstractFile }
1616

1717
/** This class ...
1818
*
@@ -250,7 +250,7 @@ abstract class SymbolLoaders {
250250
protected def doComplete(root: Symbol) {
251251
val start = if (Statistics.canEnable) Statistics.startTimer(classReadNanos) else null
252252
classfileParser.parse(classfile, root)
253-
if (root.associatedFile eq null) {
253+
if (root.associatedFile eq NoAbstractFile) {
254254
root match {
255255
// In fact, the ModuleSymbol forwards its setter to the module class
256256
case _: ClassSymbol | _: ModuleSymbol =>

src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import scala.reflect.runtime.ReflectionUtils
1313
import scala.reflect.macros.runtime.AbortMacroException
1414
import scala.util.control.NonFatal
1515
import scala.tools.nsc.util.stackTraceString
16+
import scala.reflect.io.NoAbstractFile
1617

1718
trait ContextErrors {
1819
self: Analyzer =>
@@ -642,7 +643,7 @@ trait ContextErrors {
642643
val addendums = List(
643644
if (sym0.associatedFile eq sym1.associatedFile)
644645
Some("conflicting symbols both originated in file '%s'".format(sym0.associatedFile.canonicalPath))
645-
else if ((sym0.associatedFile ne null) && (sym1.associatedFile ne null))
646+
else if ((sym0.associatedFile ne NoAbstractFile) && (sym1.associatedFile ne NoAbstractFile))
646647
Some("conflicting symbols originated in files '%s' and '%s'".format(sym0.associatedFile.canonicalPath, sym1.associatedFile.canonicalPath))
647648
else None ,
648649
if (isBug) Some("Note: this may be due to a bug in the compiler involving wildcards in package objects") else None

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.collection.mutable.ListBuffer
1111
import util.{ Statistics, shortClassOfInstance }
1212
import Flags._
1313
import scala.annotation.tailrec
14-
import scala.reflect.io.AbstractFile
14+
import scala.reflect.io.{ AbstractFile, NoAbstractFile }
1515

1616
trait Symbols extends api.Symbols { self: SymbolTable =>
1717
import definitions._
@@ -1916,8 +1916,8 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
19161916
(this.rawInfo ne NoType)
19171917
&& (this.effectiveOwner == that.effectiveOwner)
19181918
&& ( !this.effectiveOwner.isPackageClass
1919-
|| (this.associatedFile eq null)
1920-
|| (that.associatedFile eq null)
1919+
|| (this.associatedFile eq NoAbstractFile)
1920+
|| (that.associatedFile eq NoAbstractFile)
19211921
|| (this.associatedFile.path == that.associatedFile.path) // Cheap possibly wrong check, then expensive normalization
19221922
|| (this.associatedFile.canonicalPath == that.associatedFile.canonicalPath)
19231923
)
@@ -2176,17 +2176,16 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
21762176
case _ => NoSymbol
21772177
}
21782178

2179-
/** Desire to re-use the field in ClassSymbol which stores the source
2180-
* file to also store the classfile, but without changing the behavior
2181-
* of sourceFile (which is expected at least in the IDE only to
2182-
* return actual source code.) So sourceFile has classfiles filtered out.
2183-
*/
2184-
private def sourceFileOnly(file: AbstractFile): AbstractFile =
2185-
if ((file eq null) || (file.path endsWith ".class")) null else file
2186-
2187-
final def sourceFile: AbstractFile = sourceFileOnly(associatedFile)
2179+
// Desire to re-use the field in ClassSymbol which stores the source
2180+
// file to also store the classfile, but without changing the behavior
2181+
// of sourceFile (which is expected at least in the IDE only to
2182+
// return actual source code.) So sourceFile has classfiles filtered out.
2183+
final def sourceFile: AbstractFile =
2184+
if ((associatedFile eq NoAbstractFile) || (associatedFile.path endsWith ".class")) null else associatedFile
21882185

2189-
/** Overridden in ModuleSymbols to delegate to the module class. */
2186+
/** Overridden in ModuleSymbols to delegate to the module class.
2187+
* Never null; if there is no associated file, returns NoAbstractFile.
2188+
*/
21902189
def associatedFile: AbstractFile = enclosingTopLevelClass.associatedFile
21912190
def associatedFile_=(f: AbstractFile) { abort("associatedFile_= inapplicable for " + this) }
21922191

@@ -2932,7 +2931,11 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
29322931
if (c.isOverloaded) c.alternatives.head else c
29332932
}
29342933

2935-
override def associatedFile = if (owner.isPackageClass) _associatedFile else super.associatedFile
2934+
override def associatedFile = (
2935+
if (!owner.isPackageClass) super.associatedFile
2936+
else if (_associatedFile eq null) NoAbstractFile // guarantee not null, but save cost of initializing the var
2937+
else _associatedFile
2938+
)
29362939
override def associatedFile_=(f: AbstractFile) { _associatedFile = f }
29372940

29382941
override def reset(completer: Type): this.type = {
@@ -2981,9 +2984,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
29812984
clone.typeOfThis = typeOfThis
29822985
clone.thisSym setName thisSym.name
29832986
}
2984-
if (_associatedFile ne null)
2985-
clone.associatedFile = _associatedFile
2986-
2987+
clone.associatedFile = _associatedFile
29872988
clone
29882989
}
29892990

@@ -3169,7 +3170,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
31693170
override def enclosingTopLevelClass: Symbol = this
31703171
override def enclosingPackageClass: Symbol = this
31713172
override def enclMethod: Symbol = this
3172-
override def associatedFile = null
3173+
override def associatedFile = NoAbstractFile
31733174
override def ownerChain: List[Symbol] = List()
31743175
override def ownersIterator: Iterator[Symbol] = Iterator.empty
31753176
override def alternatives: List[Symbol] = List()

0 commit comments

Comments
 (0)