penne icon indicating copy to clipboard operation
penne copied to clipboard

Return currently uses the same syntax as labels

Open porky11 opened this issue 3 years ago • 3 comments

I think, removing the : after return would be less confusing.

porky11 avatar Oct 24 '22 10:10 porky11

In order to make this change backwards compatible in cases where return is the last statement, the last statement could be implicitly returned just like in Rust.

porky11 avatar Oct 24 '22 11:10 porky11

I just realized it basically already works like this. It's just enforced to call the label before the return value "return".

This is bad for refactoring.

Look at the else_if example. Let's assume, we don't want to return the value directly after jumping to return, but do something afterwards.

For example this:

fn main() -> i32
{
	var x = 33;

	if x == 0
	{
		x = 10;
		goto return;
	}
	else if x == 1
		goto return;
	else if x == 2
	{
		if x == 5
		{
			x = 17;
		}
		else
			goto return;
	}
	else if x == 3
		goto return;
	else
	{
		x = 55;
	}
	
	return:
	var y = x * x;
	x = y - x;
	x
}

It's not possible. Instead you would have to rename every "goto return" to "goto end", also rename label "return" to "end" and add a new label "return" before x.

So being allowed to use "return" as a label for "goto" while enforcing "return" to be the label before the return value at the end, might not be a good idea.

porky11 avatar Oct 24 '22 11:10 porky11

Yes return is a bit special in that it's a label that also signals the start of the return value. As a style convention I've started putting the return value on the same line as the label (return: x), but not all the code samples do this yet. That at least prevents adding statements between the return: and the return value.

I do think you raise a good point about ease of refactoring. Instead of the three steps you mention, it's just two: adding a new label before the added instructions, and changing all the goto statements. And perhaps there is some value to the invariant "goto return; never executes any further statements with side effects".

SLiV9 avatar Oct 26 '22 17:10 SLiV9