]> Cypherpunks repositories - gostls13.git/commit
flag: panic if a flag is defined after being set
authorKeith Randall <khr@golang.org>
Wed, 29 Mar 2023 16:12:20 +0000 (09:12 -0700)
committerKeith Randall <khr@golang.org>
Fri, 21 Apr 2023 22:25:02 +0000 (22:25 +0000)
commit00401835c1d5122a84f0500e48655cd787402f3f
treecba83d9f44f42e720a854b2491f3d6332794b0a2
parent1c17981f4acaedf1c262fd289c7945a521b43a2f
flag: panic if a flag is defined after being set

As part of developing #57411, we ran into cases where a flag was
defined in one package init and Set in another package init, and there
was no init ordering implied by the spec between those two
packages. Changes in initialization ordering as part of #57411 caused
a Set to happen before the definition, which makes the Set silently
fail.

This CL makes the Set fail loudly in that situation.

Currently Set *does* fail kinda quietly in that situation, in that it
returns an error. (It seems that no one checks the error from Set,
at least for string flags.) Ian suggsted that instead we panic at
the definition site if there was previously a Set called on that
(at the time undefined) flag.

So Set on an undefined flag is ok and returns an error (as before),
but defining a flag which has already been Set causes a panic.  (The
API for flag definition has no way to return an error, and does
already panic in some situations like a duplicate definition.)

Update #57411

Change-Id: I39b5a49006f9469de0b7f3fe092afe3a352e4fcb
Reviewed-on: https://go-review.googlesource.com/c/go/+/480215
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
src/flag/flag.go
src/flag/flag_test.go