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

Cannot assign to a readonly variable or a value on RW class attribute #2622

Open
melezhik opened this Issue Jan 18, 2019 · 7 comments

Comments

Projects
None yet
3 participants
@melezhik
Copy link

melezhik commented Jan 18, 2019

The Problem

Cannot assign to rw class attribute:

 perl6 -e 'class A { has Hash $.b is rw; method set-b { $.b = %( foo => 1 ); self.b } }; say A.new.set-b'
Cannot assign to a readonly variable or a value
  in method set-b at -e line 1
  in block <unit> at -e line 1

Expected Behavior

$.b = Hash should work and not throw exception.

Actual Behavior

Throw exception

Workaround

change $.b ==> self.b

$ perl6 --ll-exception -e 'class A { has Hash $.b is rw; method set-b { self.b = %( foo => 1 ); self.b } }; say A.new.set-b'
{foo => 1}

Steps to Reproduce


$ perl6 --ll-exception -e 'class A { has Hash $.b is rw; method set-b { $.b = %( foo => 1 ); self.b } }; say A.new.set-b'
Cannot assign to a readonly variable or a value
   at gen/moar/BOOTSTRAP.nqp:1633  (/opt/rakudo-pkg/share/nqp/lib/Perl6/BOOTSTRAP.moarvm:)
 from -e:1  (<ephemeral file>:set-b)
 from -e:1  (<ephemeral file>:<unit>)
 from -e:1  (<ephemeral file>:<unit-outer>)
 from gen/moar/stage2/NQPHLL.nqp:1685  (/opt/rakudo-pkg/share/nqp/lib/NQPHLL.moarvm:eval)
 from gen/moar/stage2/NQPHLL.nqp:1796  (/opt/rakudo-pkg/share/nqp/lib/NQPHLL.moarvm:)
 from gen/moar/stage2/NQPHLL.nqp:1851  (/opt/rakudo-pkg/share/nqp/lib/NQPHLL.moarvm:command_eval)
 from src/Perl6/Compiler.nqp:48  (/opt/rakudo-pkg/share/nqp/lib/Perl6/Compiler.moarvm:command_eval)
 from gen/moar/stage2/NQPHLL.nqp:1777  (/opt/rakudo-pkg/share/nqp/lib/NQPHLL.moarvm:command_line)
 from gen/moar/main.nqp:54  (/opt/rakudo-pkg/share/perl6/runtime/perl6.moarvm:MAIN)
 from gen/moar/main.nqp:42  (/opt/rakudo-pkg/share/perl6/runtime/perl6.moarvm:<mainline>)
 from <unknown>:1  (/opt/rakudo-pkg/share/perl6/runtime/perl6.moarvm:<main>)
 from <unknown>:1  (/opt/rakudo-pkg/share/perl6/runtime/perl6.moarvm:<entry>)

Environment

  • Operating system:
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
  • Compiler version (perl6 -v):
This is Rakudo version 2018.12 built on MoarVM version 2018.12
implementing Perl 6.d.

@melezhik melezhik changed the title Cannot assign to a readonly variable or a value on RW class attribue Cannot assign to a readonly variable or a value on RW class attribute Jan 18, 2019

@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 18, 2019

bisectable shows this has been around since like forever.

Looking at the location in the Bootstrap, it looks like there is no descriptor available.

@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 18, 2019

Also doesn't matter if a default has been initialized or not

@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 18, 2019

The $cont that is being received in the bootstrap, is confirmed to be a VMNull in this case.

@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 18, 2019

This appears to be the only difference in code gen between - $.b and self.b

-                                    - QAST::Op(hllize) <wanted>
-                                      - QAST::Op(callmethod b)  b
-                                        - QAST::Var(lexical self) <wanted> self                                 
+                                    - QAST::Op(callmethod item) <wanted>
+                                      - QAST::Op(callmethod b) <wanted>
+                                        - QAST::Var(lexical self) <wanted>
@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 18, 2019

Meanwhile, another workaround:

class A {
    has %.b is rw;
    method c() { %.b = %( a => 42 ) }
}
my $a = A.new;
$a.c;
dd $a;  #  A.new(b => {:a(42)})
@melezhik

This comment has been minimized.

Copy link
Author

melezhik commented Jan 22, 2019

fails on Arrays too:

$ perl6 -e 'class A { has Array $.b is rw; method set-b { $.b = ( 1,2 ); self.b } }; say A.new.set-b'
Cannot assign to a readonly variable or a value
  in method set-b at -e line 1
  in block <unit> at -e line 1

@lizmat

This comment has been minimized.

Copy link
Contributor

lizmat commented Jan 22, 2019

And the workaround for this one is the similar:

class A {
    has @.b is rw;
    method set-b { @.b = ( 1,2 ); self.b }
}
say A.new.set-b   # [1,2]
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.