Skip to content

Commit

Permalink
class.c: Complain if :reader attribute is asked to create a method wi…
Browse files Browse the repository at this point in the history
…th an invalid name
  • Loading branch information
leonerd committed Nov 24, 2024
1 parent 429dcba commit f0e1638
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,9 @@ apply_field_attribute_reader(pTHX_ PADNAME *pn, SV *value)
/* Default to name minus the sigil */
value = newSVpvn_utf8(PadnamePV(pn) + 1, PadnameLEN(pn) - 1, PadnameUTF8(pn));

if(!valid_identifier_sv(value))
croak("%" SVf_QUOTEDPREFIX " is not a valid name for a generated method", value);

PADOFFSET fieldix = PadnameFIELDINFO(pn)->fieldix;

I32 floor_ix = start_subparse(FALSE, 0);
Expand Down
7 changes: 7 additions & 0 deletions pod/perldiag.pod
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,13 @@ should use the printf/sprintf functions instead.
overload::constant needs to be a code reference. Either
an anonymous subroutine, or a reference to a subroutine.

=item "%s" is not a valid name for a generated method

(F) A custom method name was passed to an attribute on a field, such as
C<:reader>, which creates a new method. These generated methods must have
valid names, but the value given to the attribute would result in an
invalid name.

=item '%s' is not an overloadable type

(W overload) You tried to overload a constant type the overload package is
Expand Down
10 changes: 10 additions & 0 deletions t/lib/croak/class
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,13 @@ class C { BEGIN { C->new; } };
EXPECT
Cannot create an object of incomplete class "C" at - line 4.
BEGIN failed--compilation aborted at - line 4.
########
# Invalid method name for :reader attribute
use v5.36;
use feature 'class';
no warnings 'experimental::class';
class XXX {
field $x :reader(abc-def);
}
EXPECT
"abc-def" is not a valid name for a generated method at - line 6.

0 comments on commit f0e1638

Please sign in to comment.