Branch: refs/heads/blead
Home: https://github.com/Perl/perl5
Commit: 8f117f4dd48c19e8d7dcf28fe1988eca455e4d0a
https://github.com/Perl/perl5/commit/8f117f4dd48c19e8d7dcf28fe1988eca455e4d0a
Author: Richard Leach <[email protected]>
Date: 2026-09-14 (Mon, 14 Sep 2026)
Changed paths:
M perly.act
M perly.h
M perly.tab
M perly.y
M t/op/my.t
M toke.c
Log Message:
-----------
Apply HINT_BLOCK_SCOPE to lexical variable declarations
At present, any `else {...}` block is wrapped in an ENTER/LEAVE pair, even
if no new scope is warranted, which causes runtime inefficiency. For
example, this block doesn't need a scope but gets one anyway:
```
} else {
return 1;
}
```
However, this behaviour means that an object like `$kaboom` will reliably
be destroyed when the `else` block is exited:
```
} else {
my $kaboom = TickTick->new;
}
```
In contrast, `if () {...}` or `elsif () {...}` blocks default to not
having an `ENTER/LEAVE` pair, which is more efficient but arguably
incorrect, as `$marvin` in this code won't be destroyed when the `if`
block is exited:
```
if ( $x ) {
my $marvin = TickTick->new;
}
```
Exactly when it is destroyed is dependent upon where the next scope
exit happens to be, and this could be some ways away from its block.
This behaviour is also very brittle, as shown in this case where the
no-op `0;` statement causes - via a quirk of parsing - the `if`
block to be assigned an `ENTER/LEAVE` pair:
```
if ( $x ) {
0; my $marvin = TickTick->new;
}
```
Whether a block gains a scope via `ENTER/LEAVE` pair or is just parented
by a `SCOPE` OP (that will be optimized away), is decided by
`Perl_op_scope` on the basis of the `OPf_PARENS` flag on a block's
`LINESEQ` OP. The parser always sets this flag for `else` blocks,
a variety of circumstances determines whether it is set otherwise.
This commit makes two changes:
* Removes the enforced use of the `OPf_PARENS` flag on `else` blocks.
The same rules now apply to `if`, `elsif`, and `else`, hopefully making
destruction behaviour more predictable.
* Changes the tokenizer such that occurrances of `my`, `our`, and `state`
set the `HINT_BLOCK_SCOPE` flag, and the containing block will be wrapped
in an `ENTER/LEAVE` pair.
After this commit, neither of the `if` or `else` block in this example
will have an unnecessary`ENTER/LEAVE` pair:
```
if ($x ) {
return 0;
} else {
return 1;
}
```
And in this example, both objects will be destroyed when the relevant
block is entered:
```
if ( $x ) {
my $kaboom_true = TickTick->new;
} else {
my $kaboom_else = TickTick->new;
}
```
Sadly, both blocks in this example will still have unnecessary scopes,
but fixing that is for a different PR:
```
if ($x ) {
0; return 0;
} else {
0; return 1;
}
```
There are two downsides to this commit:
1. It has the potential to change the destruction timing of objects created
in an `if` block and assigned to a lexical declared within the block, if
the block hasn't ended up with an `ENTER/LEAVE` pair. As noted above though.
that behaviour is very brittle and already sensitive to even minor changes
to the Perl code - and also to minor parser/optimization changes within the
interpreter.
2. The old `else` behaviour meant that the first statement's `NEXTSTATE` COP
stays in place and consequently any error messages arising from the first
statement mention the correct (or closer) line number. Merging this commit
will make it more likely for the `NEXTSTATE` COP to be optimized away.
Historically, this would have made error messages for the first statement
within an `else` block unhelpful in the same way that errors for
`if`/`elsif` blocks have been: the reported line number would have been for
the position in source code where the `if` or `elsif` keyword keyword
occurred, which could be many lines away.
However, 4105c196494d62d0d501b87387dc2778c4d8b776 has recently made
`Perl_closest_caller` more capable at finding `ex-NEXTSTATE` OPs, which
should blunt the potential for increased line number inaccuracies.
Commit: b81b161e1242ac6e82e16b559511b2fd99434369
https://github.com/Perl/perl5/commit/b81b161e1242ac6e82e16b559511b2fd99434369
Author: Richard Leach <[email protected]>
Date: 2026-09-14 (Mon, 14 Sep 2026)
Changed paths:
M ext/B/t/optree_check.t
M ext/B/t/optree_for.t
M ext/B/t/optree_misc.t
M ext/B/t/optree_samples.t
M ext/B/t/optree_sort.t
M ext/B/t/optree_varinit.t
Log Message:
-----------
Adjust B tests to match new scoping arrangements
Commit: 3f8d2509f58144aa50f6c1e39edd13c610419b10
https://github.com/Perl/perl5/commit/3f8d2509f58144aa50f6c1e39edd13c610419b10
Author: Richard Leach <[email protected]>
Date: 2026-09-14 (Mon, 14 Sep 2026)
Changed paths:
M peep.c
M t/perf/opcount.t
Log Message:
-----------
peep.c Optimize away empty cond_expr blocks when safe to do so
Previous commits changed the look of empty `else {}` optrees. This commit
restores the interpreter's ability to optimize them away.
Working on fixing that revealed that empty branches were incorrectly
optimized away in undefined context. In that situation, the `OP_STUB`
must be retained so that the interpreter can push `&PL_undef` onto the
stack.
(Specifically, the check being performed was for the context to _not_
match `OPf_WANT_SCALAR`, whereas the correct check is for the context
to be either of`OPf_WANT_VOID` or `OPf_WANT_LIST`.)
This commit therefore also corrects the context check and modifies
_t/perf/opcount.t_ to verify all four possible branch contexts.
Commit: 60a156d26843b075366d9f7041e368dcada83967
https://github.com/Perl/perl5/commit/60a156d26843b075366d9f7041e368dcada83967
Author: Richard Leach <[email protected]>
Date: 2026-09-14 (Mon, 14 Sep 2026)
Changed paths:
M t/op/defer.t
M t/run/todo.t
M toke.c
Log Message:
-----------
Apply HINT_BLOCK_SCOPE to `defer` statement parsing
`defer` blocks run during scope exit, but if such a `defer` block is
itself the only thing inside a block, creation of a scope for the outer
block could have been erroneously skipped. This would result in the
defer code being run at some time other than when the author intended.
This example, taken from https://github.com/Perl/perl5/issues/20491
should output "first\nsecond", but previously did not:
do {
defer { say "first" };
};
say "second";
This commit sets the `HINT_BLOCK_SCOPE` bit when the `defer` keyword is
encountered, which should ensure that the containing block gets wrapped
in an `ENTER/LEAVE` pair, meaning that the correct outer scoping occurs.
Compare: https://github.com/Perl/perl5/compare/66359281f51d...60a156d26843
To unsubscribe from these emails, change your notification settings at
https://github.com/Perl/perl5/settings/notifications