From 2f85065239bee6c5156e2e84547c449ec5584c6f Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 24 Jul 2023 11:24:17 +1000 Subject: [PATCH] make sure "use VERSION;" sets the flag needed for "used only once" There's two parts to producing the "used only once" warning: 1. In a lexical scope with WARN_ONCE enabled, new GVs are created with the GVf_MULTI flag off, a second mention of such a name will set that flag. 2. After compilation, if G_WARN_ONCE is set in PL_dowarn, the entire package tree is walked to report names which don't have GVf_MULTI set. In this case G_WARN_ONCE wasn't being set, so the second part didn't happen. This flag is global, so using any other module that happened to enable the WARN_ONCE flag (anything that does C) would allow warnings to be produced after compilation. Fixes #21271 --- op.c | 4 +++- t/lib/warnings/gv | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/op.c b/op.c index df43c2a02d10..e64fc01aa3e4 100644 --- a/op.c +++ b/op.c @@ -7907,8 +7907,10 @@ Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg) if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) PL_hints |= HINT_STRICT_VARS; - if (shortver >= SHORTVER(5, 35)) + if (shortver >= SHORTVER(5, 35)) { free_and_set_cop_warnings(&PL_compiling, pWARN_ALL); + PL_dowarn |= G_WARN_ONCE; + } } /* otherwise they are off */ else { diff --git a/t/lib/warnings/gv b/t/lib/warnings/gv index 2caf2d36b582..4a8c9aabd82e 100644 --- a/t/lib/warnings/gv +++ b/t/lib/warnings/gv @@ -292,3 +292,10 @@ if ($ㄒ) { EXPECT Name "팣칵ぇ::ʎ" used only once: possible typo at - line 11. Use of uninitialized value $팣칵ぇ::ʎ in print at - line 11. +######## +# https://github.com/Perl/perl5/issues/21271 +use v5.36; +no strict; +my $x = $i; +EXPECT +Name "main::i" used only once: possible typo at - line 4.