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

Rename generic class params #594

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions lib/rbs/ast/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ def ==(other)
def hash
self.class.hash ^ name.hash ^ type.hash ^ ivar_name.hash ^ kind.hash
end

def update(name: self.name, type: self.type, ivar_name: self.ivar_name, kind: self.kind, annotations: self.annotations, location: self.location, comment: self.comment)
klass = _ = self.class
klass.new(
name: name,
type: type,
ivar_name: ivar_name,
kind: kind,
annotations: annotations,
location: location,
comment: comment
)
end
end

class AttrReader < Base
Expand Down
17 changes: 12 additions & 5 deletions lib/rbs/definition_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ def build_instance(type_name, no_self_types: false)
case entry
when Environment::ClassEntry, Environment::ModuleEntry
ancestors = ancestor_builder.instance_ancestors(type_name)
self_type = Types::ClassInstance.new(name: type_name,
args: Types::Variable.build(entry.type_params.each.map(&:name)),
location: nil)
args = Types::Variable.build(entry.type_params.each.map(&:name))
self_type = Types::ClassInstance.new(name: type_name, args: args, location: nil)

Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
one_ancestors = ancestor_builder.one_instance_ancestors(type_name)
Expand Down Expand Up @@ -217,6 +216,8 @@ def build_instance(type_name, no_self_types: false)
super_interface_method: entry.is_a?(Environment::ModuleEntry))

entry.decls.each do |d|
subst = Substitution.build(d.decl.type_params.each.map(&:name), args)

d.decl.members.each do |member|
case member
when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter
Expand All @@ -229,12 +230,18 @@ def build_instance(type_name, no_self_types: false)
end

if ivar_name
insert_variable(type_name, definition.instance_variables, name: ivar_name, type: member.type)
insert_variable(type_name,
definition.instance_variables,
name: ivar_name,
type: member.type.sub(subst))
end
end

when AST::Members::InstanceVariable
insert_variable(type_name, definition.instance_variables, name: member.name, type: member.type)
insert_variable(type_name,
definition.instance_variables,
name: member.name,
type: member.type.sub(subst))

when AST::Members::ClassVariable
insert_variable(type_name, definition.class_variables, name: member.name, type: member.type)
Expand Down
11 changes: 9 additions & 2 deletions lib/rbs/definition_builder/method_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,22 @@ def build_instance(type_name)
type = Types::ClassInstance.new(name: type_name, args: args, location: nil)
Methods.new(type: type).tap do |methods|
entry.decls.each do |d|
subst = Substitution.build(d.decl.type_params.each.map(&:name), args)
each_member_with_accessibility(d.decl.members) do |member, accessibility|
case member
when AST::Members::MethodDefinition
if member.instance?
build_method(methods, type, member: member, accessibility: accessibility)
build_method(methods,
type,
member: member.update(types: member.types.map {|type| type.sub(subst) }),
accessibility: accessibility)
end
when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
if member.kind == :instance
build_attribute(methods, type, member: member, accessibility: accessibility)
build_attribute(methods,
type,
member: member.update(type: member.type.sub(subst)),
accessibility: accessibility)
end
when AST::Members::Alias
if member.kind == :instance
Expand Down
2 changes: 2 additions & 0 deletions sig/members.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ module RBS
def initialize: (name: Symbol, type: Types::t, ivar_name: Symbol | false | nil, kind: kind, annotations: Array[Annotation], location: Location?, comment: Comment?) -> void

include _HashEqual

def update: (?name: Symbol, ?type: Types::t, ?ivar_name: Symbol | false | nil, ?kind: kind, ?annotations: Array[Annotation], ?location: Location?, ?comment: Comment?) -> instance
end

class AttrReader < Base
Expand Down
36 changes: 17 additions & 19 deletions steep/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (6.1.0)
activesupport (6.1.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.1)
ast_utils (0.3.0)
parser (~> 2.4)
thor (>= 0.19)
concurrent-ruby (1.1.7)
ffi (1.13.1)
i18n (1.8.5)
ast (2.4.2)
ast_utils (0.4.0)
parser (>= 2.7.0)
concurrent-ruby (1.1.8)
ffi (1.14.2)
i18n (1.8.8)
concurrent-ruby (~> 1.0)
language_server-protocol (3.15.0.1)
listen (3.3.3)
listen (3.4.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
minitest (5.14.2)
parser (2.7.2.0)
minitest (5.14.3)
parser (3.0.0.0)
ast (~> 2.4.1)
rainbow (3.0.0)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbs (0.20.1)
steep (0.38.0)
rbs (1.0.4)
steep (0.41.0)
activesupport (>= 5.1)
ast_utils (~> 0.3.0)
ast_utils (>= 0.4.0)
language_server-protocol (~> 3.15.0.1)
listen (~> 3.0)
parser (~> 2.7.0)
parser (>= 2.7)
rainbow (>= 2.2.2, < 4.0)
rbs (>= 0.20.0)
thor (1.0.1)
tzinfo (2.0.3)
rbs (~> 1.0.3)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)

Expand All @@ -47,4 +45,4 @@ DEPENDENCIES
steep

BUNDLED WITH
2.2.0.rc.2
2.2.3
94 changes: 94 additions & 0 deletions test/rbs/definition_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1853,4 +1853,98 @@ class Bar
end
end
end

def test_generic_class_open
SignatureManager.new do |manager|
manager.files.merge!(Pathname("foo.rbs") => <<-EOF)
class Foo[A]
def foo: () -> A
end

class Foo[B]
def bar: () -> B
attr_reader Bar: B
@bar: B
end
EOF
manager.build do |env|
builder = DefinitionBuilder.new(env: env)

builder.build_instance(type_name("::Foo")).tap do |definition|
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:foo].method_types
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:bar].method_types
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:Bar].method_types

assert_equal Types::Variable.build(:A),
definition.instance_variables[:@bar].type
assert_equal Types::Variable.build(:A),
definition.instance_variables[:@Bar].type
end
end
end
end

def test_generic_class_interface
SignatureManager.new do |manager|
manager.files.merge!(Pathname("foo.rbs") => <<-EOF)
class Foo[A]
def foo: () -> A
end

interface _Baz[Y]
def baz: () -> Y
end

class Foo[C]
include _Baz[C]
end
EOF
manager.build do |env|
builder = DefinitionBuilder.new(env: env)

builder.build_instance(type_name("::Foo")).tap do |definition|
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:foo].method_types
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:baz].method_types
end
end
end
end

def test_generic_class_module
SignatureManager.new do |manager|
manager.files.merge!(Pathname("foo.rbs") => <<-EOF)
class Foo[A]
def foo: () -> A
end

class Foo[B]
include Bar[B]
end

module Bar[Y]
def bar: () -> Y

@bar: Y
end
EOF
manager.build do |env|
builder = DefinitionBuilder.new(env: env)

builder.build_instance(type_name("::Foo")).tap do |definition|
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:foo].method_types
assert_equal [parse_method_type("() -> A", variables: [:A])],
definition.methods[:bar].method_types

assert_equal Types::Variable.build(:A),
definition.instance_variables[:@bar].type
end
end
end
end
end