From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id C6D8128EC0 for ; Sun, 18 Feb 2024 04:26:58 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1708226818; b=sgIzIna1J7ZHLQr0/IYp5H7h0L9Y5VLLDIWIdtUkQPRPQgQ6gdMAKpA2XFJkV/ZnkgfaNQ1k6G xIlbn2fcw0QWY0OslVZmmIQ9AHolBfW5Canqk/0BAK5Qjta7QiVQjysW3mqYM25RmnTWU8hbSY fK2invoyeF35eCf/8Qcupry+sqvhBZjrCWt0v7svglbLEUrCH8T0zGnc4PL4bSjHvSDJFRFhDP 8zflN11Pq71rU4HuAkOdIVZoW7Ae8ouohkzouZSJ3cGvVHcWx2dKNirRgAFHc5gGa65VrF3MiA qq0yfLYbi2ifhlbgnD7Ppg2f9/1SCNx+BqiNHDmF9FdcLw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ed1-f46.google.com) smtp.remote-ip=209.85.208.46; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1708226818; bh=HO6se+T2Gse7SCaDbFbaD618dkvMVJ49oS7+PvuIyp8=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:To:Subject:Message-ID: Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature:DKIM-Signature; b=QfjEiBevVaqYnvYwGpy7V8SC+kxNRIR9Ld1jkwV1Rpoj6pDLobC0ZaF2jhlC3X1kE5MJ8ASfmu fk/icbOgJjMHblshevYImCiJDonvO+0pYxhRfDA9o/a/2NlDlKYwIXi3yxKdtWYnshcPeDHXNL iRHKIJ+SJ4IrjSJ8Tx/uAoAarHXuWV6nXSb/13nDS+LCaCJDXEmd3ZQPrxk+6+OFlQDw+0aZi8 7kv8uW+Mf4a4DYAcpLdHmtaiZ/GGYoTfkEw7wzXINvI22eBvhOVIB5KV8cgOydbjTjFbX5Po76 Q9IA+Nnfv3rOAQ/UGlMtOlRulOmSap056XEOkL+w2vz/qg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=ip7HMEFFeqZab/CBqUY8RHjrLZQzfs+lpT5CpC6TWvE=; b=NGPnU6pVE4LmCnHjmmrPZ8wjex DhMY85UMfaII5PdW1cP4QQxvzbjbpifraCEDie41MuQvRrFbM8xQmrgHYLj5rPtgl7iEzBhpyDhof oS60QSvi+mmxTb4Su/dEZbA07IAsQ3S+C30+vZrAM+KEJ1e5Z46HjbTnKmbeYVE/lChC/XlnK109G TT/Ios2vyJc7U3akisD3lq0qrfS9l6AvalBlDXtz+uHg5o26dQK66kWCmhGZuj24x2Y0OLmgFtx6L yGZicJaoU2qVJdRdarOsqCoyUPIz2tDy+78PKhxI83smzoYJrHrUEAdQEAFc3VNPnwg5WCXNBCrrY gdQYvcHw==; Received: by zero.zsh.org with local id 1rbXpR-000PZI-57; Sun, 18 Feb 2024 03:26:57 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ed1-f46.google.com) smtp.remote-ip=209.85.208.46; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ed1-f46.google.com ([209.85.208.46]:60816) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rbXot-000PFb-4c; Sun, 18 Feb 2024 03:26:24 +0000 Received: by mail-ed1-f46.google.com with SMTP id 4fb4d7f45d1cf-5645960cd56so173685a12.1 for ; Sat, 17 Feb 2024 19:26:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20230601.gappssmtp.com; s=20230601; t=1708226779; x=1708831579; darn=zsh.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ip7HMEFFeqZab/CBqUY8RHjrLZQzfs+lpT5CpC6TWvE=; b=Z+2C6ff8JcDjzP0eRtbuYL7E6uBvaN5OVLg2dzgtiZUZLAMtGqTdaLPoq+BcQNashg aIga9njek/cHjMTcU5aOm6O/J/H5eChKYohaBBHeq1l7dASU5KfJrUL1B9E74ZYN0I0r m66G+hsoriQB80U1F3AD0kB1iP27bxJ7Af5+QOIjkhqBquqLU1s8xXm1n6dnur5iPdTK mwr8BkJK+35Xne+hhNPbhvMae1S9M9f2W7Fg6F4i52OdhJlJHA6AkN4uObOrOdkuGoiH sJCJd9DlU0veh6HT7H1x2WI3KrYjOyBDD3iN/a2h9EpCK5iNz7epfe4AqrxWe+f42PGp rOWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708226779; x=1708831579; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ip7HMEFFeqZab/CBqUY8RHjrLZQzfs+lpT5CpC6TWvE=; b=e4Ni0O4OAPOGGN96468FRIpGDVx8iq48CUcic9wOPLLBI7dbuBalh4RQjoq6wZ9fgo 2MTKOkC61c8Nu6d7Bxb97r+/0eU12vLPfGqt4bry1yAA5XvxHiNCDol2BB9C2MO+JP4X zkCPMjnqahKBMnIR08RbWBqrDKMLIUzA1cnkh9ET1vKFDY7ifomOjMw506fqaVen/x8O LPbHT3TOtNxvRVlmnTAgMDSVEmaG3dpKweW7zvh6oNPiEGUwJkgpYKo+0jll7EbdBBAy 4pJsrt/+Ho6tMzvgX4LuYnv+bvTVj7T2s/4zDeFs7t2NXx2XzXeraDpPliiQJOsQW9s2 hBIw== X-Gm-Message-State: AOJu0Yz07uLBT4OyMrb8D+hJhYHLIWgoeIfZRczFMAj/JILGo554u7pH 2cCJmv/IIEXPxAtWCrdU6jgSTfDtb+sOAoqdXy2Z0HCj6cMpKUDySSlEKjPU9vP8CpQWg0c7uVC 1MIUFnaSg729ukiri5auDIxvHckIMY9ptYVudjpMFSdKfmmQ= X-Google-Smtp-Source: AGHT+IGo6VmTVkSpfumDgMEhwdvqHa6NA02plRHFGQ/QyQcgLS+YnmXPEgBOOc2IvdIpjjlPfBJhtdq6MN4XTxVu/nM= X-Received: by 2002:a17:906:e2d5:b0:a3e:5c65:89b1 with SMTP id gr21-20020a170906e2d500b00a3e5c6589b1mr613583ejb.27.1708226779120; Sat, 17 Feb 2024 19:26:19 -0800 (PST) MIME-Version: 1.0 References: <67689-1675827940.088548@BxvG.D9_b.7RzI> <12608-1675903622.800470@Xj82.e3y1.svhG> <66045-1675975796.128039@FBF_.0yMO.Y8fk> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> In-Reply-To: From: Bart Schaefer Date: Sat, 17 Feb 2024 19:26:07 -0800 Message-ID: Subject: Up-scope named references, vs. ksh To: Zsh hackers list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 52555 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Re-establishing the original examples: On Sat, Feb 10, 2024 at 11:00=E2=80=AFPM Stephane Chazelas wrote: > > $ ./Src/zsh -c 'function f { typeset -n ref=3D$1; typeset var=3Dfoo; ref= =3DX; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"' > X var X > > Compare with ksh: > > $ ksh -c 'function f { typeset -n ref=3D$1; typeset var=3Dfoo; ref=3DX; e= cho "$ref ${!ref} $var"; }; f var; echo "$var"' > X var foo > X On Sun, Feb 11, 2024 at 8:14=E2=80=AFAM Bart Schaefer wrote: > > On Sun, Feb 11, 2024 at 1:00=E2=80=AFAM Stephane Chazelas wrote: > > > > One difference with ksh93: if the target variable was not set at > > the time of the "typeset -n ref=3Dtarget", then when ref is > > assigned it may end up refering to a local target [...] > > It can be worked around with a typeset -g $1 before the typeset -n var= =3D$1 > > Is that really sufficient? Might the -g not select the wrong scope if > the string $1 was passed down a couple of layers? Here's what I'm concerned about: A) Src/zsh -c 'function f { typeset -n ref; ref=3D$1; typeset var=3Dfoo; ref=3DX; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"' Note the subtle difference of separating the declaration of "ref" from assignment to it. Then consider this variation: B) Src/zsh -c 'function f { typeset -n ref; typeset var=3Dfoo; ref=3D$1; ref=3DX; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"' Now the assignment is after the declaration of the local. And finally: C) Src/zsh -c 'function f { typeset var=3Dfoo; typeset -n ref=3D$1; ref=3DX; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"' Now the declaration+assignment of the nameref is after the local. At least for the versions I have installed on Ubuntu 20.04, and with parens removed from (!) as appropriate, A) ksh prints "X var foo\nX"; mksh prints an error and does not create a nameref; Src/zsh prints "X var X". B) ksh prints "X var X"; mksh prints an error and does not create a nameref; Src/zsh prints "X var X". C) ksh prints "X var foo\nX"; mksh prints "X var X"; Src/zsh prints "X var= X". So in part C, ksh "knows" that $1 is not just a string but an object that belongs to a surrounding scope. That's not going to happen in zsh for a long time, if ever. It's interesting that it matters that the assignment appears in the argument of "typeset" e.g. part C vs. part B. Incidentally if I change the delcaration to typeset -n ref=3D then ksh crashes in parts A and B. The behavior of mksh and Src/zsh is unchanged. Adding the "typeset -g $1" changes the outcome for zsh only for part A, where it prints "X var foo\nX" as Stephane indicated. (The other shells don't support -g.) Given that it's not possible to fix part C for zsh, and zsh agrees with ksh on part B and with mksh on B and C, is it worth making an effort to fix Stephane's original example along with part A ? > Seems to me you > have to avoid re-using a variable regardless, especially if you do > > typeset -n var=3D > > (create what I've been calling a "placeholder") and then assign to var > in some nested scope. I have not dived into what happens in ksh if the assignments in A and B happen in a nested function call. > This may particularly be true of the "for var in ..." special case for na= merefs. The potential for confusion here seems large. If I do typeset -n up=3D$1 typeset var=3Dfoo for up in $2 $3 $4 $5; do ... what scope am I applying to each of the words in $2, $3, ... ? (E.g., suppose $3 is "var" instead of $1.) Does it change if I do typeset -n up instead? Should that act like part B where the declaration is separate from the assignment? What if I do for up in "$@" there? The ksh implementation seems both inconsistent and slightly magical= . Anyway, the way to "fix" this is to implicitly perform the "typeset -g" when assigning to the nameref, and if that creates a new parameter then mark it unset. But that still may put the new parameter at global scope rather than at the scope from which $1 was passed; the expansion of $1 is just a string like any other scalar, and there's no magic to change that. It seems as though the more consistent behavior is what zsh is already doing. I'm unsure whether doing the implicit typeset only in ksh emulation mode is worthwhile as it gets us only partway there. Adding the "typeset -g" changes the behavior of a large number of tests in K01, too. Thoughts?