Skip to content

Bug: a labeled statement drops the function's implicit tail-expression return value (§29.13/§13.5b) #640

Closed

Description

@ehartford

Summary

When a function body contains a 'label: statement, the function's implicit tail expression is no longer returned — the function returns the type's default (empty string / 0) instead. An explicit return works. The label need not be used (an unused-label warning is emitted), and it need not sit on the tail expression itself — any labeled statement in the body triggers it.

Repro

fn f() -> i32:
    let s = 42
    'done:
    s              // tail expression
fn main:
    print(f"[{f()}]")   // prints [0]  — want [42]
fn g() -> str:
    let s = "hello"
    'done:
    "v=" ++ s      // tail expression
// f() returns ""  — want "v=hello"

Both print empty/zero. Replacing the tail with return s / return "v=" ++ s yields the correct value, and goto 'done targeting the label + an explicit return after it also works (verified: [start!]/[nojump!]). So the defect is specifically implicit tail return in a body that contains a labeled statement.

Spec

§29.13 block body / implicit return (the last expression of a block is its value) interacting with §13.5b labels/goto. A labeled statement must not change whether the block's final expression is the block's value.

Impact / discovery

Found writing the §23.3 goto-through-with coverage (#536). Blocks the tail-expression form of any goto-to-forward-label pattern; the #536 goto fixtures must use explicit return as a workaround until this is fixed. Orthogonal to with — reproduces with no with block.

Root-cause direction

Block-tail detection / lowering treats the final expression as statement-position (value discarded) once a NK-label statement is present in the block, so no tail value is propagated as the return. Likely in the block-lowering path that identifies the trailing expression (MirLower block/tail handling) — the label statement shifts or masks the tail slot.

Activity

  1. added a commit that references this issue on Jul 4, 2026
  2. ehartford commented on Jul 5, 2026

    @ehartford
    ContributorAuthor

    Fixed in 6cfddb7. A labeled block in tail position now yields its tail value (was the type default). One MIR site: lower_block_mode materialized the labeled block's tail value instead of returning unit. The type side was already correct. Self-host flip sweep clean by construction — src/lib/build have zero labeled statements; fixpoint held. Verified: int/str tails, goto-to-forward-label + tail, non-Copy Drop tail (drops exactly once). 4 fixtures.

  3. added a commit that references this issue on Jul 5, 2026
    6cfddb7
  4. added a commit that references this issue on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions