AnnotationInfo.original and AnnotationInfo.tree

37 views
Skip to first unread message

Matthias Langer

unread,
Feb 22, 2015, 11:00:49 AM2/22/15
to scala-i...@googlegroups.com
Hello everybody, I'm currently trying to fix some annotation related issues with scala-refactoring. I had some success so far by basically combining AnnotiationInfo.original and AnnotationInfo.tree, but some information is still missing. My problem is explained in detail here: https://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001793-organize-imports-removes-import-referenced-from-an-annotation?comment=692140933#comment:692140933

I would highly appreciate it if somebody could take a look.

Regards,
Matthias

iulian dragos

unread,
Feb 23, 2015, 10:00:51 AM2/23/15
to scala-i...@googlegroups.com
Matthias, I'll denormalise the Internet a bit. The problem is that constant-folding erases all traces in the tree after type-checking, so Organise Imports is erroneously concluding an import is unused.

import test._
package test {
  object Constants {
     final val X = 42
   }
}

@AnAnnotation(Constants.X)
class Bug


The compiler gives as two trees for analyzing @AnAnnotation(Constants.X), namely AnnotationInfo.original and with Scala-2.11 also AnnotationInfo.tree. They look like
=======orig=======
Apply(
  Select(New(Ident(AnAnnotation)), termNames.CONSTRUCTOR), List(
    AssignOrNamedArg(
      Ident(TermName("value")), Select(Ident(TermName("Constants")), TermName("X"))
    )
  )
)
=======tree=======
Apply(
  Select(New(TypeTree()), termNames.CONSTRUCTOR), List(
    AssignOrNamedArg(Ident(TermName("value")), Literal(Constant(42)))
  )
)


So, the first tree, orig, does not tell us that Ident(TermName("Constants")), TermName("X") actually is test.Constants.X, and in the second tree, tree, the compiler already filled in the constant 42, which does not help us very much ether.

I guess that taking a look at how the compiler actually transforms Ident(TermName("Constants")), TermName("X") into Literal(Constant(42)) might help, as to do so it clearly has to find out that Ident(TermName("Constants")), TermName("X")references test.Constants.X.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais

Mirko Stocker

unread,
Feb 23, 2015, 10:15:48 AM2/23/15
to scala-i...@googlegroups.com
On Monday 23 February 2015 16.00:28 iulian dragos wrote:
> Matthias, I'll denormalise the Internet a bit. The problem is that
> constant-folding erases all traces in the tree after type-checking

But, shouldn't the presentation compiler be skipping constant folding?

Cheers

Mirko

--
Mirko Stocker | mi...@stocker.email
Work: http://ifs.hsr.ch | http://infoq.com
Personal: http://misto.ch | http://twitter.com/m_st

Simon Schäfer

unread,
Feb 23, 2015, 10:20:03 AM2/23/15
to scala-i...@googlegroups.com

On 23.02.2015 16:15, Mirko Stocker wrote:
> On Monday 23 February 2015 16.00:28 iulian dragos wrote:
>> Matthias, I'll denormalise the Internet a bit. The problem is that
>> constant-folding erases all traces in the tree after type-checking
> But, shouldn't the presentation compiler be skipping constant folding?
Yes, this is what it should do...
>
> Cheers
>
> Mirko
>

iulian dragos

unread,
Feb 23, 2015, 10:34:02 AM2/23/15
to scala-i...@googlegroups.com
Unfortunately, it's not that easy. Constant-folding is necessary for type-checking, for example @switch pattern matches and annotations:

```
@switch x match { 
  case Classfile.INVOKE_STATIC => ....
}
```

it needs constant folding to make sure `match` is really a table-switch. Similarly, the JVM requires that annotations are using only constant arguments.

It's possible to design it differently, but it's not a trivial fix.

iulian

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-internals+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages