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

Fix ruby 3.0 keyword arguments in translator #128

Closed
wants to merge 1 commit into from

Conversation

tconst
Copy link

@tconst tconst commented Jul 19, 2022

Removes ruby2_keywords added in #126 and clears up the related console warnings in AST::Translator. This may not work in ruby < 2.7, untested at time of PR open.

@dentarg
Copy link
Member

dentarg commented Jul 19, 2022

Pretty sure we need mustermann to work in Ruby < 2.7

@tconst
Copy link
Author

tconst commented Jul 19, 2022

Passing locally in 2.6.10 through 3.1.2.

@dentarg
Copy link
Member

dentarg commented Jul 19, 2022

This PR seems to lower the test coverage from 100% to 99.71%, see https://github.com/dentarg/mustermann/runs/7406755584?check_suite_focus=true#step:4:28

@tconst
Copy link
Author

tconst commented Jul 19, 2022

@dentarg without merging the coverage files, not sure the best way to cover the differing branches. I added :nocov: to the ruby < 3 branch for now.

@dentarg
Copy link
Member

dentarg commented Jul 19, 2022

True

@dentarg
Copy link
Member

dentarg commented Jul 19, 2022

The change here get rids of the warnings (warning: Skipping set of ruby2_keywords flag for translate (method accepts keywords or method does not accept argument splat)

Is this change correct though? @eregon @jeremyevans do you care to weigh in?

@tconst
Copy link
Author

tconst commented Jul 19, 2022

Another way to fix (the warnings) would be something like this

      def self.translate(*types, &block)
        Class.new(const_get(:NodeTranslator)) do
          register(*types)
          define_method(:translate, &block)

          if block.parameters.flatten.any?(:args)
            ruby2_keywords :translate
          end
        end
      end

but I think this is not really a good solution as it is tied directly to the arg naming coming in with the block.

@eregon
Copy link
Contributor

eregon commented Jul 19, 2022

This PR/change is incorrect at least on 2.7: https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html

What are the warnings, for which ruby2_keywords call?

@@ -73,7 +82,6 @@ def self.translate(*types, &block)
Class.new(const_get(:NodeTranslator)) do
register(*types)
define_method(:translate, &block)
ruby2_keywords :translate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is this one is producing the warning, and it should probably be define_method(:translate, &block.ruby2_keywords) instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still seeing the warnings with define_method(:translate, &block.ruby2_keywords)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you post the full warning output (it should show where it happens), and how to reproduce it?

Copy link
Author

@tconst tconst Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.../mustermann/mustermann/lib/mustermann/ast/translator.rb:75: warning: Skipping set of ruby2_keywords flag for proc (proc accepts keywords or proc does not accept argument splat)

You can see the full gamut in the test suite, as the class loading dumps a bunch of these.

translate Array do |*args, **kwargs|
inject(t.pattern) do |pattern, element|
t.add_to(pattern, t(element, *args, **kwargs))
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possibility would be to call Proc#ruby2_keywords on this block which delegates *args (and other blocks doing the same).
That seems the cleanest solution.

@eregon
Copy link
Contributor

eregon commented Jul 19, 2022

I created #130 which I believe is the proper fix.
I don't know the details of this codebase though, but ruby2_keywords should only be applied to methods delegating *args.
From a quick look at callers of translate only that one seems to delegate.

@tconst tconst closed this Jul 19, 2022
@jkowens
Copy link
Member

jkowens commented Jul 19, 2022

@tconst I appreciate you helping work through this issue!

@torrocus
Copy link

@jkowens When can we expect patch on rubygems? Thanks in advance.

@jkowens
Copy link
Member

jkowens commented Jul 19, 2022

Released v2.0.1

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

Successfully merging this pull request may close these issues.

5 participants