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

Compiler: correctly pass named arguments for previous_def and super #9834

Merged
merged 1 commit into from
Oct 23, 2020

Conversation

asterite
Copy link
Member

Fixes https://forum.crystal-lang.org/t/i-may-have-found-a-bug-with-previous-def-but-not-sure/2624/2

The way the compiler handles super and previous_def without parentheses is by copying the current method's arguments to the call. So for example:

def foo(x, y)
  previous_def
end

gets expanded to:

def foo(x, y)
  previous_def(x, y)
end

The logic also took into account splats. This:

def foo(x, *y)
  previous_def
end

was expanded to:

def foo(x, *y)
  previous_def(x, *y)
end

However, arguments past the splat were passed as regular arguments. So this:

def foo(x, *y, z)
  previous_def
end

was incorrectly expanded to:

def foo(x, *y, z)
  previous_def(x, *y, z)
end

which wouldn't compile, unless z had a default value (it does in the bug report).

This PR changes the above expansion to be:

def foo(x, *y, z)
  previous_def(x, *y, z: z)
end

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels Oct 17, 2020
@jgaskins
Copy link
Contributor

However, arguments past the splat were passed as regular arguments. So this:

def foo(x, *y, z)
  previous_def
end

was incorrectly expanded to:

def foo(x, *y, z)
  previous_def(x, *y, z)
end

which wouldn't compile, unless z had a default value (it does in the bug report).

I'm learning a lot about splat args from this PR. Thank you so much for your effort! 💯

@bcardiff bcardiff added this to the 1.0.0 milestone Oct 23, 2020
@asterite asterite merged commit 84aab66 into master Oct 23, 2020
@asterite asterite deleted the bug/previous_def_named_args branch October 23, 2020 14:57
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.

3 participants