lib/rbs/environment.rb in rbs-1.8.1 vs lib/rbs/environment.rb in rbs-2.0.0.pre1

- old
+ new

@@ -37,26 +37,33 @@ @primary = nil end def validate_type_params unless decls.empty? + # @type var hd_decl: MultiEntry::D[module_decl] + # @type var tl_decls: Array[MultiEntry::D[module_decl]] hd_decl, *tl_decls = decls raise unless hd_decl hd_params = hd_decl.decl.type_params - hd_names = hd_params.params.map(&:name) tl_decls.each do |tl_decl| tl_params = tl_decl.decl.type_params - unless hd_params.size == tl_params.size && hd_params == tl_params.rename_to(hd_names) + unless compatible_params?(hd_params, tl_params) raise GenericParameterMismatchError.new(name: name, decl: tl_decl.decl) end end end end + def compatible_params?(ps1, ps2) + if ps1.size == ps2.size + ps1 == AST::TypeParam.rename(ps2, new_names: ps1.map(&:name)) + end + end + def type_params primary.decl.type_params end def primary @@ -238,21 +245,23 @@ context = (outer + [decl]).each.with_object([Namespace.root]) do |decl, array| head = array.first or raise array.unshift(head + decl.name.to_namespace) end + outer_context = context.drop(1) + case decl when AST::Declarations::Class outer_ = outer + [decl] prefix_ = prefix + decl.name.to_namespace AST::Declarations::Class.new( name: decl.name.with_prefix(prefix), - type_params: decl.type_params, + type_params: resolve_type_params(resolver, decl.type_params, context: context), super_class: decl.super_class&.yield_self do |super_class| AST::Declarations::Class::Super.new( - name: absolute_type_name(resolver, super_class.name, context: context), - args: super_class.args.map {|type| absolute_type(resolver, type, context: context) }, + name: absolute_type_name(resolver, super_class.name, context: outer_context), + args: super_class.args.map {|type| absolute_type(resolver, type, context: outer_context) }, location: super_class.location ) end, members: decl.members.map do |member| case member @@ -276,11 +285,11 @@ when AST::Declarations::Module outer_ = outer + [decl] prefix_ = prefix + decl.name.to_namespace AST::Declarations::Module.new( name: decl.name.with_prefix(prefix), - type_params: decl.type_params, + type_params: resolve_type_params(resolver, decl.type_params, context: context), self_types: decl.self_types.map do |module_self| AST::Declarations::Module::Self.new( name: absolute_type_name(resolver, module_self.name, context: context), args: module_self.args.map {|type| absolute_type(resolver, type, context: context) }, location: module_self.location @@ -306,22 +315,22 @@ comment: decl.comment ) when AST::Declarations::Interface AST::Declarations::Interface.new( name: decl.name.with_prefix(prefix), - type_params: decl.type_params, + type_params: resolve_type_params(resolver, decl.type_params, context: context), members: decl.members.map do |member| resolve_member(resolver, member, context: context) end, comment: decl.comment, location: decl.location, annotations: decl.annotations ) when AST::Declarations::Alias AST::Declarations::Alias.new( name: decl.name.with_prefix(prefix), - type_params: decl.type_params, + type_params: resolve_type_params(resolver, decl.type_params, context: context), type: absolute_type(resolver, decl.type, context: context), location: decl.location, annotations: decl.annotations, comment: decl.comment ) @@ -341,11 +350,11 @@ when AST::Members::MethodDefinition AST::Members::MethodDefinition.new( name: member.name, kind: member.kind, types: member.types.map do |type| - type.map_type {|ty| absolute_type(resolver, ty, context: context) } + resolve_method_type(resolver, type, context: context) end, comment: member.comment, overload: member.overload?, annotations: member.annotations, location: member.location @@ -425,9 +434,23 @@ location: member.location, annotations: member.annotations ) else member + end + end + + def resolve_method_type(resolver, type, context:) + type.map_type do |ty| + absolute_type(resolver, ty, context: context) + end.map_type_bound do |bound| + _ = absolute_type(resolver, bound, context: context) + end + end + + def resolve_type_params(resolver, params, context:) + params.map do |param| + param.map_type {|type| _ = absolute_type(resolver, type, context: context) } end end def absolute_type_name(resolver, type_name, context:) resolver.resolve(type_name, context: context) || type_name