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
Exclude universal members (getClass, toString, etc) from root module import #8541
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,17 @@ | |
package scala.tools.nsc | ||
package typechecker | ||
|
||
import scala.collection.{immutable, mutable} | ||
import scala.annotation.tailrec | ||
import scala.reflect.internal.util.{ ReusableInstance, shortClassOfInstance, ListOfNil, SomeOfNil } | ||
import scala.collection.{immutable, mutable} | ||
import scala.reflect.internal.util.{ReusableInstance, shortClassOfInstance, ListOfNil, SomeOfNil} | ||
import scala.util.chaining._ | ||
|
||
/** | ||
* @author Martin Odersky | ||
*/ | ||
trait Contexts { self: Analyzer => | ||
import global._ | ||
import definitions.{JavaLangPackage, ScalaPackage, PredefModule, ScalaXmlTopScope, ScalaXmlPackage} | ||
import definitions.{JavaLangPackage, ScalaPackage, PredefModule, ScalaXmlTopScope, ScalaXmlPackage, isUniversalMember} | ||
import ContextMode._ | ||
import scala.reflect.internal.Flags._ | ||
|
||
|
@@ -137,7 +138,16 @@ trait Contexts { self: Analyzer => | |
} | ||
|
||
def rootContext(unit: CompilationUnit, tree: Tree = EmptyTree, throwing: Boolean = false, checking: Boolean = false): Context = { | ||
val rootImportsContext = rootImports(unit).foldLeft(startContext)((c, sym) => c.make(gen.mkWildcardImport(sym), unit = unit)) | ||
val rootImportsContext = rootImports(unit).foldLeft(startContext) { (c, sym) => | ||
if (sym.isPackage) c.make(gen.mkWildcardImport(sym), unit = unit) | ||
else { | ||
val universals = sym.typeSignature.members.filter(isUniversalMember).map(_.name).toSet | ||
val sels = universals.iterator | ||
.map(ImportSelector.mask).toList | ||
.appended(ImportSelector.wild) | ||
c.make(gen.mkImportFromSelector(sym, sels), unit = unit) | ||
} | ||
} | ||
|
||
// there must be a scala.xml package when xml literals were parsed in this unit | ||
if (unit.hasXml && ScalaXmlPackage == NoSymbol) | ||
|
@@ -150,10 +160,7 @@ trait Contexts { self: Analyzer => | |
if (!unit.hasXml || ScalaXmlTopScope == NoSymbol) rootImportsContext | ||
else rootImportsContext.make(gen.mkImport(ScalaXmlPackage, nme.TopScope, nme.dollarScope)) | ||
|
||
val c = contextWithXML.make(tree, unit = unit) | ||
|
||
c.initRootContext(throwing, checking) | ||
c | ||
contextWithXML.make(tree, unit = unit).tap(_.initRootContext(throwing, checking)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fancy! |
||
} | ||
|
||
def rootContextPostTyper(unit: CompilationUnit, tree: Tree = EmptyTree): Context = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
t5389.scala:2: error: not found: object ne | ||
import ne.scala | ||
^ | ||
one error found |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
import ne.scala | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So @jducoeur's paying you under the table after all, hmm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've owned the domain for less than 24 hours, and I'm already a world power! |
||
|
||
class C |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hurts compiler performance markedly. I haven't investigated profiles, so I'm not sure whether the cost is creating this
Import
or the overhead of subsequent symbol lookups using it rather than the simple wildcard.Could we instead make
ImportInfo.importedSymbol
exclude these, perhaps conditionally onisRootImport
?It already filters its results through
isUnimportable
which excludes constructors andprivate[this]
. There is also logic in there that purports to stop all imports of members ofAnyRef
, but was disabled fully, maybe unintentionally, in 2b4cd6c.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely for minor values of major.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was more worried about compiler performance when merging this one: https://github.com/scala/scala/pull/8538/files#diff-62ccdfe29e58b8e9f06ed66d4cfc88a5R590-R593
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically
Any
is not your friend. And how could it be?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JZ's improvement in the linked PR. I'll follow up by running benchmarks and then by putting LR's mind at ease, if I am able.