http:// soc.jayunit.net Ruby Type Inference & Code Completion for RDT Google Summer of Code Jason Morrison
Dec 23, 2014
http://soc.jayunit.net
Ruby Type Inference &
Code Completion for RDTGoogle Summer of Code
Jason Morrison
Type Inference and RDT http://soc.jayunit.net
Overview
• Project Goals• Why Eclipse and Java?• Completion Features• Type Inference Algorithm• Issues• The Future
Type Inference and RDT http://soc.jayunit.net
Project Goals
Type Inference and RDT http://soc.jayunit.net
Project Goals
• Code completion for RDT– I’m lazy; Ruby is great and succinct, but…– And, it helps other developers!
Type Inference and RDT http://soc.jayunit.net
Project Goals
• Ruby type inference– It’s fun! (Read: I knew very little about it)– Alternative implementation performance
• Priming the cache for polymorphic dispatch
Type Inference and RDT http://soc.jayunit.net
Why Eclipse and Java?
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
“Eclipse is too big!”
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
“It’s bloated! :P”
Type Inference and RDT http://soc.jayunit.net
Type Inference and RDT http://soc.jayunit.net
Not targeting that guy
Type Inference and RDT http://soc.jayunit.net
Convert the natives!
Type Inference and RDT http://soc.jayunit.net
Completion Features
Type Inference and RDT http://soc.jayunit.net
Completion Features
• Scoped object completion• Method completion
Type Inference and RDT http://soc.jayunit.net
Scoped Object Completion
• Locals, Block Locals• @, @@, $• Pre-defined variables
– $_, $:, $”, $0 and friends
• Available classes
Type Inference and RDT http://soc.jayunit.net
Scoped Object Completion
• Tricky spots:– Open classes– No explicit declaration– set_instance_var and friends
Type Inference and RDT http://soc.jayunit.net
Method Completion
• Invocations are message passing• Type is a bag of possible methods
– Selector– [Args]– [Confidence]
Type Inference and RDT http://soc.jayunit.net
Method Completion
• Many scoping and definition syntaxes:– class Foo;def method;…– instance_eval…– class_eval…– define_method…– include FooModule– Object#extend(module)– class << self; def method;…– method_missing…
Type Inference and RDT http://soc.jayunit.net
Type Inference Algorithm
Type Inference and RDT http://soc.jayunit.net
What is Type Inference?
• Determine the type of an expression
• Static analysis• Type versus Class• Not always definitive
Type Inference and RDT http://soc.jayunit.net
Type Inference Algorithm
• Inspired by DDP by Lex Spoon:– Demand-Driven Analysis with Goal
Pruning
• Type flow analysis• Unions types over contours
Type Inference and RDT http://soc.jayunit.net
DDP Structure
• Posts goals• Goals have sub-goals• Answered by judgements• Prunes low value goals
– High cost– Low impact on high level goals
Type Inference and RDT http://soc.jayunit.net
DDP Goals
• 1. Type goal– What is the type of x?
• 2. Transitive flow– Where can objects flow from x?
• 3. Simple flow– Where can objects flow from x in one step?
• 4. Sender– What statements invoke FooClass#bar?
• 5. Responder– What methods are invoked by "foo.bar"?
Type Inference and RDT http://soc.jayunit.net
DataFlowTypeInferrer
• infer(String source, int offset)• findSenders(String scope, String
selector)• findMethodDef(String scope, String
selector)
Type Inference and RDT http://soc.jayunit.net
DataFlowTypeInferrer
Type Inference and RDT http://soc.jayunit.net
Issues
• Eval• Data-bound flow control
– return ( rand() > 0.5 ) ? String.new : Array.new
• Metaprogramming• Collections• Builtins
Type Inference and RDT http://soc.jayunit.net
The Future
• Statistical type inference• Type metadata (collections)• Rails tie-in
– AR querying for magic find– Instance vars in View templates
• TI for implementation optimizations
Type Inference and RDT http://soc.jayunit.net
Thanks!
• Chris Williams, project mentor!• Google Summer of Code• Everyone on the RDT and JRuby teams• All the folks at RubyCentral