You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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:
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.
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.
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.
Activity
Paper cuts: Vec ranges as []T views, let-else return inference, usize…
Sema/MirLower/Codegen: a str range `s[a..b]` is a `&str` view of its …
Sema/MirLower/Codegen: a str range `s[a..b]` is a `&str` view of its …
Sema/MirLower/Codegen: a str range `s[a..b]` is a `&str` view of its …
ehartford commentedon Sep 28, 2026
Reopened: 9e45aa8 (#1812) made
s[a..b]a&strview with range and UTF-8 checks, but returning a slice (fn tail(s: &str) -> &str: s[1..]) is still refused until&stris a{ptr,len}value — #1810, on branch fix-strview (b511a8fc), which lands after #1819. Closes with that layer.FnAbi/TypeLayout/Codegen/CCodegen: a shared `&str` is its own `{ptr, …
Sema: a str range view carries only its base's origins, so `fn tail(s…