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
Scope flattening and regexes #2608
Comments
|
Ah! Alright! Here's a code snippet that's golfed a bit less (cfa++): my $rx = rx/<[a..z]>/; 'abc'.comb.map({ if $rx { 'yes' } else { 'no' } }).say;And this golf, unlike the one in the comment above, is not affected by eb3917c: |
|
Comment from @jnthn:
|
|
Documenting a trap might be advisable? To be clear, here's what's going on: (i) 2018.12, dynamic $_ = 'z'; my $rx = rx/<[a..z]>/; 'abc'.comb.map({ if $rx { $/ } else { 'no' } }).say;
# OUTPUT: «(「a」 「b」 「c」)»(ii) HEAD, lexical > $_ = 'z'; my $rx = rx/<[a..z]>/; 'abc'.comb.map({ if $rx { $/ } else { 'no' } }).say;
# OUTPUT: «(「z」 「z」 「z」)»or for comparison with the > $_ = Any; my $rx = rx/<[a..z]>/; 'abc'.comb.map({ if $rx { $/ } else { 'no' } }).say;
(no no no)(iii) Quickest fix to $_ = 'z'; my $rx = rx/<[a..z]>/; 'abc'.comb.map({ if /$rx/ { $/ } else { 'no' } }).say;
# OUTPUT: «(「a」 「b」 「c」)»RfC @jnthn @AlexDaniel. |
|
I think we need to decide on:
Having Therefore, in 6.d, it was decided that The one case that has shown up as awkward is the At present, the optimizer looks for scopes where a regex literal appears, and refuses to optimize out The other cases get...interesting, and honestly I'm not sure it was an intended feature that you could stick a regex literal in a variable and then use it as a boolean "at a distance". With current Rakudo, you get the first One option would be for the closure clone of a regex literal to capture the reference to the In the meantime, we could perhaps make the assumption that uses of a regex in boolean context will appear lexically beneath the point of the literal, and maybe poison Thoughts? |
This worked for the slightly dubious reason that `grep` happened to set `$_`, and boolify the regex that was returned. This was relying on an implementation detail - there's nothing that says `grep` has to have the test element in its `$_` - however there's code in the wild that relies on this construct, and it's hard to argue that it shouldn't DWIM. It stopped working when the `$_` in `grep` ceased to be dynamically available, as it no longer needs to be. For now, solve it by making `grep` explictly handle the case where a Regex is returned to it. This fixes #2614. If in the future we accept the proposal that a regex will, when cloned, capture the `$_` in the scope it literally appears in for the purpose of use in boolean or sink context, as proposed in #2608, then this change can be removed again. It's also a good sign for that proposal that it happens to solve this problem neatly.
|
@jnthn any new thoughts since the day you post your comment? Personally I'm not too concerned about this issue given that you can still get the previous behavior with |
|
@AlexDaniel I'm still quite keen on the "closure clone of a regex captures I've done an implementation of it to see what happens. The main thing it doesn't do is retain the 6.c behavior; I can make that happen, but it's only really worth it if I know we decide to go this way. I think we should, but I'd like to here a few other opinions. See the notes in the linked commit for details. The one spectest in question looks like this; And would have to become something like this: Which is still a bit...uh...interesting in terms of how it works (basically, just due to the |
|
With a lack of objections here or, to my knowledge, elsewhere, and some amount of agreement on the change, I've now added the previously mentioned commit to |
|
Thank you, @jnthn. This can be closed. |
cfa++ reported that
URI::Encodeis failing. Blin bisected to the same commit 541a4f1.However, it's a bit interesting. After attempting to golf we found that this code changed its behavior:
But that golf bisected to (2019-01-03) eb3917c, which is a different commit.
Furthermore, that same commit doesn't affect the module! Proof.
Both commits are part of the same work.
Originally posted by @AlexDaniel in #2601 (comment)
The text was updated successfully, but these errors were encountered: