Skip to content

Returning a return strange behaviour #22838

@jorgeluismireles

Description

@jorgeluismireles

Describe the bug

When a programmer by mistake use a twice unnecessary return statement within a return if { } else { } block, the code is accepted and the result is not logic.

Reproduction Steps

fn empty(s string) !string {
	return if s != '' {
		s
	} else {
		//error('empty') // This works
		return error('empty') // This "double" return FAILS
	}
}

str_1 := empty('something') or {
	assert false, 'something is not empty!'
	return
}
assert str_1 == 'something'

str_2 := empty('') or {
	println('expected error ${err}')
	return
}
assert false, 'invalid accepted ${str_2}'

Expected Behavior

Maybe first prevent the second return can be compiled or that return return error('message') worked as return error('message').

Current Behavior

code.v:20:NaN: fn main.main: assert false, 'invalid accepted $str_2'
      message: invalid accepted �

Seems the error is not returned but is returned a pointer to return error('something').

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 5092626

Environment details (OS name and version, etc.)

https://play.vlang.io/p/6c8f09ab31

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21283

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions