Skip to content

Remove ?Sized bound from Idx in Index and IndexMut#157736

Closed
benw wants to merge 1 commit into
rust-lang:mainfrom
benw:main
Closed

Remove ?Sized bound from Idx in Index and IndexMut#157736
benw wants to merge 1 commit into
rust-lang:mainfrom
benw:main

Conversation

@benw

@benw benw commented Jun 11, 2026

Copy link
Copy Markdown

Idx: ?Sized is useless because the trait methods pass the index by value, so it's impossible to impl with an unsized Idx type.

Removing this makes it a little easier for learners to understand the intent of the Index and IndexMut traits.

`Idx: ?Sized` is useless because the trait methods pass the index
by value, so it's impossible to impl with an unsized Idx type.

Removing this makes it a little easier for learners to understand
the intent of the Index and IndexMut traits.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 11, 2026
@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jhpratt (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 10 candidates
  • Random selection from Mark-Simulacrum, clarfonthey, jhpratt

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
- error: aborting due to 2 previous errors
+ error[E0277]: the size for values of type `str` cannot be known at compilation time
+   --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:16:5
+    |
+ LL |     fn index(&self, _: str) -> &Self::Output {
+    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+    |
+    = help: the trait `Sized` is not implemented for `str`
+ note: required by an implicit `Sized` bound in `Index`
+   --> $SRC_DIR/core/src/ops/index.rs:LL:COL
---
+ 
+ error[E0277]: the size for values of type `str` cannot be known at compilation time
+   --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:26:5
+    |
+ LL |     fn index(&self, _: str) -> &Self::Output {
+    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+    |
+    = help: the trait `Sized` is not implemented for `str`
+ note: required by an implicit `Sized` bound in `Index`
+   --> $SRC_DIR/core/src/ops/index.rs:LL:COL
---
-   --> /rustc/FAKE_PREFIX/library/core/src/ops/index.rs:59:0
+ error[E0277]: the size for values of type `str` cannot be known at compilation time
+   --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:16:5
+    |
+ LL |     fn index(&self, _: str) -> &Self::Output {
+    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+    |
+    = help: the trait `Sized` is not implemented for `str`
+ note: required by an implicit `Sized` bound in `Index`
+   --> $SRC_DIR/core/src/ops/index.rs:LL:COL
---
+ 
+ error[E0277]: the size for values of type `str` cannot be known at compilation time
+   --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:26:5
+    |
+ LL |     fn index(&self, _: str) -> &Self::Output {
+    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+    |
+    = help: the trait `Sized` is not implemented for `str`
+ note: required by an implicit `Sized` bound in `Index`
+   --> $SRC_DIR/core/src/ops/index.rs:LL:COL
---
To only update this specific test, also pass `--test-args resolve/issue-113808-invalid-unused-qualifications-suggestion.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: unnecessary qualification
##[error]  --> /checkout/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.rs:13:6
   |
---
---- [ui] tests/ui/unsized-locals/unsized-index.rs stdout ----

error: test compilation failed although it shouldn't!
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/unsized-locals/unsized-index.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "-O" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/unsized-locals/unsized-index/a" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0277]: the size for values of type `str` cannot be known at compilation time
##[error]  --> /checkout/tests/ui/unsized-locals/unsized-index.rs:12:5
   |
---

error[E0277]: the size for values of type `str` cannot be known at compilation time
##[error]  --> /checkout/tests/ui/unsized-locals/unsized-index.rs:17:6
   |
LL | impl ops::IndexMut<str> for A {
   |      ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `IndexMut`
  --> /rustc/FAKE_PREFIX/library/core/src/ops/index.rs:170:0

error[E0277]: the size for values of type `str` cannot be known at compilation time
##[error]  --> /checkout/tests/ui/unsized-locals/unsized-index.rs:26:29
   |
LL |     assert_eq!(&(), a.index(*s));
   |                       ----- ^^ doesn't have a size known at compile-time
   |                       |
   |                       required by a bound introduced by this call
   |
   = help: the trait `Sized` is not implemented for `str`
note: required by a bound in `std::ops::Index::index`

@jhpratt

jhpratt commented Jun 11, 2026

Copy link
Copy Markdown
Member

See #48055 for supporting unsized function parameters. While there is RFC 3829 that may remove that (it's not clear whether it's only locals or also arguments), that RFC is not yet accepted. Because unsized locals are still formally accepted, I'll be closing this. With that said, don't be discouraged! You're absolutely right that on stable the relaxed bound is unnecessary.

@jhpratt jhpratt closed this Jun 11, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants