Skip to content

Commit badeffa

Browse files
committed
Quiet the warning about field shadowing when it's about same-named fields of another (unit) class that happens to be in the same scope
1 parent b2583b0 commit badeffa

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pad.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ Perl_pad_add_name_pvn(pTHX_ const char *namepv, STRLEN namelen,
617617
ENTER;
618618
SAVEFREEPADNAME(name); /* in case of fatal warnings */
619619
/* check for duplicate declaration */
620-
pad_check_dup(name, flags & padadd_OUR, ourstash);
620+
pad_check_dup(name, flags & (padadd_OUR|padadd_FIELD), ourstash);
621621
PadnameREFCNT_inc(name);
622622
LEAVE;
623623
}
@@ -869,12 +869,13 @@ S_pad_check_dup(pTHX_ PADNAME *name, U32 flags, const HV *ourstash)
869869
PADNAME **svp;
870870
PADOFFSET top, off;
871871
const U32 is_our = flags & padadd_OUR;
872+
bool is_field = flags & padadd_FIELD;
872873

873874
PERL_ARGS_ASSERT_PAD_CHECK_DUP;
874875

875876
ASSERT_CURPAD_ACTIVE("pad_check_dup");
876877

877-
assert((flags & ~padadd_OUR) == 0);
878+
assert((flags & ~(padadd_OUR|padadd_FIELD)) == 0);
878879

879880
if (PadnamelistMAX(PL_comppad_name) < 0 || !ckWARN(WARN_SHADOW))
880881
return; /* nothing to check */
@@ -893,6 +894,9 @@ S_pad_check_dup(pTHX_ PADNAME *name, U32 flags, const HV *ourstash)
893894
{
894895
if (is_our && (PadnameIsOUR(pn)))
895896
break; /* "our" masking "our" */
897+
if (is_field && PadnameIsFIELD(pn) &&
898+
PadnameFIELDINFO(pn)->fieldstash != PL_curstash)
899+
break; /* field of a different class */
896900
/* diag_listed_as: "%s" variable %s masks earlier declaration in same %s */
897901
Perl_warner(aTHX_ packWARN(WARN_SHADOW),
898902
"\"%s\" %s %" PNf " masks earlier declaration in same %s",

t/class/field.t

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,19 @@ no warnings 'experimental::class';
114114
is($obj->count, 3, '$obj->count after invoking method-closure x 3');
115115
}
116116

117+
# fields of multiple unit classes are distinct
118+
{
119+
class Test6::A;
120+
field $x; ADJUST { $x = "A" }
121+
method m { return "unit-$x" }
122+
123+
class Test6::B;
124+
field $x; ADJUST { $x = "B" }
125+
method m { return "unit-$x" }
126+
127+
package main;
128+
ok(eq_array([Test6::A->new->m, Test6::B->new->m], ["unit-A", "unit-B"]),
129+
'Fields of multiple unit classes remain distinct');
130+
}
131+
117132
done_testing;

0 commit comments

Comments
 (0)