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

BEGIN phaser causes confusing error #2516

Open
jmaslak opened this issue Dec 4, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@jmaslak
Copy link
Contributor

commented Dec 4, 2018

The Problem

When using a BEGIN phaser to initialize an attribute in a module subject to precompiling, a confusing error is generated when the module is "use"-ed.

To duplicate:

Create a file test.pl6:

use v6
use Foo;

Create a file Foo.pm6:

use v6;
unit class Foo;
has $.foo = BEGIN { 1 };

Execute with PERL6LIB=. perl6 test.pl6

Expected Behavior

Same behavior as if the class was defined within test.pl6 (instead of an outside file) - no errors.

Example of working file, test-noerror.pl6:

use v6;
class Foo { has $.foo = BEGIN { 1 } };

I would expect this to work even when Foo is in a stand alone file (Foo.pm6). I expect the BEGIN { 1 } to get pre-compiled into an anonymous constant, and used to initialize the attribe as needed when used.

Actual Behavior

$ PERL6LIB=. perl6 test.pl6
===SORRY!===
Cannot look up attributes in a NQPMu type object

Steps to Reproduce

See above.

Environment

  • Operating system: Ubuntu 18.04
  • Compiler version (perl6 -v):
This is Rakudo version 2018.11-4-g38bc682b0 built on MoarVM version 2018.11
implementing Perl 6.d.

NOTE: On older Rakudo, such as 2018.09, a different error was given:

$ PERL6LIB=. perl6 test.pl6
===SORRY!===
Expected MAST::Frame, but didn't get one
@lizmat

This comment has been minimized.

Copy link
Contributor

commented Dec 31, 2018

$ perl6 --ll-exception -Ilibb -MFoo -e ''
Cannot look up attributes in a NQPMu type object
   at gen/moar/stage2/QAST.nqp:7285  (install/share/nqp/lib/QAST.moarvm:get_frame_index)
 from install/bin/../share/nqp/lib/MAST/Ops.nqp:7212  (install/share/nqp/lib/MASTOps.moarvm:)

which points to:

method get_frame_index(MAST::Frame $f) {
    my $idx := 0;
    if nqp::getattr($f, MAST::Frame, '$!flags') +& 32768 { # FRAME_FLAG_HAS_INDEX
                    ^^ is a type object

So I would guess that this bug has been around for a long time already, but has changed shape after the MAST refactor.

@lizmat

This comment has been minimized.

Copy link
Contributor

commented Dec 31, 2018

I don't have a solution to this atm, but it looks like a codegen issue related to BEGIN blocks. In the meantime, there is a workaround:

unit class Foo;
has $.foo = my constant = 1;

so basically s/BEGIN/my constant =/

@lizmat

This comment has been minimized.

Copy link
Contributor

commented Dec 31, 2018

@niner: perhaps you have some insights?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.