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

Giving a sub to a map fails #5719

Closed
p6rt opened this issue Oct 3, 2016 · 7 comments
Closed

Giving a sub to a map fails #5719

p6rt opened this issue Oct 3, 2016 · 7 comments

Comments

@p6rt
Copy link

p6rt commented Oct 3, 2016

Migrated from rt.perl.org#129790 (status was 'resolved')

Searchable as RT129790$

@p6rt
Copy link
Author

p6rt commented Oct 3, 2016

From @zoffixznet

Attempting to give a map some invalid arg or a sub gives an error​:

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e '^4 .map​: {};'
  ===SORRY!===
  Cannot find method 'count' on object of type NQPMu
  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e '^4 .map​: 42;'
  ===SORRY!===
  Cannot find method 'count' on object of type NQPMu
  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e 'sub foo ($) {say
"meow"}; ^4 .map​: &foo;'
  ===SORRY!===
  Cannot find method 'count' on object of type NQPMu

However, everything works fine, if we turn off the optimizer​:

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 --optimize=off -e '^4
.map​: {};'
  Cannot map a Range to a Hash.
  Did you mean to add a stub ({...}) or did you mean to .classify?
  in block <unit> at -e line 1

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 --optimize=off -e '^4
.map​: 42;'
  Cannot resolve caller map(Range​: Int); none of these signatures match​:
  ($​: Hash \h, *%_)
  (\SELF​: █; :$label, :$item, *%_)
  (HyperIterable​:D $​: █; :$label, *%_)
  in block <unit> at -e line 1

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 --optimize=off -e
'sub foo ($) {say "meow"}; ^4 .map​: &foo;'
  meow
  meow
  meow
  meow
  zoffix@​VirtualBox​:~/CPANPRC/rakudo$

Or, if we wrap the range into parenths​:

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e '(^4).map​: {};'
  Cannot map a Range to a Hash.
  Did you mean to add a stub ({...}) or did you mean to .classify?
  in block <unit> at -e line 1

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e '(^4).map​: 42;'
  Cannot resolve caller map(Range​: Int); none of these signatures match​:
  ($​: Hash \h, *%_)
  (\SELF​: █; :$label, :$item, *%_)
  (HyperIterable​:D $​: █; :$label, *%_)
  in block <unit> at -e line 1

  zoffix@​VirtualBox​:~/CPANPRC/rakudo$ ./perl6 -e 'sub foo ($) {say
"meow"}; (^4).map​: &foo;'
  meow
  meow
  meow
  meow

@p6rt
Copy link
Author

p6rt commented Oct 3, 2016

From @usev6

Playing around I found that the following change made your examples work as expected​:

Inline Patch
diff --git a/src/Perl6/Optimizer.nqp b/src/Perl6/Optimizer.nqp
index 12398ba..9102b7f 100644
--- a/src/Perl6/Optimizer.nqp
+++ b/src/Perl6/Optimizer.nqp
@@ -1082,7 +1082,8 @@ class Perl6::Optimizer {
                  || nqp::istype($op[0][0], QAST::Stmts) &&
                         nqp::istype(($c1 := $op[0][0][0]), QAST::Op) &&
                         nqp::existskey(%range_bounds, $c1.name)) &&
-              $!symbols.is_from_core($c1.name) {
+              $!symbols.is_from_core($c1.name) &&
+              nqp::defined($op[0][1].ann('code_object')) {
             self.optimize_for_range($op, $op[0][1], $c1);
             self.visit_op_children($op);
             return $op;

$ ./perl6 -e '^4 .map: {};' Cannot map a Range to a Hash\. Did you mean to add a stub \(\{\.\.\.\}\) or did you mean to \.classify?   in block \ at \-e line 1

$ ./perl6 -e '^4 .map​: 42;'
Cannot resolve caller map(Range​: Int); none of these signatures match​:
  ($​: Hash \h, *%_)
  (\SELF​: █; :$label, :$item, *%_)
  (HyperIterable​:D $​: █; :$label, *%_)
  in block <unit> at -e line 1

$ ./perl6 -e 'sub foo ($) {say "meow"}; ^4 .map​: &foo;'
meow
meow
meow
meow

The original error came from the first two lines in 'method optimize_for_ranges' in src/Perl6/Optimizer.nqp. For some reason $callee.ann('code_object') did not return a code object as expected.

method optimize_for_range($op, $callee, $c2) {
  my $code := $callee.ann('code_object');
  my $count := $code.count;

Now, I have no idea whether my change from above makes sense or whether the annotation for 'code_object' was wrong in the first place.

1 similar comment
@p6rt
Copy link
Author

p6rt commented Oct 3, 2016

From @usev6

Playing around I found that the following change made your examples work as expected​:

Inline Patch
diff --git a/src/Perl6/Optimizer.nqp b/src/Perl6/Optimizer.nqp
index 12398ba..9102b7f 100644
--- a/src/Perl6/Optimizer.nqp
+++ b/src/Perl6/Optimizer.nqp
@@ -1082,7 +1082,8 @@ class Perl6::Optimizer {
                  || nqp::istype($op[0][0], QAST::Stmts) &&
                         nqp::istype(($c1 := $op[0][0][0]), QAST::Op) &&
                         nqp::existskey(%range_bounds, $c1.name)) &&
-              $!symbols.is_from_core($c1.name) {
+              $!symbols.is_from_core($c1.name) &&
+              nqp::defined($op[0][1].ann('code_object')) {
             self.optimize_for_range($op, $op[0][1], $c1);
             self.visit_op_children($op);
             return $op;

$ ./perl6 -e '^4 .map: {};' Cannot map a Range to a Hash\. Did you mean to add a stub \(\{\.\.\.\}\) or did you mean to \.classify?   in block \ at \-e line 1

$ ./perl6 -e '^4 .map​: 42;'
Cannot resolve caller map(Range​: Int); none of these signatures match​:
  ($​: Hash \h, *%_)
  (\SELF​: █; :$label, :$item, *%_)
  (HyperIterable​:D $​: █; :$label, *%_)
  in block <unit> at -e line 1

$ ./perl6 -e 'sub foo ($) {say "meow"}; ^4 .map​: &foo;'
meow
meow
meow
meow

The original error came from the first two lines in 'method optimize_for_ranges' in src/Perl6/Optimizer.nqp. For some reason $callee.ann('code_object') did not return a code object as expected.

method optimize_for_range($op, $callee, $c2) {
  my $code := $callee.ann('code_object');
  my $count := $code.count;

Now, I have no idea whether my change from above makes sense or whether the annotation for 'code_object' was wrong in the first place.

@p6rt
Copy link
Author

p6rt commented Oct 3, 2016

@usev6 - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Oct 15, 2017

From @AlexDaniel

Maybe it is worth noting that this is pretty much a regression (even though an old one, and caused by a non-optimizer change).

(2016-08-09) rakudo/rakudo@3284025

Output before and after​: https://gist.github.com/75b15f93438bd038cf0bec26c43eaa9f

On 2016-10-03 01​:32​:09, bartolin@​gmx.de wrote​:

Playing around I found that the following change made your examples
work as expected​:

diff --git a/src/Perl6/Optimizer.nqp b/src/Perl6/Optimizer.nqp
index 12398ba..9102b7f 100644
--- a/src/Perl6/Optimizer.nqp
+++ b/src/Perl6/Optimizer.nqp
@​@​ -1082,7 +1082,8 @​@​ class Perl6​::Optimizer {
|| nqp​::istype($op[0][0], QAST​::Stmts) &&
nqp​::istype(($c1 := $op[0][0][0]), QAST​::Op)
&&
nqp​::existskey(%range_bounds, $c1.name)) &&
- $!symbols.is_from_core($c1.name) {
+ $!symbols.is_from_core($c1.name) &&
+ nqp​::defined($op[0][1].ann('code_object')) {
self.optimize_for_range($op, $op[0][1], $c1);
self.visit_op_children($op);
return $op;

$ ./perl6 -e '^4 .map​: {};'
Cannot map a Range to a Hash.
Did you mean to add a stub ({...}) or did you mean to .classify?
in block <unit> at -e line 1

$ ./perl6 -e '^4 .map​: 42;'
Cannot resolve caller map(Range​: Int); none of these signatures match​:
($​: Hash \h, *%_)
(\SELF​: █; :$label, :$item, *%_)
(HyperIterable​:D $​: █; :$label, *%_)
in block <unit> at -e line 1

$ ./perl6 -e 'sub foo ($) {say "meow"}; ^4 .map​: &foo;'
meow
meow
meow
meow

The original error came from the first two lines in 'method
optimize_for_ranges' in src/Perl6/Optimizer.nqp. For some reason
$callee.ann('code_object') did not return a code object as expected.

method optimize_for_range($op, $callee, $c2) {
my $code := $callee.ann('code_object');
my $count := $code.count;

Now, I have no idea whether my change from above makes sense or
whether the annotation for 'code_object' was wrong in the first place.

@p6rt
Copy link
Author

p6rt commented Jan 20, 2018

From @zoffixznet

On Sat, 14 Oct 2017 18​:38​:47 -0700, alex.jakimenko@​gmail.com wrote​:

Maybe it is worth noting that this is pretty much a regression (even
though an
old one, and caused by a non-optimizer change).

(2016-08-09)
rakudo/rakudo@3284025

Output before and after​:
https://gist.github.com/75b15f93438bd038cf0bec26c43eaa9f

On 2016-10-03 01​:32​:09, bartolin@​gmx.de wrote​:

Playing around I found that the following change made your examples
work as expected​:

diff --git a/src/Perl6/Optimizer.nqp b/src/Perl6/Optimizer.nqp
index 12398ba..9102b7f 100644
--- a/src/Perl6/Optimizer.nqp
+++ b/src/Perl6/Optimizer.nqp
@​@​ -1082,7 +1082,8 @​@​ class Perl6​::Optimizer {
|| nqp​::istype($op[0][0], QAST​::Stmts) &&
nqp​::istype(($c1 := $op[0][0][0]), QAST​::Op)
&&
nqp​::existskey(%range_bounds, $c1.name)) &&
- $!symbols.is_from_core($c1.name) {
+ $!symbols.is_from_core($c1.name) &&
+ nqp​::defined($op[0][1].ann('code_object')) {
self.optimize_for_range($op, $op[0][1], $c1);
self.visit_op_children($op);
return $op;

$ ./perl6 -e '^4 .map​: {};'
Cannot map a Range to a Hash.
Did you mean to add a stub ({...}) or did you mean to .classify?
in block <unit> at -e line 1

$ ./perl6 -e '^4 .map​: 42;'
Cannot resolve caller map(Range​: Int); none of these signatures
match​:
($​: Hash \h, *%_)
(\SELF​: █; :$label, :$item, *%_)
(HyperIterable​:D $​: █; :$label, *%_)
in block <unit> at -e line 1

$ ./perl6 -e 'sub foo ($) {say "meow"}; ^4 .map​: &foo;'
meow
meow
meow
meow

The original error came from the first two lines in 'method
optimize_for_ranges' in src/Perl6/Optimizer.nqp. For some reason
$callee.ann('code_object') did not return a code object as expected.

method optimize_for_range($op, $callee, $c2) {
my $code := $callee.ann('code_object');
my $count := $code.count;

Now, I have no idea whether my change from above makes sense or
whether the annotation for 'code_object' was wrong in the first
place.

Thank you for the report. This is now fixed in branch `post-release`.

Fix​: rakudo/rakudo@f3efe5e6b4a9ee5
Test​: rakudo/rakudo@f3efe5e6b4a9ee5

@p6rt p6rt closed this as completed Jan 20, 2018
@p6rt
Copy link
Author

p6rt commented Jan 20, 2018

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt added the optimizer label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant