Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeXxx<&T> should implement RangeBounds<T> even if T is unsized #107196

Open
SOF3 opened this issue Jan 22, 2023 · 3 comments
Open

RangeXxx<&T> should implement RangeBounds<T> even if T is unsized #107196

SOF3 opened this issue Jan 22, 2023 · 3 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SOF3
Copy link
Contributor

SOF3 commented Jan 22, 2023

I tried this code:

use std::collections::BTreeMap;
pub fn foo(map: &BTreeMap<String, i32>) -> impl Iterator<Item = (&String, &i32)> {
    map.range::<str, _>("".."")
}

I expected to see this happen: This should compile since BTreeMap::range is supposed to accept a RangeBounds with an unsized type parameter

Instead, this happened: It does not compile with the error:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:3:25
  |
3 |     map.range::<str, _>("".."")
  |         -----           ^^^^^^ 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`
  = help: the following other types implement trait `RangeBounds<T>`:
            std::ops::Range<&T>
            std::ops::Range<T>
  = note: required for `std::ops::Range<&str>` to implement `RangeBounds<str>`
note: required by a bound in `BTreeMap::<K, V, A>::range`

This is because Range<&str> does not implement RangeBounds<str>, because the current impl RangeBounds<str> for Range<&str> is as follows:

#[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for Range<&T> {
    fn start_bound(&self) -> Bound<&T> {
        Included(self.start)
    }
    fn end_bound(&self) -> Bound<&T> {
        Excluded(self.end)
    }
}

Similarly, for other range structs except (Bound, Bound) and RangeFull (the latter of which does not have a type anyway), the type parameter is unnecessarily constrained to be sized:

image

The implicit Sized bound on T for the RangeXxx<&T> cases appears to be unintentional judging from the fact that there are no comments explaining this decision.

Meta

rustc --version --verbose:

rustc 1.66.1 (90743e729 2023-01-10)
binary: rustc
commit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
release: 1.66.1
LLVM version: 15.0.2
Backtrace

<backtrace>

@SOF3 SOF3 added the C-bug Category: This is a bug. label Jan 22, 2023
@SOF3
Copy link
Contributor Author

SOF3 commented Jan 22, 2023

Seems to be a duplicate of #64327

@SOF3
Copy link
Contributor Author

SOF3 commented Jan 22, 2023

For posterity sake, can we add a comment there explaining why it is not fixed?

@fmease fmease added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. needs-triage-legacy labels Jan 26, 2024
@Dylan-DPC
Copy link
Member

The reason for not being merged: #64327 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants