lib/rbs/definition_builder.rb in rbs-0.5.0 vs lib/rbs/definition_builder.rb in rbs-0.6.0

- old
+ new

@@ -46,11 +46,11 @@ end def instance_ancestors(type_name, building_ancestors: []) as = instance_ancestors_cache[type_name] and return as - entry = env.class_decls[type_name] + entry = env.class_decls[type_name] or raise "Unknown name for instance_ancestors: #{type_name}" params = entry.type_params.each.map(&:name) args = Types::Variable.build(params) self_ancestor = Definition::Ancestor::Instance.new(name: type_name, args: args) RecursiveAncestorError.check!(self_ancestor, @@ -75,10 +75,12 @@ else super_name = BuiltinNames::Object.name super_args = [] end + NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location) + super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors) ancestors.unshift(*super_ancestors.apply(super_args, location: primary.decl.location)) end build_ancestors_mixin_self(self_ancestor, entry, ancestors: ancestors, building_ancestors: building_ancestors) @@ -98,11 +100,11 @@ end def singleton_ancestors(type_name, building_ancestors: []) as = singleton_ancestor_cache[type_name] and return as - entry = env.class_decls[type_name] + entry = env.class_decls[type_name] or raise "Unknown name for singleton_ancestors: #{type_name}" self_ancestor = Definition::Ancestor::Singleton.new(name: type_name) RecursiveAncestorError.check!(self_ancestor, ancestors: building_ancestors, location: entry.primary.decl.location) @@ -121,10 +123,12 @@ super_name = super_class.name else super_name = BuiltinNames::Object.name end + NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location) + super_ancestors = singleton_ancestors(super_name, building_ancestors: building_ancestors) ancestors.unshift(*super_ancestors.ancestors) else as = instance_ancestors(BuiltinNames::Class.name, building_ancestors: building_ancestors) ancestors.unshift(*as.apply([], location: entry.primary.decl.location)) @@ -141,10 +145,12 @@ decl.each_mixin do |member| case member when AST::Members::Extend if member.name.class? + NoMixinFoundError.check!(member.name, env: env, member: member) + module_ancestors = instance_ancestors(member.name, building_ancestors: building_ancestors) ancestors.unshift(*module_ancestors.apply(member.args, location: member.location)) end end end @@ -172,10 +178,12 @@ decl.each_mixin do |member| case member when AST::Members::Include if member.name.class? + NoMixinFoundError.check!(member.name, env: env, member: member) + module_name = member.name module_args = member.args.map {|type| type.sub(align_params) } module_ancestors = instance_ancestors(module_name, building_ancestors: building_ancestors) ancestors.unshift(*module_ancestors.apply(module_args, location: member.location)) @@ -195,10 +203,12 @@ Types::Variable.build(entry.type_params.each.map(&:name))) decl.each_mixin do |member| case member when AST::Members::Prepend + NoMixinFoundError.check!(member.name, env: env, member: member) + module_name = member.name module_args = member.args.map {|type| type.sub(align_params) } module_ancestors = instance_ancestors(module_name, building_ancestors: building_ancestors) ancestors.unshift(*module_ancestors.apply(module_args)) @@ -220,11 +230,11 @@ end end def build_instance(type_name) try_cache type_name, cache: instance_cache do - entry = env.class_decls[type_name] + entry = env.class_decls[type_name] or raise "Unknown name for build_instance: #{type_name}" case entry when Environment::ClassEntry, Environment::ModuleEntry ancestors = instance_ancestors(type_name) self_type = Types::ClassInstance.new(name: type_name, @@ -241,15 +251,22 @@ raise end end if entry.is_a?(Environment::ModuleEntry) - if self_type_ancestor = module_self_ancestor(type_name, entry) - definition_pairs.push [ - self_type_ancestor, - build_interface(self_type_ancestor.name) - ] + entry.self_types.each do |module_self| + ancestor = Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args) + definition_pairs.push( + [ + ancestor, + if module_self.name.interface? + build_interface(module_self.name) + else + build_instance(module_self.name) + end + ] + ) end end merge_definitions(type_name, definition_pairs, entry: entry, self_type: self_type, ancestors: ancestors) @@ -257,36 +274,13 @@ raise end end end - def module_self_ancestor(type_name, entry) - self_decls = entry.decls.select {|d| d.decl.self_type } - - return if self_decls.empty? - - selfs = self_decls.map do |d| - Definition::Ancestor::Instance.new( - name: d.decl.self_type.name, - args: d.decl.self_type.args - ) - end - - selfs.uniq! - - return selfs[0] if selfs.size == 1 - - raise ModuleSelfTypeMismatchError.new( - name: type_name, - entry: entry, - location: self_decls[0].decl.location - ) - end - def build_singleton(type_name) try_cache type_name, cache: singleton_cache do - entry = env.class_decls[type_name] + entry = env.class_decls[type_name] or raise "Unknown name for build_singleton: #{type_name}" case entry when Environment::ClassEntry, Environment::ModuleEntry ancestors = singleton_ancestors(type_name) self_type = Types::ClassSingleton.new(name: type_name, location: nil) @@ -348,10 +342,12 @@ accessibility ] when AST::Members::Include, AST::Members::Extend if member.name.interface? if (kind == :instance && member.is_a?(AST::Members::Include)) || (kind == :singleton && member.is_a?(AST::Members::Extend)) + NoMixinFoundError.check!(member.name, env: env, member: member) + interface_name = member.name interface_args = member.args interface_definition = build_interface(interface_name) @@ -865,11 +861,11 @@ end end def build_interface(type_name) try_cache(type_name, cache: interface_cache) do - entry = env.interface_decls[type_name] + entry = env.interface_decls[type_name] or raise "Unknown name for build_interface: #{type_name}" declaration = entry.decl self_type = Types::Interface.new( name: type_name, args: Types::Variable.build(declaration.type_params.each.map(&:name)), @@ -891,10 +887,12 @@ alias_members << member end end include_members.each do |member| + NoMixinFoundError.check!(member.name, env: env, member: member) + mixin = build_interface(member.name) args = member.args type_params = mixin.entry.decl.type_params @@ -955,9 +953,10 @@ end end end def expand_alias(type_name) - env.alias_decls[type_name].decl.type + entry = env.alias_decls[type_name] or raise "Unknown name for expand_alias: #{type_name}" + entry.decl.type end end end