Skip to content

Commit

Permalink
RakuAST: also detect negative subscript range at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Apr 7, 2024
1 parent 9b377fc commit e75c416
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Raku/ast/expressions.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,10 @@ class RakuAST::Postcircumfix::ArrayIndex
&& nqp::istype($statements[0], RakuAST::Statement::Expression)
{
my $expression := $statements[0].expression;

# ApplyPrefix ⎡-⎤
# Prefix ⎡-⎤
# IntLiteral ⎡2⎤
if nqp::istype($expression, RakuAST::ApplyPrefix)
&& nqp::istype($expression.prefix, RakuAST::Prefix)
&& $expression.prefix.operator eq '-'
Expand All @@ -1980,6 +1984,31 @@ class RakuAST::Postcircumfix::ArrayIndex
old => "a negative " ~ $literal ~ " subscript to index from the end",
replacement => "a function such as *" ~ $literal;
}

# ApplyInfix ⎡..⎤
# Infix 【..】 ⎡..⎤
# ArgList
# IntLiteral ⎡0⎤
# ApplyPrefix ⎡-⎤
# Prefix ⎡-⎤
# IntLiteral ⎡2⎤
if nqp::istype($expression, RakuAST::ApplyInfix)
&& nqp::istype($expression.infix, RakuAST::Infix)
&& $expression.infix.operator eq '..'
&& $expression.args.args.elems == 2
&& nqp::istype((my $end := $expression.args.args.AT-POS(1)), RakuAST::ApplyPrefix)
&& nqp::istype($end.prefix, RakuAST::Prefix)
&& $end.prefix.operator eq '-'
&& nqp::istype($end.operand, RakuAST::IntLiteral)
{
my $literal := -$end.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;
}
}

}
Expand Down

0 comments on commit e75c416

Please sign in to comment.