Skip to content

&str points at a string header, so a str range view (D71 §4.8a) cannot be returned: make &str its own {ptr, len} #1810

Closed

Description

@ehartford

Spec

D71 / §4.8a: a range of a str or &str is a &str view "with the origin and ephemerality of any view (§5, §21.1)". So fn tail(s: &str) -> &str: s[1..] is valid: the view's origin is the parameter s (§21.1 Rule 6).

State after #1587 (branch fix-lang, 8832b058)

A &str is a pointer to a str header {ptr, len}. A range view has no header of its own, so lower_slice_expr builds {ptr+a, b-a} into a hidden str local of the frame and the view is a reference to it. Inside the function every use works (print, methods, compare, owned demand, invalidation). Returning it would hand out a pointer into the dead frame, so Sema gives the view a frame-local sentinel origin (syms.str_range_view) and refuses the return by name:

error: a string slice cannot be returned yet (#1587): ...

(test/compile_errors/err_1587_str_range_returned*.w pin it; they flip to behavior tests when this lands.)

What the ruling needs

&str represented as {ptr, len} by value (a fat view, like []u8), so a range view is a value and carries only its base's origins. Touches every &str producer and consumer: FnAbi/PassMode for &str params and returns, build_str_ref_from_value and the with_*_str_ref runtime seams, c_import marshalling (#1589's D47 path), element views of Vec[str] (#925), pattern bindings, str_data in rt_core. Then drop the sentinel origin and the hidden header.

Question

Land #1587's interim refusal now (loud, one rule) and do the representation change as its own campaign? Recommended: yes — the refusal is not a silent fallback, every non-returning use already matches the ruling, and the representation change is ABI work that must be alone in its battery.

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