Skip to content

Commit f0e1638

Browse files
committed
class.c: Complain if :reader attribute is asked to create a method with an invalid name
1 parent 429dcba commit f0e1638

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

class.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,9 @@ apply_field_attribute_reader(pTHX_ PADNAME *pn, SV *value)
979979
/* Default to name minus the sigil */
980980
value = newSVpvn_utf8(PadnamePV(pn) + 1, PadnameLEN(pn) - 1, PadnameUTF8(pn));
981981

982+
if(!valid_identifier_sv(value))
983+
croak("%" SVf_QUOTEDPREFIX " is not a valid name for a generated method", value);
984+
982985
PADOFFSET fieldix = PadnameFIELDINFO(pn)->fieldix;
983986

984987
I32 floor_ix = start_subparse(FALSE, 0);

pod/perldiag.pod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,13 @@ should use the printf/sprintf functions instead.
34993499
overload::constant needs to be a code reference. Either
35003500
an anonymous subroutine, or a reference to a subroutine.
35013501

3502+
=item "%s" is not a valid name for a generated method
3503+
3504+
(F) A custom method name was passed to an attribute on a field, such as
3505+
C<:reader>, which creates a new method. These generated methods must have
3506+
valid names, but the value given to the attribute would result in an
3507+
invalid name.
3508+
35023509
=item '%s' is not an overloadable type
35033510

35043511
(W overload) You tried to overload a constant type the overload package is

t/lib/croak/class

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,13 @@ class C { BEGIN { C->new; } };
155155
EXPECT
156156
Cannot create an object of incomplete class "C" at - line 4.
157157
BEGIN failed--compilation aborted at - line 4.
158+
########
159+
# Invalid method name for :reader attribute
160+
use v5.36;
161+
use feature 'class';
162+
no warnings 'experimental::class';
163+
class XXX {
164+
field $x :reader(abc-def);
165+
}
166+
EXPECT
167+
"abc-def" is not a valid name for a generated method at - line 6.

0 commit comments

Comments
 (0)