Skip to content

Commit

Permalink
Warn about shadowing names when importing a lexical sub whose name ma…
Browse files Browse the repository at this point in the history
…tches a package one
  • Loading branch information
leonerd committed Jan 31, 2024
1 parent 12bfaf5 commit 0ee6e75
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Perl_prepare_export_lexical(pTHX)
#define export_lexical(name, sv) S_export_lexical(aTHX_ name, sv)
static void S_export_lexical(pTHX_ SV *name, SV *sv)
{
if(ckWARN(WARN_SHADOW) && SvPVX(name)[0] == '&') {
CV *cv = get_cvn_flags(SvPVX(name)+1, SvCUR(name)-1, GV_NOTQUAL);
if(cv)
warner(packWARN(WARN_SHADOW),
"Lexical import of subroutine %" SVf_QUOTEDPREFIX " masks package subroutine",
SVfARG(name));
}

PADOFFSET off = pad_add_name_sv(name, padadd_STATE, 0, 0);
SvREFCNT_dec(PL_curpad[off]);
PL_curpad[off] = SvREFCNT_inc(sv);
Expand Down
1 change: 1 addition & 0 deletions lib/builtin.t
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ package FetchStoreCounter {
# imported builtins can be unexported
{
package UnimportTest;
no warnings 'shadow';

sub true() { return "true" };

Expand Down
6 changes: 6 additions & 0 deletions pod/perldiag.pod
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,12 @@ The number of items in a hash can be obtained by doing:

scalar(keys %hash);

=item Lexical import of subroutine "%s" masks package subroutine

(W shadow) The current package already contains a subroutine whose name
matches that of a lexically imported subroutine. The latter will become
visible and make the package one inaccessible by name.

=item Lexing code attempted to stuff non-Latin-1 character into Latin-1 input

(F) An extension is attempting to insert text into the current parse
Expand Down
7 changes: 7 additions & 0 deletions t/lib/warnings/builtin
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ use builtin qw(true false);
use builtin ':5.39';
use builtin ':5.39';
EXPECT
########
# builtin.c - import warns of collisions with package subs
use warnings 'shadow';
sub true { "ok" }
use builtin 'true';
EXPECT
Lexical import of subroutine "&true" masks package subroutine at - line 4.

0 comments on commit 0ee6e75

Please sign in to comment.