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

Incorrect translation of &. evaluating LHS twice #68

Closed
eregon opened this issue Jan 10, 2021 · 2 comments
Closed

Incorrect translation of &. evaluating LHS twice #68

eregon opened this issue Jan 10, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@eregon
Copy link

eregon commented Jan 10, 2021

test.rb:

p Object.new&.object_id
$ ruby-next -V --min-version=2.2.0 nextify test.rb         
RubyNext core strategy: refine
RubyNext transpile mode: rewrite
Remove old files: ./.rbnext
Generated: ./.rbnext/2.3/test.rb
$ cat ./.rbnext/2.3/test.rb
p ((!Object.new.nil? || nil) && Object.new.object_id)

But the LHS, Object.new should only be evaluated once.

Environment

Ruby version: 2.7

Ruby Next version: 0.11.1

@palkan palkan added the bug Something isn't working label Jan 11, 2021
@palkan
Copy link
Collaborator

palkan commented Jan 11, 2021

Thanks for the report!

So far, I have only the following idea:

Object.new&.object_id #=> (((__tmp__ = Object.new) || true) && (!__tmp__.nil? || nil) && __tmp__.object_id)

(and the lvar name should be unique in the context (file?))

@palkan palkan mentioned this issue Jan 11, 2021
3 tasks
palkan added a commit that referenced this issue Jan 11, 2021
palkan added a commit that referenced this issue Jan 11, 2021
@eregon
Copy link
Author

eregon commented Jan 11, 2021

Yes, I think an extra var is needed, just like for some cases of pattern matching.

I think it can be simplified a bit:

Object.new&.object_id
#=>
(__tmp__ = Object.new; __tmp__.nil? ? nil : __tmp__.object_id)

to be closer to the actual behavior, checking with nil.equal?(__tmp__) might be better.

@palkan palkan closed this as completed in 382b2d6 Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants