Skip to content

Optimise away empty else {} blocks #22053

Open
@richardleach

Description

@richardleach

Description
@rjbs suggested on #p5p:

snowdrop:~$ perl -MO=Concise -E 'if ($x) { say 1 }'
8  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
-     <1> null vK/1 ->8
4        <|> and(other->5) vK/1 ->8
-           <1> ex-rv2sv sK/1 ->4
3              <$> gvsv(*x) s ->4
-           <@> scope vK ->-
-              <;> ex-nextstate(main 4 -e:1) v:%,us,fea=15 ->5
7              <@> say vK ->8
5                 <0> pushmark s ->6
6                 <$> const(IV 1) s ->7
-e syntax OK
snowdrop:~$ perl -MO=Concise -E 'if ($x) { say 1 } else {} '
8  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
-     <1> null vK/1 ->8
4        <|> cond_expr(other->5) vK/1 ->9
-           <1> ex-rv2sv sK/1 ->4
3              <$> gvsv(*x) s ->4
-           <@> scope vK ->-
-              <;> ex-nextstate(main 4 -e:1) v:%,us,fea=15 ->5
7              <@> say vK ->8
5                 <0> pushmark s ->6
6                 <$> const(IV 1) s ->7
b           <@> leave vK ->8
9              <0> enter v ->a
a              <0> stub vP ->b

"I bet the subtree starting at (b) in the second dump could be optimized away. Then you could always put "... } else { (comment only) }" at the end of an if/elsif block."

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions