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

GIve syntax error when using a local variable inside an assignment to… #2793

Merged
merged 1 commit into from
Jul 5, 2016

Conversation

asterite
Copy link
Member

… that same local variable

Fixes #2142

I'm sending this as a PR because I'm not sure about this change.

Basically, this now gives a syntax error:

def a
  20
end

a = a + 1

Previously that compiled fine: the a inside the assignment refers to the a method. If no such method exists you would get an error saying "undefined local variable or method 'a'". Now it always gives an error.

For this particular case, one can do:

a = ::a + 1

Or

a = a() + 1

If inside a method and we want to refer to an instance method, we must use self.name now, as you can see in the diff in several places.

The "downside" is that this also disallows declaring a variable with the same name inside an outer declaration:

var = if some_condition
        var = 1
        do_something_else
        var
      end

Nor this:

var = if (var = some_expression)
        10
      end

But maybe this is good? It basically makes it impossible to write code that shadows a variable inside an assignment to that variable, which can be very confusing. And when wanting to call a method with that same it forces one to use self., making it more explicit and obvious.

Of course this is a breaking change.

@jhass
Copy link
Member

jhass commented Jun 10, 2016

Yes I think this is a good change. One alternative I see is initializing the variable with nil, Ruby does this in some situations since locals are defined and set to nil in the parser already. However an error is probably better since it sometimes leads to similarly confusing behavior as parsing it as a method call does.

@miketheman
Copy link
Contributor

#triage
@jhass agrees this is a good change, and there are 5 other 👍 in support.
@asterite - anything holding it up?

@asterite
Copy link
Member Author

It's a breaking change, so we can probably include this in 0.19.0

@asterite asterite added this to the 0.19.0 milestone Jun 20, 2016
@refi64
Copy link
Contributor

refi64 commented Jun 20, 2016

Would this also error on assignments, or just declarations?

@asterite
Copy link
Member Author

@kirbyfan64 It currently only errors if the variable didn't exist, but I think it would be a good idea to disallow assigning a variable inside its own assignment it in all cases. Good catch!

@asterite asterite force-pushed the feature/local_var_assign_shadow branch from 3bf04ee to b841f90 Compare June 21, 2016 12:24
@refi64
Copy link
Contributor

refi64 commented Jun 21, 2016

@asterite Uhhh...well I was saying because I prefer if its only declarations...

I tend to do stuff like this in my code:

a = my_fun a
a = -a
a = something {|| a }

Would stuff like this now be disallowed?

@asterite
Copy link
Member Author

@kirbyfan64 Depends. What do you mean with this?

# a on the right hand side doesn't seem to be defined, so it's an error
a = my_fun a

I said wrong before, I meant to say that I'd like assignment of a variable inside an assignment to that same variable to be an error. But reading a variable if it was declared before is fine.

@refi64
Copy link
Contributor

refi64 commented Jun 21, 2016

I said wrong before, I meant to say that I'd like assignment of a variable inside an assignment to that same variable to be an error. But reading a variable if it was declared before is fine.

Ah, ok. Sorry for the noise.

@asterite asterite force-pushed the feature/local_var_assign_shadow branch from b841f90 to 2c9c710 Compare July 5, 2016 21:40
@asterite asterite merged commit e8b1b84 into master Jul 5, 2016
@asterite asterite deleted the feature/local_var_assign_shadow branch July 5, 2016 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants