[vim/vim] Can't write to public object variables (Issue #13130)

5 views
Skip to first unread message

errael

unread,
Sep 19, 2023, 8:50:05 PM9/19/23
to vim/vim, Subscribed

Steps to reproduce

In #13118 (comment) it says

In the current implementation the read-only object variable behaves like a "final" variable.

The following script gives the error

E1335: Member is not writable: inner

While it's true that inner is not writable, the test does not attempt to write to the variable inner. It is writing to the class Inner public variable val.

vim9script

class Inner
  public this.val = 13
endclass

class Outer
    this.inner = Inner.new()
endclass

var o1 = Outer.new()

o1.inner.var = 11    # <<<<< can't write

Expected behaviour

No error. The object's variable val is written to.

Version of Vim

9.0.1916

Environment

ubuntu

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13130@github.com>

errael

unread,
Sep 19, 2023, 8:57:27 PM9/19/23
to vim/vim, Subscribed

An example of writing to val when the object reference is final

vim9script

class Inner
  public this.val = 13
endclass

final o2 = Inner.new()
o2.val = 11
echo o2.val

def F()
    final o = Inner.new()
    o.val = 11
    echo o.val
enddef
F()


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13130/1726729979@github.com>

Christian J. Robinson

unread,
Sep 19, 2023, 8:59:13 PM9/19/23
to vim...@googlegroups.com, reply+ACY5DGHGZZR3LTWIZ6...@reply.github.com
It works fine for me once I correct a couple of bugs:

    vim9script

    class Inner
        public this.val = 13
    endclass

    class Outer
        public this.inner = Inner.new()

    endclass

    var o1 = Outer.new()

    o1.inner.val = 11

    echo o1.inner.val

--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/13130%40github.com.


--
Christian J. Robinson <hep...@gmail.com>

vim-dev ML

unread,
Sep 19, 2023, 8:59:31 PM9/19/23
to vim/vim, vim-dev ML, Your activity

It works fine for me once I correct a couple of bugs:

vim9script

class Inner
public this.val = 13
endclass

class Outer
public this.inner = Inner.new()
endclass

var o1 = Outer.new()

o1.inner.val = 11

echo o1.inner.val

On Tue, Sep 19, 2023 at 6:50 PM errael ***@***.***> wrote:

> Steps to reproduce
>
> In #13118 (comment)
> <https://github.com/vim/vim/issues/13118#issuecomment-1726020513> it says
>
> In the current implementation the read-only object variable behaves like a "final" variable.
>
> The following script gives the error
>
> E1335: Member is not writable: inner
>
> While it's true that inner is not writable, the test does not attempt to
> write to the variable inner. It is writing to the class Inner public
> variable val.
>
> vim9script
>
> class Inner
> public this.val = 13
> endclass
>
> class Outer
> this.inner = Inner.new()
> endclass
>
> var o1 = Outer.new()
>
> o1.inner.var = 11 # <<<<< can't write
>
> Expected behaviour
>
> No error. The object's variable val is written to.
> Version of Vim
>
> 9.0.1916
> Environment
>
> ubuntu
> Logs and stack traces
>
> *No response*
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/vim/vim/issues/13130>.
> You are receiving this because you are subscribed to this thread.Message
> ID: ***@***.***>
>
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups
> "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ***@***.***
> <https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/13130%40github.com?utm_medium=email&utm_source=footer>
> .
>


--
Christian J. Robinson ***@***.***>


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13130/1726731455@github.com>

errael

unread,
Sep 19, 2023, 9:14:19 PM9/19/23
to vim/vim, vim-dev ML, Comment

correct a couple of bugs

this.inner is not supposed to be publicly writable; once this.inner is set, it is not supposed to be changed from outside the class to reference a different object. Only val is public. I don't see a bug in the test script.

The implementation should treat this.inner as final, but it is treating it as const.

class Outer
    this.inner = Inner.new()
endclass


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726740690@github.com>

errael

unread,
Sep 19, 2023, 10:29:17 PM9/19/23
to vim/vim, vim-dev ML, Comment

This only fails at script level, works in :def

vim9script

class Inner
  public this.val = 13
endclass

class Outer
    this.inner: Inner = Inner.new()
endclass

var o1 = Outer.new()

def F()
    o1.inner.val = 11           # OK
    echo o1.inner.val
enddef
F()

o1.inner.var = 11    # <<<<< can't write


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726788276@github.com>

errael

unread,
Sep 19, 2023, 11:10:26 PM9/19/23
to vim/vim, vim-dev ML, Comment

It works fine for me once I correct a couple of bugs


var o1 = Outer.new() o1.inner.var = 11 # <<<<< can't write

-- Christian J. Robinson @.***>

Thanks Christian, I only noticed the change to public this.inner which should not have public. I missed that you caught the var which is supposed to be val.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726817156@github.com>

errael

unread,
Sep 19, 2023, 11:14:26 PM9/19/23
to vim/vim, vim-dev ML, Comment

@yegappan This might be the fix, there's the assumption that 'name..' and 'name.=' can't happen when processing an object or class.

diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -1564,6 +1564,9 @@
                                        om->ocm_name);
                                return NULL;
                            case VIM_ACCESS_READ:
+                               // If [idx] or .key following, read only OK.
+                               if (*p == '[' || *p == '.')
+                                   break;
                                if ((flags & GLV_READ_ONLY) == 0)
                                {
                                    semsg(_(e_member_is_not_writable_str),


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726819553@github.com>

Yegappan Lakshmanan

unread,
Sep 19, 2023, 11:53:22 PM9/19/23
to vim/vim, vim-dev ML, Comment

@yegappan This might be the fix, there's the assumption that 'name..' and 'name.=' can't happen when processing an object or class.

Yes. This looks good to me. I was working on a fix similar to the one below. Can you open a PR
with this change and a test?

diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -1564,6 +1564,9 @@
                                        om->ocm_name);
                                return NULL;
                            case VIM_ACCESS_READ:
+                               // If [idx] or .key following, read only OK.
+                               if (*p == '[' || *p == '.')
+                                   break;
                                if ((flags & GLV_READ_ONLY) == 0)
                                {
                                    semsg(_(e_member_is_not_writable_str),


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726885588@github.com>

errael

unread,
Sep 20, 2023, 1:30:02 AM9/20/23
to vim/vim, vim-dev ML, Comment

Can you open a PR with this change and a test?

#13131


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/13130/1726996663@github.com>

Christian Brabandt

unread,
Sep 20, 2023, 2:16:19 PM9/20/23
to vim/vim, vim-dev ML, Comment

Closed #13130 as completed via 98e68c0.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issue/13130/issue_event/10426409956@github.com>

Reply all
Reply to author
Forward
0 new messages