Skip to content

Commit

Permalink
Use type instead of is_a? in filters (#10815)
Browse files Browse the repository at this point in the history
* Use type instead of `is_a?` in filters

* Revert union types in filters
  • Loading branch information
caspiano committed Jun 21, 2021
1 parent 860e9c9 commit 71d6e9b
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/cond.cr
Expand Up @@ -31,7 +31,7 @@ class Crystal::CodeGenVisitor

has_nil = union_types.any? &.nil_type?
has_bool = union_types.any? &.bool_type?
has_pointer = union_types.any? &.is_a?(PointerInstanceType)
has_pointer = union_types.any?(PointerInstanceType)

cond = llvm_true

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/llvm_id.cr
Expand Up @@ -164,7 +164,7 @@ module Crystal
end

private def subclasses_of(type)
type.subclasses.reject(&.is_a?(GenericInstanceType))
type.subclasses.reject(GenericInstanceType)
end
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/types.cr
Expand Up @@ -150,7 +150,7 @@ module Crystal

class UnionType
def expand_union_types
if union_types.any?(&.is_a?(NonGenericModuleType))
if union_types.any?(NonGenericModuleType)
types = [] of Type
union_types.each &.append_to_expand_union_types(types)
types
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/cleanup_transformer.cr
Expand Up @@ -211,7 +211,7 @@ module Crystal

def has_enum_type?(type)
if type.is_a?(UnionType)
type.union_types.any? &.is_a?(EnumType)
type.union_types.any?(EnumType)
else
type.is_a?(EnumType)
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/exhaustiveness_checker.cr
Expand Up @@ -51,7 +51,7 @@ struct Crystal::ExhaustivenessChecker
# If we covered all types, we are done.
return if targets.empty?

if targets.all?(&.is_a?(TypeTarget)) && all_patterns_are_types
if targets.all?(TypeTarget) && all_patterns_are_types
node.raise <<-MSG
case is not exhaustive.
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/new.cr
Expand Up @@ -71,7 +71,7 @@ module Crystal
has_self_initialize_methods = !self_initialize_methods.empty?
if !has_self_initialize_methods
is_generic = type.is_a?(GenericClassType)
inherits_from_generic = type.ancestors.any?(&.is_a?(GenericClassInstanceType))
inherits_from_generic = type.ancestors.any?(GenericClassInstanceType)
if is_generic || inherits_from_generic
has_default_self_new = self_new_methods.any? do |a_def|
a_def.args.empty? && !a_def.yields
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/type_lookup.cr
Expand Up @@ -280,7 +280,7 @@ class Crystal::Type
end

begin
if instance_type.is_a?(GenericUnionType) && type_vars.any? &.is_a?(TypeSplat)
if instance_type.is_a?(GenericUnionType) && type_vars.any?(TypeSplat)
# In the case of `Union(*T)`, we don't need to instantiate the union right
# now because it will just return `*T`, but what we want to expand the
# union types only when the type is instantiated.
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/type.cr
Expand Up @@ -577,7 +577,7 @@ class Crystal::Doc::Type
end

def type_to_html(type : Crystal::UnionType, io, text = nil, html : HTMLOption = :all)
has_type_splat = type.union_types.any? &.is_a?(TypeSplat)
has_type_splat = type.union_types.any?(TypeSplat)

if !has_type_splat && type.union_types.size == 2
if type.union_types[0].nil_type?
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/print_hierarchy.cr
Expand Up @@ -58,7 +58,7 @@ module Crystal

compute_targets type.types, exp, false

subtypes = type.subclasses.select { |sub| !sub.is_a?(GenericClassInstanceType) }
subtypes = type.subclasses.reject(GenericClassInstanceType)
must_include |= compute_targets subtypes, exp, must_include
if must_include
@targets << type
Expand All @@ -75,7 +75,7 @@ module Crystal
compute_targets type.types, exp, false
compute_targets type.instantiated_types, exp, must_include

subtypes = type.subclasses.select { |sub| !sub.is_a?(GenericClassInstanceType) }
subtypes = type.subclasses.reject(GenericClassInstanceType)
must_include |= compute_targets subtypes, exp, must_include
if must_include
@targets << type
Expand Down
4 changes: 2 additions & 2 deletions src/indexable.cr
Expand Up @@ -333,7 +333,7 @@ module Indexable(T)
{% if T == String %}
join_strings(separator)
{% elsif String < T %}
if all?(&.is_a?(String))
if all?(String)
join_strings(separator)
else
super(separator)
Expand All @@ -358,7 +358,7 @@ module Indexable(T)

each_with_index do |elem, i|
# elem is guaranteed to be a String, but the compiler doesn't know this
# if we enter via the all?(&.is_a?(String)) branch.
# if we enter via the all?(String) branch.
elem = elem.to_s

# Copy separator to buffer
Expand Down

0 comments on commit 71d6e9b

Please sign in to comment.