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

Skip abstract def warnings after first def with matching parameter names #12167

Merged

Conversation

HertzDevil
Copy link
Contributor

Fixes #12150.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels Jun 29, 2022
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

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

This is great, thank you! I have just one nitpick suggestion, I'll let you @HertzDevil decide if you prefer to apply it or not

Comment on lines +290 to +291
named_args1 = nil
named_args2 = nil
Copy link
Member

Choose a reason for hiding this comment

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

A nitpick suggestion (see also below)

Suggested change
named_args1 = nil
named_args2 = nil
named_args1 = [] of String
named_args2 = [] of String

Copy link
Member

Choose a reason for hiding this comment

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

It's a performance optimization to avoid the (double) array allocation for the (more likely) case that there are no named args. So I think the current code is probably better (although perhaps not as elegant).

Copy link
Member

@straight-shoota straight-shoota Jul 1, 2022

Choose a reason for hiding this comment

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

However, we can know beforehand if there could be an allocation for named args (only if args.size > splat_index).

And I suppose the loop could be refactored and split into different stages:

(0...(splat_index || m1.args.size)).each do |i|
  arg1 = m1.args[i]
  arg2 = m2.args[i]
  return false unless arg1.external_name == arg2.external_name
end

if splat_index
  return false unless (m1.args[splat_index].external_name != "") == (m2.args[splat_index].external_name != "")

  named_args1 = m1.args[splat_index..].sort!
  named_args2 = m2.args[splat_index..].sort!

  return false unless named_args1 == named_args2
end

m1.double_splat.nil? == m2.double_splat.nil?

I think this should be semantically equivalent (more or less, haven't tested it). And it's definitely more concise. Not sure about performance characteristics, but I doubt it makes a huge difference.

Comment on lines +299 to +300
(named_args1 ||= [] of String) << arg1.external_name
(named_args2 ||= [] of String) << arg2.external_name
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(named_args1 ||= [] of String) << arg1.external_name
(named_args2 ||= [] of String) << arg2.external_name
named_args1 << arg1.external_name
named_args2 << arg2.external_name

return false unless arg1.external_name == arg2.external_name
end

if named_args1 && named_args2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if named_args1 && named_args2
unless named_args1.empty? # named_args1.empty? iff named_args2.empty?

@straight-shoota straight-shoota added this to the 1.5.0 milestone Jul 1, 2022
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:compiler:semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive positional parameter mismatch warning
3 participants