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

Undefined subroutine &%s called, close to label '%s' #22860

Open
wants to merge 2 commits into
base: blead
Choose a base branch
from

Conversation

richardleach
Copy link
Contributor

#17839 requested a compile-time
warning for the situation whereby a single colon is accdentally typed
as a package separator when calling a function. For example:

package BOOP;
sub beep;
package main;
BOOP:beep();  # Meant to be BOOP::beep();

However, because of both Perl's syntax and the potential to populate the
stash at runtime, this falls somewhere between very difficult and
impossible. As an alternative, some enhanced fatal error wording was
requested and these commits attempt to provide that.

The above example would previously die with the message:

Undefined subroutine &main::beep called at ... line 4.

Now it dies with the message:

Undefined subroutine &main::beep called, close to label 'BOOP' at ... line 4.

For some of the same reasons mentioned, distinguishing this typo from
other errors at runtime - such as the target subroutine not being
present at all - is also nigh on impossible. The hope is that the
error message will give some additional clue when the error is the
result of a typo, without distracting the user in all other cases.

As part of these commits, some common DIE() calls in pp_entersub were
extracted into a new helper function, S_croak_undefined_subroutine, to
avoid adding (and slightly reduce) cold code bloating in pp_entersub.

@richardleach richardleach force-pushed the hydahy/17839_missing_colon branch from 01a4fbf to 73a4618 Compare December 14, 2024 23:35
Copy link
Contributor

@iabyn iabyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to see this split into two commits - the first to extract out the DIEes into a separate helper function, and the second to add the LABEL functionality.

I think the perldiag text (and perldelta) could be improved: give an actual example of the problem it's trying to flag, e.g. "Cfoo:bar() would be parsed as a label followed by an unqualified function name: C<foo: bar()>".

I'm somewhat twitchy that the message is generated even if the label doesn't immediately precede the function call but is merely in the same statement; E.g.

FOO: if ($x > 1 && foo()) { ... }

@richardleach
Copy link
Contributor Author

I'd prefer to see this split into two commits - the first to extract out the DIEes into a separate helper function, and the second to add the LABEL functionality.

I think the perldiag text (and perldelta) could be improved: give an actual example of the problem it's trying to flag, e.g. "Cfoo:bar() would be parsed as a label followed by an unqualified function name: C<foo: bar()>".

I'll make those changes now.

I'm somewhat twitchy that the message is generated even if the label doesn't immediately precede the function call but is merely in the same statement;

Yes, I'm in two minds about it for that reason too. If, on balance, it looks like this will add more confusion than clarity, there's no point in adding it.

In `pp_entersub`, when trying to find the subroutine `CV` to execute,
there are three nearby code paths that can result in a `DIE()`. This
commit extracts the DIE() logic to a helper subroutine,
`S_croak_undefined_subroutine`. This is partly in anticipation of
additional warning logic, but also generally reduces the size of this
hot function.
Perl#17839 requested a compile-time
warning for the situation whereby a single colon is accdentally typed
as a package separator when calling a function. For example:
```
package BOOP;
sub beep;
package main;
BOOP:beep();  # Meant to be BOOP::beep();
```
However, because of both Perl's syntax and the potential to populate the
stash at runtime, this falls somewhere between very difficult and
impossible. As an alternative, some enhanced fatal error wording was
requested and these commits attempt to provide that.

The above example would previously die with the message:
```
Undefined subroutine &main::beep called at ... line 4.
```
Now it dies with the message:
```
Undefined subroutine &main::beep called, close to label 'BOOP' at ... line 4.
```

For some of the same reasons mentioned, distinguishing this typo from
other errors at runtime - such as the target subroutine not being
present at all - is also nigh on impossible. The hope is that the
error message will give some additional clue when the error is the
result of a typo, without distracting the user in all other cases.
@richardleach richardleach force-pushed the hydahy/17839_missing_colon branch from 73a4618 to 46adb82 Compare December 16, 2024 22:28
@iabyn
Copy link
Contributor

iabyn commented Dec 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants