From 252daa3b778d7e53ec92557bf4556d4ad137362c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 7 Oct 2024 18:37:28 +0200 Subject: [PATCH] Remove `AliasType#types?` and guardrails for it --- src/compiler/crystal/codegen/link.cr | 2 -- .../crystal/semantic/abstract_def_checker.cr | 5 ++++- src/compiler/crystal/semantic/new.cr | 2 -- .../crystal/semantic/recursive_struct_checker.cr | 5 ++++- .../crystal/semantic/type_declaration_processor.cr | 5 ++++- src/compiler/crystal/tools/doc/generator.cr | 7 ------- src/compiler/crystal/tools/typed_def_processor.cr | 9 --------- src/compiler/crystal/tools/unreachable.cr | 9 --------- src/compiler/crystal/types.cr | 13 ------------- 9 files changed, 12 insertions(+), 45 deletions(-) diff --git a/src/compiler/crystal/codegen/link.cr b/src/compiler/crystal/codegen/link.cr index 3601aa0fd870..593468484664 100644 --- a/src/compiler/crystal/codegen/link.cr +++ b/src/compiler/crystal/codegen/link.cr @@ -292,8 +292,6 @@ module Crystal private def add_link_annotations(types, annotations) types.try &.each_value do |type| - next if type.is_a?(AliasType) || type.is_a?(TypeDefType) - if type.is_a?(LibType) && type.used? && (link_annotations = type.link_annotations) annotations.concat link_annotations end diff --git a/src/compiler/crystal/semantic/abstract_def_checker.cr b/src/compiler/crystal/semantic/abstract_def_checker.cr index 2a7ccdc05d2a..27f310d6efc3 100644 --- a/src/compiler/crystal/semantic/abstract_def_checker.cr +++ b/src/compiler/crystal/semantic/abstract_def_checker.cr @@ -41,7 +41,10 @@ class Crystal::AbstractDefChecker end def check_single(type) - return if @all_checked.includes?(type) + if @all_checked.includes?(type) + ::raise "AbstractDefChecker duplicate: #{type} (#{type.class})" + return + end @all_checked << type if type.abstract? || type.module? diff --git a/src/compiler/crystal/semantic/new.cr b/src/compiler/crystal/semantic/new.cr index de8ae55312a0..43a0a631e2c6 100644 --- a/src/compiler/crystal/semantic/new.cr +++ b/src/compiler/crystal/semantic/new.cr @@ -22,8 +22,6 @@ module Crystal end def define_default_new(type) - return if type.is_a?(AliasType) || type.is_a?(TypeDefType) - type.types?.try &.each_value do |type| define_default_new_single(type) end diff --git a/src/compiler/crystal/semantic/recursive_struct_checker.cr b/src/compiler/crystal/semantic/recursive_struct_checker.cr index e7f64913789f..5c2661b6b0cf 100644 --- a/src/compiler/crystal/semantic/recursive_struct_checker.cr +++ b/src/compiler/crystal/semantic/recursive_struct_checker.cr @@ -35,7 +35,10 @@ class Crystal::RecursiveStructChecker def check_single(type) has_not_been_checked = @all_checked.add?(type) - return unless has_not_been_checked + unless has_not_been_checked + raise "RecursiveStructChecker duplicate: #{type} (#{type.class})" + return + end if struct?(type) target = type diff --git a/src/compiler/crystal/semantic/type_declaration_processor.cr b/src/compiler/crystal/semantic/type_declaration_processor.cr index 65451741fac3..a16f569321fa 100644 --- a/src/compiler/crystal/semantic/type_declaration_processor.cr +++ b/src/compiler/crystal/semantic/type_declaration_processor.cr @@ -627,7 +627,10 @@ struct Crystal::TypeDeclarationProcessor end private def remove_duplicate_instance_vars_declarations(type : Type, duplicates_checked : Set(Type)) - return unless duplicates_checked.add?(type) + unless duplicates_checked.add?(type) + raise "remove_duplicate_instance_vars_declarations duplicate: #{type} (#{type.class})" + return + end # If a class has an instance variable that already exists in a superclass, remove it. # Ideally we should process instance variables in a top-down fashion, but it's tricky diff --git a/src/compiler/crystal/tools/doc/generator.cr b/src/compiler/crystal/tools/doc/generator.cr index a2f4db47dee0..4c5988cccae5 100644 --- a/src/compiler/crystal/tools/doc/generator.cr +++ b/src/compiler/crystal/tools/doc/generator.cr @@ -251,13 +251,6 @@ class Crystal::Doc::Generator def collect_subtypes(parent) types = [] of Type - # AliasType has defined `types?` to be the types - # of the aliased type, but for docs we don't want - # to list the nested types for aliases. - if parent.is_a?(AliasType) - return types - end - parent.types?.try &.each_value do |type| case type when Const, LibType diff --git a/src/compiler/crystal/tools/typed_def_processor.cr b/src/compiler/crystal/tools/typed_def_processor.cr index a0a911a6a618..2ba2441d7902 100644 --- a/src/compiler/crystal/tools/typed_def_processor.cr +++ b/src/compiler/crystal/tools/typed_def_processor.cr @@ -17,15 +17,6 @@ module Crystal::TypedDefProcessor end private def process_type(type : Type) : Nil - # Avoid visiting circular hierarchies. There's no use in processing - # alias types anyway. - # For example: - # - # struct Foo - # alias Bar = Foo - # end - return if type.is_a?(AliasType) || type.is_a?(TypeDefType) - if type.is_a?(NamedType) || type.is_a?(Program) || type.is_a?(FileModule) type.types?.try &.each_value do |inner_type| process_type inner_type diff --git a/src/compiler/crystal/tools/unreachable.cr b/src/compiler/crystal/tools/unreachable.cr index 733a94518899..a8886fecf596 100644 --- a/src/compiler/crystal/tools/unreachable.cr +++ b/src/compiler/crystal/tools/unreachable.cr @@ -128,15 +128,6 @@ module Crystal property excludes = [] of String def process_type(type) - # Avoid visiting circular hierarchies. There's no use in processing - # alias types anyway. - # For example: - # - # struct Foo - # alias Bar = Foo - # end - return if type.is_a?(AliasType) || type.is_a?(TypeDefType) - if type.is_a?(ModuleType) track_unused_defs type end diff --git a/src/compiler/crystal/types.cr b/src/compiler/crystal/types.cr index be52adfd0a2b..2655ea1cdc16 100644 --- a/src/compiler/crystal/types.cr +++ b/src/compiler/crystal/types.cr @@ -2760,19 +2760,6 @@ module Crystal delegate lookup_defs, lookup_defs_with_modules, lookup_first_def, lookup_macro, lookup_macros, to: aliased_type - def types? - process_value - if aliased_type = @aliased_type - aliased_type.types? - else - nil - end - end - - def types - types?.not_nil! - end - def lookup_type(name) process_value @aliased_type.try(&.lookup_type(name))