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

Negative-index Failure over-eagerly triggers even for Whatever-y indexings in Rakudo #2681

Closed
p6rt opened this issue Mar 21, 2012 · 9 comments

Comments

@p6rt
Copy link

p6rt commented Mar 21, 2012

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

Searchable as RT111924$

@p6rt
Copy link
Author

p6rt commented Mar 21, 2012

From @masak

<felher> nom​: my @​array; say @​array[4];
<p6eval> rakudo 1a468d​: OUTPUT�Any()␤�
<felher> nom​: my @​array; say @​array[*-1];
<p6eval> rakudo 1a468d​: OUTPUT�Failure.new(exception =>
X​::AdHoc.new(payload => "Cannot use negative index -1 on Array"))␤�
* felher hoped for 'Any', too
<masak> probably it's * getting expanded as 0, and then the failure
triggers (wrongly)
<masak> it could be argued that it should never trigger for whatever-y
expressions.
* masak submits rakudobug
<felher> \o/

@p6rt
Copy link
Author

p6rt commented Oct 11, 2014

From @usev6

The current error message is kind of funny and supports masak's assumption that * getting expanded as 0, and then the failure triggers (wrongly)​:

$ perl6-m -e 'my @​array; say @​array[*-1]'
Unsupported use of [-1] subscript to access from end of Array; in Perl 6 please use [*-1]

(An exception of type X​::Subscript​::FromEnd is thrown.)

1 similar comment
@p6rt
Copy link
Author

p6rt commented Oct 11, 2014

From @usev6

The current error message is kind of funny and supports masak's assumption that * getting expanded as 0, and then the failure triggers (wrongly)​:

$ perl6-m -e 'my @​array; say @​array[*-1]'
Unsupported use of [-1] subscript to access from end of Array; in Perl 6 please use [*-1]

(An exception of type X​::Subscript​::FromEnd is thrown.)

@p6rt
Copy link
Author

p6rt commented Oct 11, 2014

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

@p6rt
Copy link
Author

p6rt commented Oct 11, 2014

From @pmichaud

The fact that an Exception is thrown is correct -- negative subscripts are not allowed unless the subscript has been declared modular (from S09).

The Exception message needs to be changed. STD.pm6 has "in Perl 6 please use [*-1]" when it can statically determine that a negative subscript has been passed. For dynamically computed negative subscript, the message can hint that something like *-1 might be useful but definitely shouldn't recommend using the very thing that just failed.

Better would be to produce a different error message when a negative subscript is generated from a Code (WhateverCode) object.

Pm

@p6rt
Copy link
Author

p6rt commented Nov 25, 2014

From @usev6

TimToady++ recently worked on this (rakudo/rakudo@10aa8bf41c) and it should be in good shape now​:

$ perl6 -e 'my @​array; say @​array[*-1]'
Calculated index (-1) is negative, but Array allows only 0-based indexing
  in method gist at src/gen/m-CORE.setting​:13776
  in sub say at src/gen/m-CORE.setting​:16510
  in block <unit> at -e​:1

The exception is now of type X​::Subscript​::Negative and the error message no longer recommends to use [*-1]. It does so when using [-1]​:

$ perl6-m -e 'my @​array; say @​array[-1]'
===SORRY!=== Error while compiling -e
Unsupported use of a negative -1 subscript to index from the end; in Perl 6 please use a function such as *-1
at -e​:1
------> my @​array; say @​array[-1]⏏<EOL>

There are tests in S02-types/array.t. I'm closing this ticket now.

1 similar comment
@p6rt
Copy link
Author

p6rt commented Nov 25, 2014

From @usev6

TimToady++ recently worked on this (rakudo/rakudo@10aa8bf41c) and it should be in good shape now​:

$ perl6 -e 'my @​array; say @​array[*-1]'
Calculated index (-1) is negative, but Array allows only 0-based indexing
  in method gist at src/gen/m-CORE.setting​:13776
  in sub say at src/gen/m-CORE.setting​:16510
  in block <unit> at -e​:1

The exception is now of type X​::Subscript​::Negative and the error message no longer recommends to use [*-1]. It does so when using [-1]​:

$ perl6-m -e 'my @​array; say @​array[-1]'
===SORRY!=== Error while compiling -e
Unsupported use of a negative -1 subscript to index from the end; in Perl 6 please use a function such as *-1
at -e​:1
------> my @​array; say @​array[-1]⏏<EOL>

There are tests in S02-types/array.t. I'm closing this ticket now.

@p6rt
Copy link
Author

p6rt commented Nov 25, 2014

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

@p6rt p6rt closed this as completed Nov 25, 2014
@p6rt
Copy link
Author

p6rt commented Nov 26, 2014

From @lizmat

On 25 Nov 2014, at 22​:37, Christian Bartolomaeus via RT <bugs-comment@​bugs6.perl.org> wrote​:

TimToady++ recently worked on this (rakudo/rakudo@10aa8bf41c) and it should be in good shape now​:

$ perl6 -e 'my @​array; say @​array[*-1]'
Calculated index (-1) is negative, but Array allows only 0-based indexing
in method gist at src/gen/m-CORE.setting​:13776
in sub say at src/gen/m-CORE.setting​:16510
in block <unit> at -e​:1

The exception is now of type X​::Subscript​::Negative and the error message no longer recommends to use [*-1]. It does so when using [-1]​:

$ perl6-m -e 'my @​array; say @​array[-1]'
===SORRY!=== Error while compiling -e
Unsupported use of a negative -1 subscript to index from the end; in Perl 6 please use a function such as *-1
at -e​:1
------> my @​array; say @​array[-1]⏏<EOL>

There are tests in S02-types/array.t. I'm closing this ticket now.

That’s because the first one is a run-time exception, at which time we don’t know anymore whether the calculated index involved * or not (a patch to that effect was reverted by TimToady++). So the error message cannot mention *, because it could have been used already.

The second one is a compile time warning because you’re using a literal “-1”. That *clearly* doesn’t use a *, so it can be mentioned then.

Liz

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