Skip to content

Commit

Permalink
RakuAST: throw the appropriate error for negative array index literals
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Apr 4, 2024
1 parent fe279b0 commit 65f349a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Raku/ast/expressions.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,7 @@ class RakuAST::Postcircumfix
# A postcircumfix array index operator, possibly multi-dimensional.
class RakuAST::Postcircumfix::ArrayIndex
is RakuAST::Postcircumfix
is RakuAST::CheckTime
is RakuAST::Lookup
{
has RakuAST::SemiList $.index;
Expand Down Expand Up @@ -1972,6 +1973,30 @@ class RakuAST::Postcircumfix::ArrayIndex

method IMPL-CURRIES() { 3 }

method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $statements := $!index.code-statements;

if nqp::elems($statements) == 1
&& nqp::istype($statements[0], RakuAST::Statement::Expression)
{
my $expression := $statements[0].expression;
if nqp::istype($expression, RakuAST::ApplyPrefix)
&& nqp::istype($expression.prefix, RakuAST::Prefix)
&& $expression.prefix.operator eq '-'
&& nqp::istype($expression.operand, RakuAST::IntLiteral)
{
my $literal := -$expression.operand.value;

self.add-sorry:
$resolver.build-exception:
'X::Obsolete',
old => "a negative " ~ $literal ~ " subscript to index from the end",
replacement => "a function such as *" ~ $literal;
}
}

}

method IMPL-POSTFIX-QAST(RakuAST::IMPL::QASTContext $context, Mu $operand-qast) {
my $name := self.resolution.lexical-name;
my $op := QAST::Op.new( :op('call'), :$name, $operand-qast );
Expand Down

0 comments on commit 65f349a

Please sign in to comment.