Skip to content

Commit d848617

Browse files
authored
[spec] Improve Function Return Values section (#4296)
* [spec] Improve Function Return Values section One `return` is required *per execution path*. Don't mention `assert(0)` as that is covered by the `noreturn` case. Clarify return value being an rvalue. * Add missing return example * Do list `assert(0)` It's implementation defined whether it's recognized with type noreturn.
1 parent d2b3099 commit d848617

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

spec/function.dd

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,23 +395,53 @@ $(GNAME OutStatement):
395395

396396
$(H2 $(LNAME2 function-return-values, Function Return Values))
397397

398-
$(P At least one $(DDSUBLINK spec/statement, return-statement, return statement)
399-
is required if the function specifies a return type that is not void,
398+
$(P One $(DDSUBLINK spec/statement, return-statement, return statement) per execution path
399+
is required if the function specifies a return type that is not `void`,
400400
unless:)
401401
$(UL
402-
$(LI the function executes an infinite loop)
403-
$(LI the function executes an $(DDSUBLINK spec/expression, assert-ct,
404-
`assert(0)` statement))
405-
$(LI the function evaluates an expression of type
402+
$(LI the path executes an infinite loop)
403+
$(LI the path evaluates an $(DDSUBLINK spec/expression, assert-ct,
404+
`assert(0)` expression))
405+
$(LI the path evaluates an expression of type
406406
$(DDSUBLINK spec/type, noreturn, `noreturn`))
407-
$(LI the function contains inline assembler code)
407+
$(LI the path contains inline assembler code)
408408
)
409409

410+
$(P Simple forms of the first 3 cases above can be recognized by the compiler,
411+
but not all.)
412+
413+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
414+
---
415+
int error(int i)
416+
{
417+
if (i & 1)
418+
i++;
419+
else
420+
return i;
421+
422+
// Error: no return statement
423+
}
424+
---
425+
)
426+
427+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
428+
---
429+
int f(int i)
430+
{
431+
if (i >= 0)
432+
return i;
433+
434+
assert(0);
435+
// OK, no return needed
436+
}
437+
---
438+
)
410439
$(P Function return values not marked as $(RELATIVE_LINK2 ref-functions, `ref`)
411-
are considered to be rvalues.
440+
are considered to be rvalues by the calling function.
412441
This means they cannot be passed by reference to other functions.
413442
)
414443

444+
415445
$(H2 $(LNAME2 pure-functions, Pure Functions))
416446

417447
$(P Pure functions are annotated with the `pure` attribute.

0 commit comments

Comments
 (0)