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

Add type inference #108

Open
wants to merge 122 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
dd1b568
inference work initial commit
mckaz Sep 4, 2019
d5fd382
add support for blocks, comp types, other updates
mckaz Sep 16, 2019
5902e7e
add support for heuristic inference rules. some bug fixes, refactoring.
mckaz Sep 30, 2019
bd9f6fe
refactoring and updates to constraint resolution/heuristics
mckaz Oct 18, 2019
626e16b
get rid of deletion of struct types in struct_to_nominal
mckaz Oct 26, 2019
6ac3d3f
many updates
mckaz Dec 11, 2019
757e1aa
Merge branch 'infer' of https://github.com/tupl-tufts/rdl
mckaz Dec 11, 2019
fea7876
beginning of unionvar/possible types
mckaz Dec 12, 2019
91ea750
updates to choice types
mckaz Dec 12, 2019
b943a5e
updates, mostly to choice types
mckaz Jan 2, 2020
5b98c1d
updates for dynamic type collection
mckaz Jan 14, 2020
704eb5b
some clean up, and updates to class indexer
mckaz Jan 28, 2020
7f66dd9
fixes for test failures
mckaz Feb 13, 2020
da86622
add code to treat empty array, has with type var
jeffrey-s-foster Mar 4, 2020
9838b58
start of infer tests
jeffrey-s-foster Mar 4, 2020
c20fa7c
travis ruby versions
jeffrey-s-foster Mar 4, 2020
3302573
fix some warnings
jeffrey-s-foster Mar 4, 2020
f1f5e72
fix some warnings
jeffrey-s-foster Mar 4, 2020
2471e2c
Don't call infer tests typecheck tests
jeffrey-s-foster Mar 4, 2020
1170fb1
Don't call infer tests typecheck tests
jeffrey-s-foster Mar 4, 2020
9f22b20
fix more warnings
jeffrey-s-foster Mar 4, 2020
12d36ee
get rid of @cur_meth, add klass/meth to scope
mckaz Mar 4, 2020
240f0ef
Merge branch 'infer' of https://github.com/tupl-tufts/rdl into infer
mckaz Mar 4, 2020
efc538a
change naming of array/hash param VarTypes
mckaz Mar 4, 2020
6b413dd
fix a couple of tests
jeffrey-s-foster Mar 4, 2020
05d9254
bugfix
jeffrey-s-foster Mar 4, 2020
eaa01b1
fix a 2.7.0 nit
jeffrey-s-foster Mar 4, 2020
ddae8db
fix printing solution
jeffrey-s-foster Apr 7, 2020
9eb92de
fix reset a bit
jeffrey-s-foster Apr 7, 2020
a2d8c99
Add RDL.infer_added, which takes a block and marks all methods create…
jeffrey-s-foster Apr 8, 2020
8133510
skip methods marked no_infer_meth
jeffrey-s-foster Apr 9, 2020
a4990fa
Change misssing ast to warning for infer
jeffrey-s-foster Apr 10, 2020
381aa49
Fix typo in readme, fix note_type
jeffrey-s-foster Apr 12, 2020
8794511
[WIP] Infer unit tests (#95)
Apr 22, 2020
7666873
Add gems for travis
jeffrey-s-foster Apr 23, 2020
6f775f7
Added a config reset option
jeffrey-s-foster Apr 23, 2020
a86a89b
suppress infer info messages unless RDL::config.instance.infer_verbos…
jeffrey-s-foster Apr 24, 2020
a764305
[WIP] Infer unit tests pattern matcher (#96)
May 3, 2020
ae136f6
fix reset (#97)
ngsankha May 3, 2020
5318987
record module includes/extends
jeffrey-s-foster May 3, 2020
aac984a
merge
jeffrey-s-foster May 3, 2020
b79be68
Continue on Inference Errors (#98)
May 7, 2020
c0fec0b
support type checking with module mixins
jeffrey-s-foster May 8, 2020
830802f
merge
jeffrey-s-foster May 8, 2020
355e9b2
better error message for unsupported expression kinds
jeffrey-s-foster May 8, 2020
e2f306c
Add test_match_with_lvasgn
jeffrey-s-foster May 8, 2020
93d3c99
implement match_with_lvasgn
jeffrey-s-foster May 8, 2020
0846937
Allow constants in default args
jeffrey-s-foster May 9, 2020
5391674
add twin_network heuristic
mckaz May 11, 2020
e3f6aa1
remove effects across RDL
mckaz May 11, 2020
d516022
remove effects across RDL (#100)
mckaz May 12, 2020
e4b986c
Merge branch 'infer' of https://github.com/tupl-tufts/rdl into infer
mckaz May 12, 2020
509f1c8
Merge branch 'infer' into ml_infer
mckaz May 12, 2020
f50f60f
* Add wrap log area
jeffrey-s-foster May 12, 2020
5011ae8
Allow A<x> <= B<y> if B is an ancestor of A and A and B have the same…
jeffrey-s-foster May 12, 2020
2066e8e
Types for PrettyPrint
jeffrey-s-foster May 13, 2020
85c9d86
new test case for when with blocks
jeffrey-s-foster May 13, 2020
2813f48
Add a to_s method to Env, clarify test_when_block
jeffrey-s-foster May 19, 2020
910806c
add logging of var name/types in an app, query twin network against a…
mckaz May 21, 2020
a6784ee
added note_type test (#99)
May 26, 2020
5587375
Infer filtering (#101)
May 26, 2020
8be9660
* Add assumption that methods that take blocks with effects don't sto…
jeffrey-s-foster May 28, 2020
39e86d2
Merge branch 'infer' of github.com:tupl-tufts/rdl into infer
jeffrey-s-foster May 28, 2020
9d79b6b
bug fix
mckaz May 28, 2020
8f81cc2
fix error with block args being propagated to outer scope
jeffrey-s-foster May 28, 2020
238d81f
* Remove `Env#fix` and replace with new `force` param to `Env#bind`
jeffrey-s-foster May 28, 2020
3baf769
Fix some PrettyPrint types
jeffrey-s-foster May 28, 2020
703bb7d
get_ast always returns nil if it can't find the source
jeffrey-s-foster May 28, 2020
ccefc99
fix handling of optional AnnotatedArgTypes when checking method subty…
mckaz May 29, 2020
68dfcb7
Fix a couple of nil errors
jeffrey-s-foster Jun 1, 2020
b53bb8c
Fix typos in Type#solution=
jeffrey-s-foster Jun 2, 2020
401926e
Infer Output Sorbet Types (#102)
Jun 2, 2020
e721f94
fix for Array#+ type so it handles ActiveRecord::Relation when Rails …
mckaz Jun 11, 2020
d1d69c5
small type check bug fix
mckaz Jun 11, 2020
fb39926
small fix to rails comp type
mckaz Jun 11, 2020
ec3b657
fix methods used in extraction report
mckaz Jun 11, 2020
13eec83
small fix in sorbet type reporting
mckaz Jun 11, 2020
1efc6da
fix env issue in tc_block
mckaz Jun 11, 2020
bba5179
re-fix method reporting
mckaz Jun 12, 2020
062f3f7
fix env handling for map case"
mckaz Jun 12, 2020
86679da
fix sorbet reporting of hash value optional types, and also of non-Me…
mckaz Jun 12, 2020
cbc0271
minor
jeffrey-s-foster Jun 13, 2020
916867d
Merge branch 'infer' of github.com:tupl-tufts/rdl into infer
jeffrey-s-foster Jun 13, 2020
6dcfe77
add nowrap to PrettyPrint
mckaz Jun 18, 2020
d358646
Merge branch 'infer' of github.com:tupl-tufts/rdl into infer
jeffrey-s-foster Jun 18, 2020
ef638c9
additional type for Enumerable#all?
mckaz Jun 18, 2020
6e78549
Merge branch 'infer' of github.com:tupl-tufts/rdl into infer
jeffrey-s-foster Jun 18, 2020
f9d5ee2
fix ileft for structral type cases in leq, and add test case
mckaz Jun 19, 2020
4d7fb21
Merge branch 'infer' of https://github.com/tupl-tufts/rdl into infer
mckaz Jun 19, 2020
21062d0
fix to Array#each type
mckaz Jun 22, 2020
0c31eb5
small fix to matching_classes (#103)
Dargones Jun 28, 2020
58f92d0
fix ordering of union/intersection types by basing it on string name
mckaz Jul 16, 2020
9eb6f48
merge
mckaz Jul 17, 2020
abd88f7
bug fixes for twin network queries
mckaz Jul 30, 2020
7b45dac
fix printing of singleton classes in sorbet
jeffrey-s-foster Aug 19, 2020
a03544f
Matching classes speed-up and more logging (#104)
Dargones Aug 19, 2020
4a9ca62
tweak Hash#merge to return Hash<x, y> for VarType cases
mckaz Sep 21, 2020
1790dcc
type prediction for args with codeBERT + cosine similarity
mckaz Oct 16, 2020
eb478e1
add BERT guesses for instance vars, rets, and add some caching and re…
mckaz Nov 6, 2020
e90badc
latest updates
mckaz Dec 22, 2020
5234486
many updates, just before working on batch queries
mckaz Jan 22, 2021
1e9fe8f
latest updates
mckaz Apr 6, 2021
ffa3003
get rid of temp file
mckaz Apr 9, 2021
f32d529
latest progress
mckaz Apr 19, 2021
aa11ed4
switch to not using twin, for now
mckaz Apr 19, 2021
c378049
fix for loading rails when using rake
mckaz Apr 19, 2021
335c283
add pathname dependency
mckaz Apr 19, 2021
9439801
dont use twin
mckaz Apr 19, 2021
16f2c84
Merge branch 'ml_infer' of https://github.com/mckaz/rdl into ml_infer
mckaz Apr 19, 2021
305e93b
updates for artifact
mckaz Jul 8, 2021
9f72be5
Merge branch 'ml_infer' of https://github.com/mckaz/rdl into ml_infer
mckaz Jul 8, 2021
20feaec
minor updates
mckaz Jul 9, 2021
b54f076
typo fix
mckaz Jul 9, 2021
a7d10bb
attempt fix for CDO schema loading issue
mckaz Jul 9, 2021
27025f7
initial merge
mwaldrich Jun 14, 2022
792c40f
undid ML changes to heuristics
mwaldrich Jun 15, 2022
761da45
skipped test
mwaldrich Jun 15, 2022
d18d6c7
Merge branch 'mckaz-ml_infer' into infer
mwaldrich Jun 15, 2022
117e735
added render: json type
mwaldrich Jun 17, 2022
124d38e
readded the simpler render type
mwaldrich Jun 17, 2022
93b0ab1
log global type constraints
mwaldrich Jun 17, 2022
061bb21
undo; didn't log the correct info
mwaldrich Jun 17, 2022
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
89 changes: 45 additions & 44 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf210
{\fonttbl\f0\fnil\fcharset0 Consolas;}
{\colortbl;\red255\green255\blue255;\red38\green38\blue38;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

\f0\fs24 \cf2 # Compiled source #\
###################\
*.com\
*.class\
*.dll\
*.exe\
*.o\
*.so\
\'a0\
# Packages #\
############\
# it's better to unpack these files and commit the raw source\
# git has its own built in compression methods\
*.7z\
*.dmg\
*.gz\
*.iso\
*.jar\
*.rar\
*.tar\
*.zip\
*.gem\
\'a0\
# Logs and databases #\
######################\
*.log\
*.sql\
*.sqlite\
\'a0\
# OS generated files #\
######################\
.DS_Store\
.DS_Store?\
._*\
.Spotlight-V100\
.Trashes\
ehthumbs.db\
Thumbs.db}
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.gem

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

### Editors
.#*

### Ruby
*.gem

### RDL
*.csv
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Metrics/MethodLength:
Max: 30

Lint/BooleanSymbol:
Enabled: false
19 changes: 3 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@ language: ruby
gemfile:
- gemfiles/Gemfile.travis
rvm:
- 2.1.1
- 2.1.6
- 2.1.7
- 2.1.8
- 2.2.0
- 2.2.1
- 2.2.2
- 2.2.3
- 2.2.4
- 2.2.5
- 2.2.6
- 2.3.0
- 2.3.1
- 2.3.2
- 2.3.3
- 2.3.4
- 2.4.0
- 2.4.1
- 2.4.2
Expand All @@ -27,6 +11,9 @@ rvm:
- 2.5.5
- 2.6.2
- 2.6.3
- 2.6.4
- 2.6.5
- 2.7.0
notifications:
email:
recipients:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ Value types can also be declared as optional, indicating that the key/value pair
```ruby
type MyClass, :foo, '(a: Integer, b: ?String) { () -> %any } -> %any'
```
With this type, `foo` takes a hash where key `:a` is mapped to an `Integer`, and furthermore the hash may or may not include a key `:b` that is mapped to a `String`.
With this type, `foo` takes a hash where key `:a` is mapped to an `Integer`, and furthermore the hash may or may not include a key `:b` that is mapped to a `String`.

RDL also allows a "rest" type in finite hashes (of course, they're not so finite if they use it!):
```ruby
Expand Down Expand Up @@ -734,7 +734,7 @@ RDL uses the same approach for hashes: hash literals are treated as finite hashe

## Other Features and Limitations

*Displaying types.* As an aid to debugging, the method `RDL.note_type e` will display the type of `e` during type checking. At run time, this method returns its argument. Note that in certain cases RDL may type check the same code repeatedly, in which case an expression's type could be printed multiple times.
* *Displaying types.* As an aid to debugging, the method `RDL.note_type e` will display the type of `e` during type checking. At run time, this method returns its argument. Note that in certain cases RDL may type check the same code repeatedly, in which case an expression's type could be printed multiple times.

* *Conditional guards and singletons.* If an `if` or `unless` guard has a singleton type, RDL will typecheck both branches but not include types from the unrealizable branch in the expression type. For example, `if true then 1 else 'two' end` has type `1`. RDL behaves similarly for `&&` and `||`. However, RDL does not implement this logic for `case`.

Expand Down Expand Up @@ -762,6 +762,7 @@ RDL's static type checker makes some assumptions that should hold unless your Ru
* `Class#===` is not redefined
* `Proc#call` is not redefined
* `Object#class` is not redefined
* Methods that take blocks with externally visible effects do not store those blocks and then call them later

(More assumptions will be added here as they are added to RDL...)

Expand Down
5 changes: 5 additions & 0 deletions gemfiles/Gemfile.travis
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ source 'https://rubygems.org'
gem 'rake'
gem 'minitest'
gem 'parser'
gem 'sql-parser'
gem 'method_source'
gem 'colorize'
gem 'coderay'
gem 'parlour'
1 change: 1 addition & 0 deletions lib/rdl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This wrapper file allows us to completely disable RDL in certain modes.
if defined?(Rails)
require 'active_record' if !defined?(ActiveRecord)
require 'rdl/boot_rails'
else
require 'rdl/boot'
Expand Down
64 changes: 60 additions & 4 deletions lib/rdl/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
require 'digest'
require 'set'
require 'parser/current'
#require 'method_source'
require 'colorize'
require 'pathname'
require 'rake'
require 'net/http'

module RDL
end

require 'rdl/config.rb'
require 'rdl/logging.rb'
def RDL.config
yield(RDL::Config.instance)
end
Expand Down Expand Up @@ -49,6 +55,17 @@ module RDL::Globals
@to_typecheck = Hash.new
@to_typecheck[:now] = Set.new

# Map from symbols to set of [class, method] pairs to infer when those symbols are rdl_do_infer'd
# (or the methods are defined, for the symbol :now)
@to_infer = Hash.new
@to_infer[:now] = Set.new

## List of [klass, method] pairs for which we have generated constraints.
## That is, if we look up RDL::Globals.info.get(klass, meth, :type), we will get a single MethodType
## composed of VarTypes with constraints.
## TODO: add inst/class vars to this list?
@constrained_types = []

# Map from symbols to Array<Proc> where the Procs are called when those symbols are rdl_do_typecheck'd
@to_do_at = Hash.new

Expand All @@ -59,13 +76,29 @@ module RDL::Globals
@dep_types = []

## Hash mapping node object IDs (integers) to a list [tmeth, tmeth_old, tmeth_res, self_klass, trecv_old, targs_old], where: tmeth is a MethodType that is fully evaluated (i.e., no ComputedTypes) *and instantiated*, tmeth_old is the unevaluated method type (i.e., with ComputedTypes), tmeth_res is the result of evaluating tmeth_old *but not instantiating it*, self_klass is the class where the MethodType is defined, trecv_old was the receiver type used to evaluate tmeth_old, and targs_old is an Array of the argument types used to evaluate tmeth_old.
@comp_type_map = {}
@comp_type_map = Hash.new

# Map from ActiveRecord table names (symbols) to their schema types, which should be a Table type
@ar_db_schema = {}
@ar_db_schema = Hash.new

# Map from Sequel table names (symbols) to their schema types, which should be a Table type
@seq_db_schema = {}
@seq_db_schema = Hash.new

# Array<[String, String]>, where each first string is a class name and each second one is a method name.
# klass/method pairs here should not be inferred.
@no_infer_meths = []

# Array<String> of absolute file paths for files that should not be inferred.
@no_infer_files = []

# If non-nil, should be a symbol. Added, untyped methods will be tagged
# with that symbol
@infer_added = nil

# Hash<Module module, [Class/Module class/module_name, :include or :extend]>
# Map from module names k to pairs [class/module v, :include/:extend] indicating the module
# k was included/extended in v
@module_mixees = Hash.new
end

class << RDL::Globals # add accessors and readers for module variables
Expand All @@ -75,12 +108,19 @@ class << RDL::Globals # add accessors and readers for module variables
attr_reader :aliases
attr_accessor :to_wrap
attr_accessor :to_typecheck
attr_accessor :to_infer
attr_accessor :constrained_types
attr_accessor :to_do_at
attr_accessor :deferred
attr_accessor :dep_types
attr_accessor :comp_type_map
attr_accessor :ar_db_schema
attr_accessor :seq_db_schema
attr_accessor :no_infer_meths
attr_accessor :no_infer_files
attr_accessor :infer_added
attr_accessor :infer_added_filter_dirs
attr_accessor :module_mixees
end

# Create switches to control whether wrapping happens and whether
Expand Down Expand Up @@ -122,6 +162,7 @@ class << RDL::Globals
require 'rdl/types/type_query.rb'
require 'rdl/types/union.rb'
require 'rdl/types/var.rb'
require 'rdl/types/choice.rb' ## depends on var.rb
require 'rdl/types/vararg.rb'
require 'rdl/types/wild_query.rb'
require 'rdl/types/string.rb'
Expand All @@ -133,9 +174,13 @@ class << RDL::Globals
require 'rdl/contracts/proc.rb'

require 'rdl/util.rb'
require 'rdl/class_indexer.rb'
require 'rdl/wrap.rb'
require 'rdl/query.rb'
require 'rdl/typecheck.rb'
require 'rdl/reporting/reporting.rb'
require 'rdl/constraint.rb'
require 'rdl/heuristics.rb'
#require_relative 'rdl/stats.rb'

class << RDL::Globals
Expand All @@ -148,17 +193,28 @@ class << RDL::Globals
module RDL
def self.reset
RDL::Globals.module_eval {
RDL::Config.reset
@info = RDL::Info.new
@wrapped_calls = Hash.new 0
@type_params = Hash.new
@aliases = Hash.new
@to_wrap = Set.new
@to_typecheck = Hash.new
@to_typecheck[:now] = Set.new
@to_infer = Hash.new
@to_infer[:now] = Set.new
@constrained_types = []
@to_do_at = Hash.new
@deferred = []
# @dep_types = []
# @comp_type_map = Hash.new
@ar_db_schema = Hash.new
@seq_db_schema = Hash.new
@deferred = []
@no_infer_meths = []
@no_infer_files = []
@infer_added = nil
# @module_mixees = Hash.new - resetting this breaks test cases that assume
# we know which modules are mixed into which other modules

@parser = RDL::Type::Parser.new

Expand Down
1 change: 1 addition & 0 deletions lib/rdl/boot_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ def self.params_type(typs); end
else
raise RuntimeError, "Don't know what to do in Rails environment #{Rails.env}"
end

Loading