Skip to content

str range indexing (s[2..], s[..j]) yields a value nothing accepts: let t: str = s[2..] is invalid MIR, print(s[2..]) and .replace fail inference #1587

Closed

Description

@ehartford

Found while writing a one-liner (a with -e that slices a line by find positions — the pain is the priority, per docs/improve_oneliners.md).

Repro

let s = "abcd"
let body = s[2..]
print(body)
error: cannot infer type parameter 'T' for this call; the argument's shape does not match the parameter type that mentions 'T'

Same for print(s[..j]) and body.replace("c","x") (unknown/inference failure). With an annotation the compiler crashes its own validator:

let s = "abcd"
let body: str = s[2..]
error: invalid MIR before codegen: use rvalue type is incompatible with assign destination (dest ty=16 kind=5, src ty=14 kind=4)

Same on the pinned seed, the installed compiler (v0.15.2.1, main ae609cd) and out/release — not a regression.

Expected

Spec §4.8a defines range indexing for arrays/Vecs (arr[1..3] is a []T view) and §15 gives str .slice(a, b); it does not say what str[range] is. Two conforming outcomes, and the current behavior is neither:

  1. str[a..b] is the substring view (&str, ephemeral, same origin rules as arr[a..b]): the mission's spelling — the compiler already knows the .slice(a, b) it would infer. print, .replace, .len, let t: str = … (materializes under the owned-value demand, D22) all work.
  2. It is rejected at Sema with a fix-it to s.slice(a, b).

An "invalid MIR before codegen" and a generic-inference failure on a well-typed program are the compiler's fault either way (No Silent Fallbacks: a validator BUG on user code is a defect, not a diagnostic). My prediction of the ruling is (1); needs Eric's yes for the spec sentence.

Workaround

s.slice(i, s.len()) / s.slice(0, j).

Activity

  1. added
    bug
    on Sep 23, 2026
  2. ehartford commented on Sep 28, 2026

    @ehartford
    ContributorAuthor

    Reopened: 9e45aa8 (#1812) made s[a..b] a &str view with range and UTF-8 checks, but returning a slice (fn tail(s: &str) -> &str: s[1..]) is still refused until &str is a {ptr,len} value — #1810, on branch fix-strview (b511a8fc), which lands after #1819. Closes with that layer.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions