← Contribution overview

Function exits and return values ยท PR #4621

Handle explicit-return Drops with try/finally cleanup

Merged

Objective

Add Drop handling for explicit returns with the function-scope try/finally cleanup.

Evaluate a unit return expression, or save a non-unit return value, before cleanup runs.

Why it was needed

The earlier function-exit work only covered normal exits, so an explicit return still had no Drop cleanup.

A return from a nested block can leave several active scopes at once. Each scope may have its own values to drop.

The return expression must also run before cleanup.

Main changes

  • For return make_unit(), runs the expression as a statement before returning ().
  • For a non-unit return, saves the value in the function return slot before cleanup.
  • Stores the generated return statement in translated instead of adding it immediately to the compilation context.

Test case

  • drop-explicit-return.rs: Covers return;, unit and non-unit return expressions, and a return from a nested block. It also checks that the non-unit result is 42.

Patch history

The first commit manually walked the active Drop scopes. That code was removed before merge.

The final version uses the structured cleanup from PR #4685 and PR #4711.

View PR on GitHub ← Contribution overview