std: `SliceIndex<str>` missing for `std::ops` ranges, so generic `R: SliceIndex<str>` bounds fail

Open Beginner friendly
#269 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
compilers

Research direction

Read the existing SliceIndex<str> impls in libs/relax/compiler/std/core/str/traits_impls.rs (lines 63, 88, 113, 138) next to the StrIndex trait and its impls in libs/relax/compiler/std/core/str/mod.rs from line 219, and compare with how real std covers ops ranges. Then add SliceIndex<str> impls for the std::ops range types, delegating to the same boundary checks the std::range impls and StrIndex use. Done looks like the reproducer in the issue compiling and printing el, plus the existing str indexing tests still passing.

Written by the indexing model from the issue text.

Description

On Relax 953d174a (macOS arm64), a generic bound on SliceIndex<str> fails for std::ops range types, although direct indexing works.

Reproduce

Cargo.toml

[package]
name = "repro-sliceindex"
version = "0.1.0"
edition = "2024"

[workspace]

src/main.rs

use std::slice::SliceIndex;

fn text_slice<R: SliceIndex<str, Output = str>>(s: &str, r: R) -> &str {
    s.get(r).unwrap_or("")
}

fn main() {
    let r: std::ops::Range<usize> = 1..3;
    println!("{}", text_slice("hello", r));
}

Expected: prints el under rustc 1.100 (and edition 2021 behaves the same).

Actual:

src/main.rs:10:20: E0277: the trait bound `Range<usize>: SliceIndex<str>` is not satisfied

&"hello"[1..3] works, so indexing itself is fine; the trait bound is not.

Where it points

Relax std implements SliceIndex<str> only for the new std::range types:

  • libs/relax/compiler/std/core/str/traits_impls.rs:63 (crate::range::Range<usize>), :88 (RangeInclusive), :113 (RangeFrom), :138 (RangeToInclusive).

The std::ops range types take the separate StrIndex trait instead (libs/relax/compiler/std/core/str/mod.rs:219 and following), which is what the indexing operator uses. Real std has impl SliceIndex<str> for ops::Range<usize> and the other ops ranges.

This breaks any generic API bounded on SliceIndex<str>; it shows up in a vendored text-area widget whose fn text_slice<R: SliceIndex<str, Output = str>>(...) is called with 1..3.

Fix: add the SliceIndex<str> impls for the ops range types, delegating to the same boundary checks / StrIndex methods.

Dominant language
Rust
Stars
601
Forks
34
Avg merge
1h 58m
Merged PRs (30d)
45

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from makepad/relax

All issues in makepad/relax

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.