summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2016-03-14 11:45:27 -0700
committerAdriaan Moors <adriaan.moors@typesafe.com>2016-03-14 11:45:27 -0700
commitcbba68fb6085f238097fbf1635046051bd7c6889 (patch)
treec2ad5795f1fc7935a543018854264d3f431df2cd /src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
parent067a4d36a6d30d6431a520c9db8ec4714f1a8675 (diff)
parent1706a37eb84ec252aea77bccebad3e48448534ad (diff)
downloadscala-cbba68fb6085f238097fbf1635046051bd7c6889.tar.gz
scala-cbba68fb6085f238097fbf1635046051bd7c6889.tar.bz2
scala-cbba68fb6085f238097fbf1635046051bd7c6889.zip
Merge 2.11.x into 2.12.x
Resolved conflicts as in b0e05b67c7
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/RefChecks.scala')
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/RefChecks.scala85
1 files changed, 47 insertions, 38 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
index 9261d6b851..da269168ec 100644
--- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
@@ -1507,9 +1507,8 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
}
}
- private def transformCaseApply(tree: Tree, ifNot: => Unit) = {
+ private def isSimpleCaseApply(tree: Tree): Boolean = {
val sym = tree.symbol
-
def isClassTypeAccessible(tree: Tree): Boolean = tree match {
case TypeApply(fun, targs) =>
isClassTypeAccessible(fun)
@@ -1517,40 +1516,36 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
( // SI-4859 `CaseClass1().InnerCaseClass2()` must not be rewritten to `new InnerCaseClass2()`;
// {expr; Outer}.Inner() must not be rewritten to `new Outer.Inner()`.
treeInfo.isQualifierSafeToElide(module) &&
- // SI-5626 Classes in refinement types cannot be constructed with `new`. In this case,
- // the companion class is actually not a ClassSymbol, but a reference to an abstract type.
- module.symbol.companionClass.isClass
- )
+ // SI-5626 Classes in refinement types cannot be constructed with `new`. In this case,
+ // the companion class is actually not a ClassSymbol, but a reference to an abstract type.
+ module.symbol.companionClass.isClass
+ )
}
- val doTransform =
- sym.isSourceMethod &&
+ sym.isSourceMethod &&
sym.isCase &&
sym.name == nme.apply &&
isClassTypeAccessible(tree) &&
- !tree.tpe.resultType.typeSymbol.primaryConstructor.isLessAccessibleThan(tree.symbol)
-
- if (doTransform) {
- def loop(t: Tree): Unit = t match {
- case Ident(_) =>
- checkUndesiredProperties(t.symbol, t.pos)
- case Select(qual, _) =>
- checkUndesiredProperties(t.symbol, t.pos)
- loop(qual)
- case _ =>
- }
- tree foreach {
- case i@Ident(_) =>
- enterReference(i.pos, i.symbol) // SI-5390 need to `enterReference` for `a` in `a.B()`
- case _ =>
- }
- loop(tree)
- toConstructor(tree.pos, tree.tpe)
+ !tree.tpe.finalResultType.typeSymbol.primaryConstructor.isLessAccessibleThan(tree.symbol)
+ }
+
+ private def transformCaseApply(tree: Tree) = {
+ def loop(t: Tree): Unit = t match {
+ case Ident(_) =>
+ checkUndesiredProperties(t.symbol, t.pos)
+ case Select(qual, _) =>
+ checkUndesiredProperties(t.symbol, t.pos)
+ loop(qual)
+ case _ =>
}
- else {
- ifNot
- tree
+
+ tree foreach {
+ case i@Ident(_) =>
+ enterReference(i.pos, i.symbol) // SI-5390 need to `enterReference` for `a` in `a.B()`
+ case _ =>
}
+ loop(tree)
+ toConstructor(tree.pos, tree.tpe)
}
private def transformApply(tree: Apply): Tree = tree match {
@@ -1590,12 +1585,24 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
// term should have been eliminated by super accessors
assert(!(qual.symbol.isTrait && sym.isTerm && mix == tpnme.EMPTY), (qual.symbol, sym, mix))
- transformCaseApply(tree,
+ // Rewrite eligible calls to monomorphic case companion apply methods to the equivalent constructor call.
+ //
+ // Note: for generic case classes the rewrite needs to be handled at the enclosing `TypeApply` to transform
+ // `TypeApply(Select(C, apply), targs)` to `Select(New(C[targs]), <init>)`. In case such a `TypeApply`
+ // was deemed ineligible for transformation (e.g. the case constructor was private), the refchecks transform
+ // will recurse to this point with `Select(C, apply)`, which will have a type `[T](...)C[T]`.
+ //
+ // We don't need to perform the check on the Select node, and `!isHigherKinded will guard against this
+ // redundant (and previously buggy, SI-9546) consideration.
+ if (!tree.tpe.isHigherKinded && isSimpleCaseApply(tree)) {
+ transformCaseApply(tree)
+ } else {
qual match {
case Super(_, mix) => checkSuper(mix)
case _ =>
}
- )
+ tree
+ }
}
private def transformIf(tree: If): Tree = {
val If(cond, thenpart, elsepart) = tree
@@ -1720,7 +1727,10 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
case TypeApply(fn, args) =>
checkBounds(tree, NoPrefix, NoSymbol, fn.tpe.typeParams, args map (_.tpe))
- transformCaseApply(tree, ())
+ if (isSimpleCaseApply(tree))
+ transformCaseApply(tree)
+ else
+ tree
case x @ Apply(_, _) =>
transformApply(x)
@@ -1739,12 +1749,11 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
case Ident(name) =>
checkUndesiredProperties(sym, tree.pos)
- transformCaseApply(tree,
- if (name != nme.WILDCARD && name != tpnme.WILDCARD_STAR) {
- assert(sym != NoSymbol, "transformCaseApply: name = " + name.debugString + " tree = " + tree + " / " + tree.getClass) //debug
- enterReference(tree.pos, sym)
- }
- )
+ if (name != nme.WILDCARD && name != tpnme.WILDCARD_STAR) {
+ assert(sym != NoSymbol, "transformCaseApply: name = " + name.debugString + " tree = " + tree + " / " + tree.getClass) //debug
+ enterReference(tree.pos, sym)
+ }
+ tree
case x @ Select(_, _) =>
transformSelect(x)