Skip to content

Commit

Permalink
RakuAST: detect passing single literal to native rw arg at compile time
Browse files Browse the repository at this point in the history
Not sure why the old frontend handles this case of having a single such
argument specially. Let's match it until we know more.
  • Loading branch information
niner committed Apr 19, 2024
1 parent f6cc5d9 commit 8f568c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Raku/ast/call.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class RakuAST::Call::Name
}

method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my int $ARG_IS_LITERAL := 32;

if self.is-resolved && (
nqp::istype(self.resolution, RakuAST::CompileTimeValue)
|| nqp::can(self.resolution, 'maybe-compile-time-value')
Expand All @@ -268,12 +270,17 @@ class RakuAST::Call::Name
my @types;
my @flags;
my $ok := 1;
for self.IMPL-UNWRAP-LIST(self.args.args) {
my @args := self.IMPL-UNWRAP-LIST(self.args.args);
for @args {
my $type := $_.type;
nqp::push(@types, $type);
$ok := 0 if $type =:= Mu; # Don't know the type
nqp::push(@flags, nqp::objprimspec($type));
}
if nqp::elems(@types) == 1 && nqp::istype(@args[0], RakuAST::Literal) {
my $rev := @args[0].native-type-flag;
@flags[0] := nqp::defined($rev) ?? $rev +| $ARG_IS_LITERAL !! 0;
}
if $ok {
my $ct_result := nqp::p6trialbind($sig, @types, @flags);
if $ct_result == -1 {
Expand Down
7 changes: 7 additions & 0 deletions src/Raku/ast/literals.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RakuAST::Literal

method expression() { self }
method type() { $!value.WHAT }
method native-type-flag() { Nil }
method compile-time-value() { $!value }
method IMPL-CAN-INTERPRET() { True }
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
Expand Down Expand Up @@ -64,6 +65,8 @@ class RakuAST::Constant
class RakuAST::IntLiteral
is RakuAST::Literal
{
method native-type-flag() { 1 }

method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.value;
$context.ensure-sc($value);
Expand All @@ -81,6 +84,8 @@ class RakuAST::IntLiteral
class RakuAST::NumLiteral
is RakuAST::Literal
{
method native-type-flag() { 2 }

method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.value;
$context.ensure-sc($value);
Expand Down Expand Up @@ -115,6 +120,8 @@ class RakuAST::MapLiteral
class RakuAST::StrLiteral
is RakuAST::Literal
{
method native-type-flag() { 3 }

method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.value;
$context.ensure-sc($value);
Expand Down

0 comments on commit 8f568c6

Please sign in to comment.