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

Native array[int] accepts junction with negative index #6187

Open
p6rt opened this issue Apr 11, 2017 · 5 comments
Open

Native array[int] accepts junction with negative index #6187

p6rt opened this issue Apr 11, 2017 · 5 comments
Labels
Bug

Comments

@p6rt
Copy link

@p6rt p6rt commented Apr 11, 2017

Migrated from rt.perl.org#131134 (status was 'open')

Searchable as RT131134$

@p6rt
Copy link
Author

@p6rt p6rt commented Apr 11, 2017

From eigil@skjaeve.land

my Int @​a = 1, 2;
my int @​b = 1, 2;
say so @​a[-1 ^ 0];
True
say so @​b[-1 ^ 0];
False
say @​a[-1 ^ 0];
Index out of range. Is​: -1, should be in 0..^Inf
  in block <unit> at <unknown file> line 1

say @​b[-1 ^ 0];
one(2, 1)

It seems the native int array treats negative indices in a junction as
relative to the end of the array.

This is Rakudo version 2017.01-58-gd06d7c16b built on MoarVM version 2017.01-13-g4aab5064
implementing Perl 6.c.

@p6rt
Copy link
Author

@p6rt p6rt commented Apr 14, 2017

From @lizmat

On 11 Apr 2017, at 14​:44, Eigil Skjæveland (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Eigil Skjæveland
# Please include the string​: [perl #​131134]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=131134 >

my Int @​a = 1, 2;
my int @​b = 1, 2;
say so @​a[-1 ^ 0];
True
say so @​b[-1 ^ 0];
False
say @​a[-1 ^ 0];
Index out of range. Is​: -1, should be in 0..^Inf
in block <unit> at <unknown file> line 1

say @​b[-1 ^ 0];
one(2, 1)

It seems the native int array treats negative indices in a junction as
relative to the end of the array.

Any expression resulting in a negative value at runtime, exposes this behaviour​:

$ 6 'my int @​a = ^10; my $a = 0; dd @​a[$a-1]’
9

At the moment, this happens because the AT-POS candidate

@p6rt
Copy link
Author

@p6rt p6rt commented Apr 14, 2017

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

@p6rt
Copy link
Author

@p6rt p6rt commented Apr 14, 2017

From @lizmat

On 11 Apr 2017, at 14​:44, Eigil Skjæveland (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Eigil Skjæveland
# Please include the string​: [perl #​131134]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=131134 >

my Int @​a = 1, 2;
my int @​b = 1, 2;
say so @​a[-1 ^ 0];
True
say so @​b[-1 ^ 0];
False
say @​a[-1 ^ 0];
Index out of range. Is​: -1, should be in 0..^Inf
in block <unit> at <unknown file> line 1

say @​b[-1 ^ 0];
one(2, 1)

It seems the native int array treats negative indices in a junction as
relative to the end of the array.

Any expression resulting in a negative value at runtime, exposes this behaviour​:

$ 6 'my int @​a = ^10; my $a = 0; dd @​a[$a-1]’
9

At the moment, this happens because the AT-POS candidate directly uses nqp​::atposref_i for performance reasons.

Exactly for that reason, I don’t think we should fix this at the Perl 6 level, because we would lose a *lot* of performance if we did.

So, *if* we consider this to be a bug (and personally, I think it is), the index checking should happen at the VM level, rather than at the Perl 6 level.

@p6rt
Copy link
Author

@p6rt p6rt commented Apr 14, 2017

From @lizmat

On 14 Apr 2017, at 10​:29, Elizabeth Mattijsen <liz@​dijkmat.nl> wrote​:

On 11 Apr 2017, at 14​:44, Eigil Skjæveland (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Eigil Skjæveland
# Please include the string​: [perl #​131134]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=131134 >

my Int @​a = 1, 2;
my int @​b = 1, 2;
say so @​a[-1 ^ 0];
True
say so @​b[-1 ^ 0];
False
say @​a[-1 ^ 0];
Index out of range. Is​: -1, should be in 0..^Inf
in block <unit> at <unknown file> line 1

say @​b[-1 ^ 0];
one(2, 1)

It seems the native int array treats negative indices in a junction as
relative to the end of the array.

Any expression resulting in a negative value at runtime, exposes this behaviour​:

$ 6 'my int @​a = ^10; my $a = 0; dd @​a[$a-1]’
9

At the moment, this happens because the AT-POS candidate directly uses nqp​::atposref_i for performance reasons.

Exactly for that reason, I don’t think we should fix this at the Perl 6 level, because we would lose a *lot* of performance if we did.

Hmmm… the loss in performance appears to be in the 10% range, so maybe not as bad as I anticipated.

Liz

@p6rt p6rt added the Bug 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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.