Subject: [BUG] functions can't create global readonly variables
From: Martijn Dekker <[email-masked]>
To: [email-masked]
Date: Sat, 02 Jan 2016 04:27:15 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

It is apparently impossible to create global readonly variables from
within a function.

If a variable is created with a command like
    readonly var=value
then the entire variable gets a function-local scope, as if 'local' were
used.

If a global variable is set first, then set to readonly with 'readonly'
or 'typeset -r', then the read-only attribute only lasts within the
function, and the global variable remains read/write.

I don't know if this is intended behaviour for native zsh mode; the
'zshbuiltins' man page does not mention anything about this. But it is
certainly a bug for POSIX/'emulate sh' mode.

Steps to reproduce:

$ fn() { readonly var=foo; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var

$ var=writtento
$ echo $var
writtento
$ fn() { var=foo; readonly var; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var
foo
$ var=writtento
$ echo $var
writtento

Confirmed in zsh 4.1.1 through current.

Thanks,

- Martijn