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

next and break in macro blocks #13859

Open
HertzDevil opened this issue Oct 3, 2023 · 0 comments · May be fixed by #14894
Open

next and break in macro blocks #13859

HertzDevil opened this issue Oct 3, 2023 · 0 comments · May be fixed by #14894

Comments

@HertzDevil
Copy link
Contributor

HertzDevil commented Oct 3, 2023

Inside a macro expresion, next and break cannot be used inside while and until, simply because these loops also aren't supported yet (see #10959). But there are also next and break inside blocks:

{%
  (0..10).each do |i|
    p i
    break if i >= 5 # Error: can't execute Break in a macro
  end
%}

It would allow us to rewrite this:

{% ssl_version = nil %}
{% for dir in Crystal::LIBRARY_PATH.split(';') %}
{% unless ssl_version %}
{% config_path = "#{dir.id}\\openssl_VERSION" %}
{% if config_version = read_file?(config_path) %}
{% ssl_version = config_version.chomp %}
{% end %}
{% end %}
{% end %}

into one of the following:

{%
  # normal refactor
  ssl_version = nil
  Crystal::LIBRARY_PATH.split(';').each do |dir|
    config_path = "#{dir.id}\\openssl_VERSION"
    if config_version = read_file?(config_path)
      ssl_version = config_version.chomp
      break
    end
  end

  # `break` with value
  ssl_version = Crystal::LIBRARY_PATH.split(';').each do |dir|
    config_path = "#{dir.id}\\openssl_VERSION"
    if config_version = read_file?(config_path)
      break config_version.chomp
    end
  end
%}

It is always unambiguous whether a given next or break is associated with a block or a while, and these keywords inside blocks cannot introduce any more macro infinite loops than we already have right now, unlike #10959. There is also the macro for, which one might argue should also support next and break, but for isn't impacted since it cannot appear inside a macro block. So I believe there are no blockers here and we could implement those keywords straight away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant