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

Indexing an undef value halts a program in Rakudo instead of just giving a warning #282

Closed
p6rt opened this issue Aug 26, 2008 · 8 comments

Comments

@p6rt
Copy link

p6rt commented Aug 26, 2008

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

Searchable as RT58372$

@p6rt
Copy link
Author

p6rt commented Aug 26, 2008

From @masak

r30537​:
$ ./perl6 -e '$/<a><b>; say "Alive!"'
get_pmc_keyed() not implemented in class 'Undef'
[...]

Compare Perl 5​:

$ perl -Mstrict -we '$_{'a'}{'b'}; print "Alive!\n"'
Useless use of hash element in void context at -e line 1.
Alive!

@p6rt
Copy link
Author

p6rt commented Aug 26, 2008

From @pmichaud

On Tue, Aug 26, 2008 at 01​:28​:22AM -0700, Carl Mäsak wrote​:

r30537​:
$ ./perl6 -e '$/<a><b>; say "Alive!"'
get_pmc_keyed() not implemented in class 'Undef'
[...]

Compare Perl 5​:

$ perl -Mstrict -we '$_{'a'}{'b'}; print "Alive!\n"'
Useless use of hash element in void context at -e line 1.
Alive!

The Perl 5 comparison really isn't applicable here. In Perl 5,
$_{'a'} refers to the %_ variable, which autovivifies as a
hash and thus doesn't generate any sort of error. The
"useless use ..." message just comes from using a value in
void context, and is unrelated to indexing. In fact, I get the
same warning for an unindexed use of $_ :

  $ perl -Mstrict -we '$_; print "Alive!\n"'
  Useless use of a variable in void context at -e line 1.
  Alive!

So, let's look at what is happening in Rakudo. In Perl 6,
unset variables (such as $/ above) are treated as
undef, which IIUC should immediately throw an exception
when attempting to use them in a value context without
first having tested for definedness or truth (S04​:922).
Since subscripting is attempting to use the value
of undef (as an array or hash), I'm thinking the correct
behavior is to immediately throw an exception here.

I completely agree that Rakudo needs a better exception
than "get_pmc_keyed() not implemented in class 'Undef'",
although that message _is_ fairly close to describing
exactly what happens.

So, I'll leave this ticket as open until we get an
appropriate exception in place, and possibly any
responses or clarifications from p6l.

Thanks!

Pm

@p6rt
Copy link
Author

p6rt commented Aug 26, 2008

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Aug 26, 2008

From @moritz

Patrick R. Michaud wrote​:

On Tue, Aug 26, 2008 at 01​:28​:22AM -0700, Carl Mäsak wrote​:

r30537​:
$ ./perl6 -e '$/<a><b>; say "Alive!"'
get_pmc_keyed() not implemented in class 'Undef'
[...]

Compare Perl 5​:

$ perl -Mstrict -we '$_{'a'}{'b'}; print "Alive!\n"'
Useless use of hash element in void context at -e line 1.
Alive!

The Perl 5 comparison really isn't applicable here. In Perl 5,
$_{'a'} refers to the %_ variable, which autovivifies as a
hash and thus doesn't generate any sort of error. The
"useless use ..." message just comes from using a value in
void context, and is unrelated to indexing. In fact, I get the
same warning for an unindexed use of $_ :

$ perl -Mstrict -we '$_; print "Alive!\n"'
Useless use of a variable in void context at -e line 1.
Alive!

So, let's look at what is happening in Rakudo. In Perl 6,
unset variables (such as $/ above) are treated as
undef, which IIUC should immediately throw an exception
when attempting to use them in a value context without
first having tested for definedness or truth (S04​:922).
Since subscripting is attempting to use the value
of undef (as an array or hash), I'm thinking the correct
behavior is to immediately throw an exception here.

Your analysis is certainly right in this case, but note that it doesn't
work in other cases either​:

"a" ~~ /a/; say $<c><d>
get_pmc_keyed() not implemented in class 'Undef'
my %h; say %h<a><b>
get_pmc_keyed() not implemented in class 'Undef'
my %h; say %h<a><b> = 2
get_pmc_keyed() not implemented in class 'Undef'

As opposed to Perl 5, the second example should not autovivify %h<a> in
the first case, only in the second one. But neither should throw an error.
To quote S09/Autovivification/​:

Autovivification will only happen if the vivifiable path is bound to a
read-write container. Value extraction (that is, binding to a readonly
or copy container) does not autovivify.

And later on brings an example​:

  # This is Perl 5 code
  my %hash;
  exists $hash{foo}{bar}; # creates $hash{foo} as an empty hash reference

In Perl 6 these read-only operations are indeed non-destructive​:

  my %hash;
  exists %hash<foo><bar>; # %hash is still empty

I think that's very clearly not what is implemented at the moment ;-)

--
Moritz Lenz
http://moritz.faui2k3.org/ | http://perl-6.de/

@p6rt
Copy link
Author

p6rt commented Jul 23, 2010

From @coke

On Tue Aug 26 01​:28​:22 2008, masak wrote​:

r30537​:
$ ./perl6 -e '$/<a><b>; say "Alive!"'
get_pmc_keyed() not implemented in class 'Undef'
[...]

Compare Perl 5​:

$ perl -Mstrict -we '$_{'a'}{'b'}; print "Alive!\n"'
Useless use of hash element in void context at -e line 1.
Alive!

With an Atlanta-era Rakudo, this now prints​:

$ ./perl6 -e '$/<a><b>; say "Alive!"'
Alive!

Assigning to moritz++ for testing, though I /think/ this might already
be covered.

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Jul 23, 2010

From @coke

Er, that variable used in the original post is more magical than most.
Here's an actual unknown var​:

$ ./perl6 -e '$Q<a><b>; say "Alive!"'
===SORRY!===
Symbol '$Q' not predeclared in <anonymous>

This seems to satisfy the original request, though, in that perl 6 is
strict by default.

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Jul 24, 2010

From @kyleha

This is an automatically generated mail to inform you that tests are now available in t/spec/S02-builtin_data_types/array.t

commit 062319f3e4bbfa3cd2a051ed4a96dd0f537d07da
Author​: moritz <moritz@​c213334d-75ef-0310-aa23-eaa082d1ae64>
Date​: Sat Jul 24 17​:49​:51 2010 +0000

  [t/spec] test that .[] is defined oin Any, and .[0] returns self for scalars. Tests RT #​58372
 
  git-svn-id​: http://svn.pugscode.org/pugs@&#8203;31810 c213334d-75ef-0310-aa23-eaa082d1ae64

Inline Patch
diff --git a/t/spec/S02-builtin_data_types/array.t b/t/spec/S02-builtin_data_types/array.t
index 7f38341..6a7752d 100644
--- a/t/spec/S02-builtin_data_types/array.t
+++ b/t/spec/S02-builtin_data_types/array.t
@@ -2,7 +2,7 @@ use v6;
 
 use Test;
 
-plan 96;
+plan *;
 
 #L<S02/Mutable types/Array>
 
@@ -329,4 +329,17 @@ my @array2 = ("test", 1, Mu);
     is [][].elems, 0, '[][] returns empty list/array';
 }
 
+# RT #58372
+# by current group understanding of #perl6, postcircumifx:<[ ]> is actually
+# defined in Any, so that .[0] is the identity operation for non-Positional
+# types
+{
+    is 1[0], 1, '.[0] is identiity operation for scalars (Int)';
+    is 'abc'[0], 'abc', '.[0] is identiity operation for scalars (Str)';
+    nok 'abc'[1].defined, '.[1] on a scalar is not defined';
+    dies_ok { Mu.[0] }, 'but Mu has no .[]';
+}
+
+done_testing;
+
 # vim: ft=perl6

@p6rt
Copy link
Author

p6rt commented Jul 24, 2010

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

@p6rt p6rt closed this as completed Jul 24, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant