Skip to content

Commit

Permalink
Make sure we use compare by identity Sets properly
Browse files Browse the repository at this point in the history
There have been a few instances where we have not been using `Set`s with `compare_by_identity` properly. This commit fixes those instances so that the test added in the previous commit passes.

Fixes #1728
  • Loading branch information
paracycle committed Dec 18, 2023
1 parent f7970d8 commit c1333df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def gather_constants; end
sig { returns(T::Set[Module]) }
def processable_constants
@processable_constants ||= T.let(
T::Set[Module].new(gather_constants).compare_by_identity,
T::Set[Module].new.compare_by_identity.merge(gather_constants),
T.nilable(T::Set[Module]),
)
end
Expand Down
10 changes: 7 additions & 3 deletions lib/tapioca/dsl/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def gather_active_compilers(requested_compilers, excluded_compilers)

sig { params(requested_constants: T::Array[Module], requested_paths: T::Array[Pathname]).returns(T::Set[Module]) }
def gather_constants(requested_constants, requested_paths)
constants = active_compilers.map(&:processable_constants).reduce(Set.new, :union)
constants = Set.new.compare_by_identity
active_compilers.each do |compiler|
constants.merge(compiler.processable_constants)
end
constants = filter_anonymous_and_reloaded_constants(constants)

constants &= requested_constants unless requested_constants.empty? && requested_paths.empty?
Expand Down Expand Up @@ -158,11 +161,12 @@ def filter_anonymous_and_reloaded_constants(constants)

# Look up all the constants back from their names. The resulting constant set will be the
# set of constants that are actually in memory with those names.
constants_by_name
filtered_constants = constants_by_name
.keys
.map { |name| T.cast(Runtime::Reflection.constantize(name), Module) }
.select { |mod| Runtime::Reflection.constant_defined?(mod) }
.to_set

Set.new.compare_by_identity.merge(filtered_constants)
end

sig { params(constant: Module).returns(T.nilable(RBI::File)) }
Expand Down

0 comments on commit c1333df

Please sign in to comment.