Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class.c: Complain if :reader attribute is asked to create a method with an invalid name #22776

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading