Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[formatter] Index out of bounds (IndexError) When run formatter #15262

Open
zw963 opened this issue Dec 10, 2024 · 0 comments
Open

[formatter] Index out of bounds (IndexError) When run formatter #15262

zw963 opened this issue Dec 10, 2024 · 0 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:tools:formatter

Comments

@zw963
Copy link
Contributor

zw963 commented Dec 10, 2024

Bug Report

Probably related to #15242

When try to format following code:

module Procodile
  class Supervisor
    def check_concurrency(
      options : Supervisor::Options = Supervisor::Options.new
    ) : Hash(Symbol, Array(Instance))
      if result[:started].present?
        Procodile.log nil, "system", "Concurrency check started \
#{result[:started].map(&.description).join(", ")}"
      end
    end

    def to_hash : NamedTuple(started_at: Int64?, pid: Int64)
      started_at = @started_at

      {
        started_at: started_at ? started_at.to_unix : nil,
        pid:        ::Process.pid,
      }
    end
  end
end

get error like following:

 ╰──➤ $ 1  crystal tool format --show-backtrace test.cr
Index out of bounds (IndexError)
  from /crystal/src/indexable.cr:977:29 in '[]'
  from /crystal/src/compiler/crystal/tools/formatter.cr:5042:15 in 'align_info'
  from /crystal/src/compiler/crystal/tools/formatter.cr:4999:11 in 'align_infos'
  from /crystal/src/compiler/crystal/tools/formatter.cr:4948:7 in 'finish'
  from /crystal/src/compiler/crystal/tools/formatter.cr:23:7 in 'format_source'
  from /crystal/src/compiler/crystal/command/format.cr:133:7 in 'format_many'
  from /crystal/src/compiler/crystal/command/format.cr:63:5 in 'format'
  from /crystal/src/reference.cr:75:3 in 'tool'
  from /crystal/src/compiler/crystal/command.cr:118:7 in 'run'
  from /crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
  from /crystal/src/crystal/main.cr:118:5 in 'main'
  from src/env/__libc_start_main.c:95:2 in 'libc_start_main_stage2'

The issue caused by

        Procodile.log nil, "system", "Concurrency check started \
#{result[:started].map(&.description).join(", ")}"

If change to following one-line version, issue is not happen.

        Procodile.log nil, "system", "Concurrency check started #{result[:started].map(&.description).join(", ")}"

But, if remove the to_hash method, this issue not happen too.

# no error

module Procodile
  class Supervisor
    def check_concurrency(
      options : Supervisor::Options = Supervisor::Options.new
    ) : Hash(Symbol, Array(Instance))
      if result[:started].present?
        Procodile.log nil, "system", "Concurrency check started \
#{result[:started].map(&.description).join(", ")}"
      end
    end
  end
end
@zw963 zw963 added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:tools:formatter
Projects
None yet
Development

No branches or pull requests

2 participants