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

var_type :@ary, "Array" prohibits any access to the instance variable #76

Closed
mame opened this issue Jan 30, 2018 · 3 comments
Closed

var_type :@ary, "Array" prohibits any access to the instance variable #76

mame opened this issue Jan 30, 2018 · 3 comments

Comments

@mame
Copy link
Contributor

mame commented Jan 30, 2018

What should I do?

require "rdl"

class Foo
  extend RDL::Annotate
  var_type :@ary, "Array"

  type "() -> %any", typecheck: :now
  def foo
    @ary = []
  end
end
Traceback (most recent call last):
	11: from t.rb:3:in `<main>'

*snip*

	 1: from /home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:1079:in `tc_vasgn'
/home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:158:in `error':  (RDL::Typecheck::StaticTypeError)
t.rb:9:5: error: incompatible types: `[]' can't be assigned to variable of type `Array'
t.rb:9:     @ary = []
t.rb:9:     ^~~~~~~~~

Another case:

require "rdl"

class Foo
  extend RDL::Annotate
  var_type :@ary, "Array"

  type "() -> %any", typecheck: :now
  def foo
    @ary << 1
  end
end
$ ruby -Ilib t.rb 
Traceback (most recent call last):
	16: from t.rb:3:in `<main>'

*snip*

	 1: from /home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:1261:in `tc_send_one_recv'
/home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:158:in `error':  (RDL::Typecheck::StaticTypeError)
t.rb:9:5: error: no type information for instance method `Array#<<'
t.rb:9:     @ary << 1
t.rb:9:     ^~~~~~~~~
@mame
Copy link
Contributor Author

mame commented Jan 30, 2018

Sorry, I noticed that adding require "types/core" solves the latter case.

@mame
Copy link
Contributor Author

mame commented Jan 31, 2018

I guess that in the former case, the array literal is considered as a type of 0-member tuple, but I have not found the solution yet.

@mckaz
Copy link
Collaborator

mckaz commented Apr 16, 2019

You are correct-- in the former case, the literal is considered as tuple type [], which for soundness reasons is not considered a subtype of Array (see issue #64 for a more detailed explanation). Currently, you can solve this issue by type casting [], i.e., @ary = RDL.type_cast([], "Array").

The comp-types branch of RDL uses type-level computations to bring a greater level of precision to various library types, including Arrays. It greatly reduces the need for type casts like the aforementioned one. We'll be merging that branch with master very shortly.

@mckaz mckaz closed this as completed Apr 16, 2019
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