Skip to content

Encode static-final constant fields as Singletons#2085

Merged
jsuereth merged 10 commits intosbt:0.13from
twitter-forks:stuhood/java-static-final-fields-as-singletons
Jul 14, 2015
Merged

Encode static-final constant fields as Singletons#2085
jsuereth merged 10 commits intosbt:0.13from
twitter-forks:stuhood/java-static-final-fields-as-singletons

Conversation

@stuhood
Copy link

@stuhood stuhood commented Jul 2, 2015

... so that when their values change, the APIs change and downstream consumers are invalidated. See #1967

There are 3 TODOs in the code currently:

  1. We re-parse the classfile for every final field we encounter; should instead plumb through a lazy val for the parsed representation
  2. Could skip parsing for non-static fields, since javac won't inline them
  3. Should add a negative test, split out a JavacForUnitTests class, and move the new test to the appropriate package

@lightbend-cla-validator
Copy link

Hi @stuhood,

Thank you for your contribution! We really value the time you've taken to put this together.

Before we proceed with reviewing this pull request, please sign the Typesafe Contributors License Agreement:

http://www.typesafe.com/contribute/cla

@eed3si9n eed3si9n added the ready label Jul 2, 2015
@typesafe-tools
Copy link

Can one of the admins verify this patch?

@stuhood
Copy link
Author

stuhood commented Jul 7, 2015

@jsuereth, @eed3si9n : I believe this one is ready for review... would you mind taking a look?

@jsuereth
Copy link
Member

jsuereth commented Jul 9, 2015

@stuhood on it now, sorry for delay.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe the TODO shoudl be about reading as much of the APi as possible from a classfile parser rather than reflection. Reflection locks us into a specific JDK we have to be running on, where as parsing frees that.

@jsuereth
Copy link
Member

jsuereth commented Jul 9, 2015

This LGTM. My comments are all very minor, but usability related. Can't wait to see more of these :)

@adriaanm
Copy link
Contributor

adriaanm commented Jul 9, 2015

Skimming on BART, I'm wondering whether the compiler's ConstantType is available here? When type checking, a final val without an expected type is typed as a ConstantType that tracks its value, denoting it can be constant folded. In general, it's not safe to do this for a SingletonType. I'll have a deeper look at the latest when M2 is out.

@jsuereth
Copy link
Member

jsuereth commented Jul 9, 2015

@adriaanm We may need to add a new subclass to denote ConstantType. you're right that may be better as an option in the future. Shouldn't be a difficult change to make. Let use know after M2 :)

@jsuereth
Copy link
Member

Cleanups look great!, just waiting on @adriaanm if he has any concerns over how we've encoded the types of the constant pool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to elaborate a bit on why these parts (class name, field name, constant value) are included in the singleton type's path. The thought process of a future incremental compiler hacker would have to go something like this. If the class name changes, but the constant does not, should we recompile? (I assume we don't.) So, does it suffice to include the value? But wait! If you change the constant's value, and not the class name, the change won't be detected, so I should include the reference to the field to encode the dependency.

How about the type of the field (not captured here), though? Does it matter whether I go from int to long, as, erm, long as the value doesn't change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding my earlier comment, I think it's okay not to add ConstantType to the API, but I would suggest including a marker (:snail:, to evoke its constant nature?) in the singleton type's path to make sure we can tell the difference if we ever need to.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot of questions, none of which I know the answer to. "baby's first sbt/scala patch", and so forth.

What would you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should've used a more specific format for that feedback.

I think your encoding makes sense, but it does not capture the type of the field, which is unlikely not to also result in a change of value when it changes, but it could (and thus changes to the type of the field would go undetected).

I'd also like to see some docs for the motivation of choosing these elements for the path. I think my rhetorical questions could serve as a basis for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your encoding makes sense, but it does not capture the type of the field, which is unlikely not to also result in a change of value when it changes, but it could (and thus changes to the type of the field would go undetected).

Aha! Absolutely right. Will fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test for the type change, and expanded the explanation around the choice of members in the Singleton type. Thanks!

@adriaanm
Copy link
Contributor

This is great stuff, thanks! I added a suggestion or two.

@jsuereth
Copy link
Member

Thanks @adriaanm, all good points.

@dwijnand
Copy link
Member

This needs notes as well btw.

@stuhood
Copy link
Author

stuhood commented Jul 14, 2015

@dwijnand , @adriaanm : I believe I've addressed your feedback. Thanks!

@jsuereth
Copy link
Member

LGTM

jsuereth added a commit that referenced this pull request Jul 14, 2015
…-fields-as-singletons

Encode static-final constant fields as Singletons
@jsuereth jsuereth merged commit 4abc838 into sbt:0.13 Jul 14, 2015
@jsuereth jsuereth removed the ready label Jul 14, 2015
@adriaanm
Copy link
Contributor

Thank you! I love the template approach to testing compilation. Hypnotoad approves.

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

Successfully merging this pull request may close these issues.

7 participants