ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: chris@chrisseaton.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:99503] [Ruby master Bug#17105] A single `return` can return to two different places in a proc inside a lambda inside a method
Date: Thu, 06 Aug 2020 14:43:46 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-86960.20200806144345.772@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17105.20200806105428.772@ruby-lang.org

Issue #17105 has been updated by chrisseaton (Chris Seaton).


Hans I don't think anyone is debating the basic idea of what return in a proc or lambda does - I think we're talking about the edge-case for a proc in a return in the example above, which isn't explained by the text you have.

----------------------------------------
Bug #17105: A single `return` can return to two different places in a proc inside a lambda inside a method
https://bugs.ruby-lang.org/issues/17105#change-86960

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
A single `return` in the source code might return to 2 different lexical places.
That seems wrong to me, as AFAIK all other control flow language constructs always jump to a single place.

```ruby
def m(call_proc)
  r = -> {
    # This single return in the source might exit the lambda or the method!
    proc = Proc.new { return :return }

    if call_proc
      proc.call
      :after_in_lambda
    else
      proc
    end
  }.call # returns here if call_proc

  if call_proc
    [:after_in_method, r]
  else
    r.call
    :never_reached
  end
end


p m(true)  # => [:after_in_method, :return]
p m(false) # :return
```

We're trying to figure out the semantics of `return` inside a proc in
https://github.com/oracle/truffleruby/issues/1488#issuecomment-669185675
and this behavior doesn't seem to make much sense.

@headius also seems to agree:
> I would consider that behavior to be incorrect; once the proc has escaped from the lambda, its return target is no longer valid. It should not return to a different place.
> https://github.com/jruby/jruby/issues/6350#issuecomment-669603740

So:
* is this behavior intentional? or is it a bug?
* what are actually the semantics of `return` inside a proc?

The semantics seem incredibly complicated to a point developers have no idea where `return` actually goes.
Also it must get even more complicated if one defines a `lambda` method as the block in `lambda { return }` is then non-deterministically a proc or lambda.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2020-08-06 14:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-06 10:54 [ruby-core:99499] [Ruby master Bug#17105] A single `return` can return to two different places in a proc inside a lambda inside a method eregontp
2020-08-06 11:00 ` [ruby-core:99500] " eregontp
2020-08-06 14:34 ` [ruby-core:99502] " hanmac
2020-08-06 14:43 ` chris [this message]
2020-08-06 19:09 ` [ruby-core:99504] " daniel
2020-08-06 23:47 ` [ruby-core:99505] " eregontp
2020-08-09 10:41 ` [ruby-core:99524] " shyouhei
2020-08-12 21:29 ` [ruby-core:99574] " headius
2020-08-31  9:00 ` [ruby-core:99794] " matz
2021-02-25  2:22 ` [ruby-core:102600] " merch-redmine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-86960.20200806144345.772@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).