Skip to content

Fix #8748: Verify symbol is a param by querying the reftree #8805

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 2 commits into from
Apr 27, 2020
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: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TreeTypeMap(
case id: Ident if tpd.needsSelect(id.tpe) =>
ref(id.tpe.asInstanceOf[TermRef]).withSpan(id.span)
case ddef @ DefDef(name, tparams, vparamss, tpt, _) =>
val (tmap1, tparams1) = transformDefs(ddef.tparams)
val (tmap1, tparams1) = transformDefs(tparams)
val (tmap2, vparamss1) = tmap1.transformVParamss(vparamss)
val res = cpy.DefDef(ddef)(name, tparams1, vparamss1, tmap2.transform(tpt), tmap2.transform(ddef.rhs))
res.symbol.setParamssFromDefs(tparams1, vparamss1)
Expand Down
17 changes: 11 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/HoistSuperArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object HoistSuperArgs {
*
* An argument is complex if it contains a method or template definition, a this or a new,
* or it contains an identifier which needs a `this` prefix to be accessed. This is the case
* if the identifer neither a global reference nor a reference to a parameter of the enclosing class.
* if the identifier has neither a global reference nor a reference to a parameter of the enclosing class.
* @see needsHoist for an implementation.
*
* A hoisted argument definition gets the parameters of the class it is hoisted from
Expand Down Expand Up @@ -116,21 +116,26 @@ class HoistSuperArgs extends MiniPhase with IdentityDenotTransformer { thisPhase
case _ => false
}

/** Only rewire types that are owned by the current Hoister and is an param or accessor */
def needsRewire(tp: Type) = tp match {
case ntp: NamedType =>
(ntp.symbol.owner == cls || ntp.symbol.owner == constr) && ntp.symbol.isParamOrAccessor
case _ => false
}

// begin hoistSuperArg
arg match {
case Apply(fn, arg1 :: Nil) if fn.symbol == defn.cbnArg =>
cpy.Apply(arg)(fn, hoistSuperArg(arg1, cdef) :: Nil)
case _ if (arg.existsSubTree(needsHoist)) =>
case _ if arg.existsSubTree(needsHoist) =>
val superMeth = newSuperArgMethod(arg.tpe)
val superArgDef = polyDefDef(superMeth, trefs => vrefss => {
val paramSyms = trefs.map(_.typeSymbol) ::: vrefss.flatten.map(_.symbol)
val tmap = new TreeTypeMap(
typeMap = new TypeMap {
lazy val origToParam = origParams.zip(paramSyms).toMap
def apply(tp: Type) = tp match {
case tp: NamedType
if (tp.symbol.owner == cls || tp.symbol.owner == constr) &&
tp.symbol.isParamOrAccessor =>
case tp: NamedType if needsRewire(tp) =>
origToParam.get(tp.symbol) match {
case Some(mappedSym) => if (tp.symbol.isType) mappedSym.typeRef else mappedSym.termRef
case None => mapOver(tp)
Expand All @@ -140,7 +145,7 @@ class HoistSuperArgs extends MiniPhase with IdentityDenotTransformer { thisPhase
}
},
treeMap = {
case tree: RefTree if paramSyms.contains(tree.symbol) =>
case tree: RefTree if needsRewire(tree.tpe) =>
cpy.Ident(tree)(tree.name).withType(tree.tpe)
case tree =>
tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MegaPhase.MiniPhase
import config.Printers.transforms

/** Add accessors for all protected accesses. An accessor is needed if
* according to the rules of the JVM a protected class member is not accesissible
* according to the rules of the JVM a protected class member is not accessible
* from the point of access, but is accessible if the access is from an enclosing
* class. In this point a public access method is placed in that enclosing class.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i8748.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class A(a: String) extends B(new C {
override def get(): String = a
})

class B(c: C)

trait C {
def get(): String
}
7 changes: 7 additions & 0 deletions tests/pos/i8786.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class B(y: Int) extends A(new C(y){})

class A(c: C)

abstract class C(y: Int) {
def x: Int = y
}