Skip to content
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

mkAttributedRef(PredefModule) creates scala.this.Predef, which is benign but leaky #9473

Closed
scabug opened this issue Sep 16, 2015 · 2 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Sep 16, 2015

scala> gen.mkAttributedRef(predef.owner.thisType,predef)
res4: $r.intp.global.gen.global.RefTree = scala.this.Predef

One glaring example of this odd choice is in root imports, rather than import scala.Predef._, we actually have import scala.this.Predef._. This leaks into the trees based on this import:

%  scalac -Xprint:typer sandbox/foo.scala
[[syntax trees at end of                     typer]] // foo.scala
package <empty> {
  class C extends scala.AnyRef {
    def <init>(): C = {
      C.super.<init>();
      ()
    };
    def x: Int = 0;
    def test: Int = scala.this.Predef.identity[Int](1)
  }
}

With this patch:

~/code/scala on topic/completely-2.11*
⚡ git diff head
diff --git a/src/reflect/scala/reflect/internal/TreeGen.scala b/src/reflect/scala/reflect/internal/TreeGen.scala
index 201b727..9bb4fc0 100644
--- a/src/reflect/scala/reflect/internal/TreeGen.scala
+++ b/src/reflect/scala/reflect/internal/TreeGen.scala
@@ -129,7 +129,8 @@ abstract class TreeGen {

   /** Builds a reference to given symbol. */
   def mkAttributedRef(sym: Symbol): RefTree =
-    if (sym.owner.isClass) mkAttributedRef(sym.owner.thisType, sym)
+    if (sym.owner.hasPackageFlag) mkAttributedRef(sym.owner.typeOfThis, sym)
+    else if (sym.owner.isClass) mkAttributedRef(sym.owner.thisType, sym)
     else mkAttributedIdent(sym)

   def mkUnattributedRef(sym: Symbol): RefTree = mkUnattributedRef(sym.fullNameAsName('.'))

We get:

% qscalac -Xprint:typer sandbox/foo.scala
[[syntax trees at end of                     typer]] // foo.scala
package <empty> {
  class C extends scala.AnyRef {
    def <init>(): C = {
      C.super.<init>();
      ()
    };
    def x: Int = 0;
    def test: Int = scala.Predef.identity[Int](1)
  }
}
@scabug
Copy link
Author

scabug commented Sep 16, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9473?orig=1
Reporter: @retronym

@scabug
Copy link
Author

scabug commented Sep 16, 2015

@retronym said:
Why I called it benign:

scala> val ScalaPackageClass = symbolOf[Predef.type].owner
ScalaPackageClass: $r.intp.global.Symbol = package scala

scala> showRaw(ScalaPackageClass.typeOfThis)
res0: String = SingleType(ThisType(<root>), scala)

scala> showRaw(ScalaPackageClass.thisType)
res1: String = ThisType(scala)

scala> ScalaPackageClass.typeOfThis =:= ScalaPackageClass.thisType
res2: Boolean = true

@scabug scabug closed this as completed Sep 22, 2015
@scabug scabug added the quickfix label Apr 7, 2017
@scabug scabug added this to the 2.12.0-M3 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants