Skip to content

Commit 858ffa2

Browse files
committed
Refuse to create a class if its package already contains a non-empty @isa array
1 parent 032970b commit 858ffa2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

class.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ Perl_class_setup_stash(pTHX_ HV *stash)
251251
HEKfARG(HvNAME_HEK(stash)));
252252
}
253253

254+
{
255+
SV *isaname = newSVpvf("%" HEKf "::ISA", HvNAME_HEK(stash));
256+
sv_2mortal(isaname);
257+
258+
AV *isa = get_av(SvPV_nolen(isaname), (SvFLAGS(isaname) & SVf_UTF8));
259+
260+
if(isa && av_count(isa) > 0)
261+
croak("Cannot create class %" HEKf " as it already has a non-empty @ISA",
262+
HvNAME_HEK(stash));
263+
}
264+
254265
char *classname = HvNAME(stash);
255266
U32 nameflags = HvNAMEUTF8(stash) ? SVf_UTF8 : 0;
256267

pod/perldiag.pod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,12 @@ no legal conversion from that type of reference to a typeglob.
713713
(P) Perl detected an attempt to copy a value to an internal type that cannot
714714
be directly assigned to.
715715

716+
=item Cannot create class %s as it already has a non-empty @ISA
717+
718+
(F) An attempt was made to create a class out of a package that already has
719+
an C<@ISA> array, and the array is not empty. This is not permitted, as it
720+
would lead to a class with inconsistent inheritance.
721+
716722
=item Cannot find encoding "%s"
717723

718724
(S io) You tried to apply an encoding that did not exist to a filehandle,

t/lib/croak/class

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,10 @@ class XXX {}
8080
push @XXX::ISA, q(Another);
8181
EXPECT
8282
Modification of a read-only value attempted at - line 4.
83+
########
84+
no warnings 'experimental::class';
85+
use feature 'class';
86+
BEGIN { push @XXX::ISA, q(Another); }
87+
class XXX {}
88+
EXPECT
89+
Cannot create class XXX as it already has a non-empty @ISA at - line 4.

0 commit comments

Comments
 (0)