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

nicer error message for 12345[1] (number scalar indexed with .[]) #227

Closed
p6rt opened this issue Aug 11, 2008 · 20 comments
Closed

nicer error message for 12345[1] (number scalar indexed with .[]) #227

p6rt opened this issue Aug 11, 2008 · 20 comments
Labels
LHF Low-hanging fruit, easy to resolve

Comments

@p6rt
Copy link

p6rt commented Aug 11, 2008

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

Searchable as RT57790$

@p6rt
Copy link
Author

p6rt commented Aug 11, 2008

From @masak

r30166​:
$ perl6 -e '999999[1]'
get_pmc_keyed() not implemented in class 'Integer'
[...]
$ perl6 -e '1000000[1]'
get_pmc_keyed() not implemented in class 'Float'
[...]

So​:

* For some reason, a literal integer followed by an array index makes
it though parsing. Is it really valid standard Perl 6 syntax?

* A Parrot error is thrown once the code is run. Even if code like
this gets through the parsing stage (and we can't stop, say, integers
in variables being array-indexed), the error should be trapped by
rakudo.

* Some magical limit between 999999 and 1000000 makes the error start
talking about 'Float' rather than 'Integer'. This is also the point
when numbers are written in exponential form, as 1+e6. Perl 5 handles
integers of much higher magnitude by default.

Perl 5.8 has a simple solution to array indexing of integers​:

$ perl -e 1[1]'
syntax error at -e line 1, near "1["

@p6rt
Copy link
Author

p6rt commented Aug 14, 2008

From @pmichaud

On Mon, Aug 11, 2008 at 12​:08​:21AM -0700, Carl Mäsak wrote​:

r30166​:
$ perl6 -e '999999[1]'
get_pmc_keyed() not implemented in class 'Integer'
[...]
$ perl6 -e '1000000[1]'
get_pmc_keyed() not implemented in class 'Float'
[...]

So​:

* For some reason, a literal integer followed by an array index makes
it though parsing. Is it really valid standard Perl 6 syntax?

AFAICT, according to the grammar it's valid standard Perl 6 syntax.
Whether something else is supposed to catch this and throw an
exception is (as yet) unspecced. But keep in mind that we can
do things like method calls on literals, so it seems that other
postfixish sorts of things are (syntactically) legal.

* A Parrot error is thrown once the code is run. Even if code like
this gets through the parsing stage (and we can't stop, say, integers
in variables being array-indexed), the error should be trapped by
rakudo.

At present Rakudo doesn't try to interpret all exceptions that Parrot
may throw and impose its own interpretations on them. But suggestions
are welcome here.

* Some magical limit between 999999 and 1000000 makes the error start
talking about 'Float' rather than 'Integer'. This is also the point
when numbers are written in exponential form, as 1+e6. Perl 5 handles
integers of much higher magnitude by default.

I think this has to do with the way that integers are interpreted
in PIR. I'm not exactly certain how we want to resolve it, but
it's likely going to require some help from Parrot. (Either that
or we get POST to compile to bytecode directly and avoid the PIR
translation altogether.)

Pm

@p6rt
Copy link
Author

p6rt commented Aug 14, 2008

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

@p6rt
Copy link
Author

p6rt commented Aug 14, 2008

From @TimToady

On Thu, Aug 14, 2008 at 02​:32​:29AM -0500, Patrick R. Michaud wrote​:
: AFAICT, according to the grammar it's valid standard Perl 6 syntax.

At least in pugs, 42[0] is legal, and produces the value 42, on
the theory that anything can be considered a list of 1 item, and
if you use something as a list, it is one. And it's a useful
idiom when you're not sure whether you are getting a list or not,
but you just want the first value in any case.

Larry

@p6rt
Copy link
Author

p6rt commented Aug 14, 2008

From @moritz

Carl MXXsak (via RT) wrote​:

# New Ticket Created by "Carl Mäsak"
# Please include the string​: [perl #​57790]
# in the subject line of all future correspondence about this issue.
# <URL​: http://rt.perl.org/rt3/Ticket/Display.html?id=57790 >

r30166​:
$ perl6 -e '999999[1]'
get_pmc_keyed() not implemented in class 'Integer'
[...]
$ perl6 -e '1000000[1]'
get_pmc_keyed() not implemented in class 'Float'
[...]

So​:

* For some reason, a literal integer followed by an array index makes
it though parsing. Is it really valid standard Perl 6 syntax?

Yes. Imagine

class Int is also { method postcircumfix​:<[ ]> { ... } }

In general .[] and .{} are just method calls with a fancy syntax, so
they are allowed everywhere (but might throw runtime errors).

Cheers,
Moritz

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

@p6rt
Copy link
Author

p6rt commented Apr 2, 2009

From @masak

<PhatEddy> rakudo​: 1000000[1]
<p6eval> rakudo c40f3b​: OUTPUT«elements() not implemented in class
'Integer' [...]
<masak> PhatEddy​: what did you expect that to do?
<PhatEddy> according to an old ticket it was giving some parrot errors
<PhatEddy> rt 57790 http://rt.perl.org/rt3/Public/Bug/Display.html?id=57790
<masak> PhatEddy​: ah.
<masak> August... I didn't recognize it. :)
<masak> I think we can close that one.

@p6rt
Copy link
Author

p6rt commented Apr 2, 2009

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

@p6rt
Copy link
Author

p6rt commented Apr 2, 2009

From @ronaldxs

On Thu Apr 02 07​:02​:55 2009, masak wrote​:

<PhatEddy> rakudo​: 1000000[1]
<p6eval> rakudo c40f3b​: OUTPUT«elements() not implemented in class
'Integer' [...]

After further discussion this output was noted as an improvement but
not as good error ouput as desired.

For now an improvement to behavior similar to that displayed below was
loosely agreed upon as wanted​:

<PhatEddy> rakudo​: say "abc"[0]
<p6eval> rakudo c40f3b​: OUTPUT«Method 'postcircumfix​:[ ]' not found for
invocant of class 'Str'␤current instr.​: 'postcircumfix​:[ ]' pc 3926
(src/classes/Positional.pir​:100)␤»

@p6rt
Copy link
Author

p6rt commented Apr 2, 2009

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

@p6rt
Copy link
Author

p6rt commented Apr 4, 2009

From @ronaldxs

The attached patch should generate the desired error message. If
someone wants a test for this, just let me know which test file the
test belongs in and I will be happy to post an appropriate test for
that file.

@p6rt
Copy link
Author

p6rt commented Apr 4, 2009

From @ronaldxs

Positional_trap_elements.patch
--- a/src/classes/Positional.pir
+++ b/src/classes/Positional.pir
@@ -105,9 +105,24 @@ Returns a list element or slice.
     .param pmc invocant
     .param pmc argsblock
     .param pmc options :slurpy :named
+    
+    # if elements fails we can probably count on postcfix to err adequately
+    push_eh call_and_throw_err
     $I0 = elements invocant
     $P0 = box $I0
     set_hll_global ['Whatever'], '$!slice', $P0
+    goto just_call
+
+call_and_throw_err:
+    .const string NO_ELEMENTS_MARKER = "elements() not implemented"
+    .get_results ($P0)
+    $I0 = length NO_ELEMENTS_MARKER
+    $S0 = $P0
+    $S0 = substr $S0, 0, $I0
+    if $S0 == NO_ELEMENTS_MARKER goto just_call
+    rethrow $P0
+
+just_call:
     .local pmc args
     args = argsblock()
     args = 'list'(args)

@p6rt
Copy link
Author

p6rt commented Apr 7, 2010

From @ShimmerFairy

As a convenience, the error generated now is​:
[17​:28​:35] <lue> rakudo​: say 10000000[1]
[17​:28​:37] <p6eval> rakudo b75b2b​: OUTPUT«get_pmc_keyed() not
implemented in class 'Integer'␤current instr.​:
'perl6;Positional[​::T];postcircumfix​:<[ ]>' pc 10553
(src/metamodel/GrammarHOW.pir​:44)␤»
This is the same whether it's 999999 or 1000000.
--
Don't Panic!

@p6rt
Copy link
Author

p6rt commented Jul 23, 2010

From @coke

On Tue Apr 06 17​:31​:42 2010, lue wrote​:

As a convenience, the error generated now is​:
[17​:28​:35] <lue> rakudo​: say 10000000[1]
[17​:28​:37] <p6eval> rakudo b75b2b​: OUTPUT«get_pmc_keyed() not
implemented in class 'Integer'␤current instr.​:
'perl6;Positional[​::T];postcircumfix​:<[ ]>' pc 10553
(src/metamodel/GrammarHOW.pir​:44)␤»
This is the same whether it's 999999 or 1000000.

With Altanta​:

$ ./perl6

say 10000000[1]
===SORRY!===
.[1] out of range for type Int()

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Sep 4, 2010

From @ShimmerFairy

The attached patch changes the error message from a `fail' message to a
`die' message, which allows the error to appear everywhere.
--
Don't Panic!

@p6rt
Copy link
Author

p6rt commented Sep 4, 2010

From @ShimmerFairy

errorfix

@p6rt
Copy link
Author

p6rt commented Sep 16, 2010

From @pmichaud

I'm fairly certain that Rakudo's current behavior (returning a Failure
for indexes other than zero) is correct here. Switching "fail" to "die"
is definitely not correct.

I'm declaring this ticket "fixed" and can be resolved when we have
spectests verifying that the non-zero indexes return a Failure.

Pm

1 similar comment
@p6rt
Copy link
Author

p6rt commented Sep 16, 2010

From @pmichaud

I'm fairly certain that Rakudo's current behavior (returning a Failure
for indexes other than zero) is correct here. Switching "fail" to "die"
is definitely not correct.

I'm declaring this ticket "fixed" and can be resolved when we have
spectests verifying that the non-zero indexes return a Failure.

Pm

@p6rt
Copy link
Author

p6rt commented Sep 18, 2010

From @moritz

Tested in S02-builtin_data_types/array.t

@p6rt
Copy link
Author

p6rt commented Sep 18, 2010

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

@p6rt p6rt closed this as completed Sep 18, 2010
@p6rt
Copy link
Author

p6rt commented Sep 18, 2010

From @kyleha

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

commit 5ca3dae8bbf33a3e051a2e5389bea2c68c1b3448
Author​: Moritz Lenz <moritz@​faui2k3.org>
Date​: Sat Sep 18 19​:46​:16 2010 +0200

  test for RT #​57790, scalars indexed with [1] should return a Failure

Inline Patch
diff --git a/S02-builtin_data_types/array.t b/S02-builtin_data_types/array.t
index b6685e7..8283e98 100644
--- a/S02-builtin_data_types/array.t
+++ b/S02-builtin_data_types/array.t
@@ -2,7 +2,7 @@ use v6;
 
 use Test;
 
-plan 102;
+plan 103;
 
 #L<S02/Mutable types/Array>
 
@@ -329,7 +329,7 @@ my @array2 = ("test", 1, Mu);
     is [][].elems, 0, '[][] returns empty list/array';
 }
 
-# RT #58372
+# RT #58372 and RT #57790
 # by current group understanding of #perl6, postcircumifx:<[ ]> is actually
 # defined in Any, so that .[0] is the identity operation for non-Positional
 # types
@@ -337,6 +337,7 @@ my @array2 = ("test", 1, Mu);
     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';
+    isa_ok 1[1],  Failure, 'indexing a scalar with other than 0 returns a Failure';
     dies_ok { Mu.[0] }, 'but Mu has no .[]';
 }
 

@p6rt p6rt added the LHF Low-hanging fruit, easy to resolve label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LHF Low-hanging fruit, easy to resolve
Projects
None yet
Development

No branches or pull requests

1 participant