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

Tracking issue for Range[Inclusive]::is_empty (feature range_is_empty) #48111

Closed
scottmcm opened this issue Feb 10, 2018 · 8 comments · Fixed by #75132
Closed

Tracking issue for Range[Inclusive]::is_empty (feature range_is_empty) #48111

scottmcm opened this issue Feb 10, 2018 · 8 comments · Fixed by #75132
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Feb 10, 2018

These methods are a clear way to check for empty ranges without the restrictions of ExactSizeIterator.

Merged as unstable on 2018-02-15 in PR at #48087

scottmcm added a commit to scottmcm/rust that referenced this issue Feb 10, 2018
@pietroalbini pietroalbini added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Feb 10, 2018
@jimblandy
Copy link
Contributor

This method is really hard to use. In the program:

fn main() {
    println!("{:?}", (0..0).is_empty());
}

Rust 1.31 in the playground (which is 2018 Edition) complains:

error[E0034]: multiple applicable items in scope
 --> src/main.rs:2:29
  |
2 |     println!("{:?}", (0..0).is_empty());
  |                             ^^^^^^^^ multiple `is_empty` found
  |
  = note: candidate #1 is defined in an impl for the type `std::ops::Range<_>`
note: candidate #2 is defined in the trait `std::iter::ExactSizeIterator`
  = help: to disambiguate the method call, write `std::iter::ExactSizeIterator::is_empty(::std::ops::Range{start: 0, end: 0,})` instead

This arises without any imports in scope, just the prelude. Using std::ops::Range::is_empty doesn't help. Using:

<std::ops::Range<i32> as ExactSizeIterator>::is_empty(&(0..0))

does work, but is longer than x.len() == 0.

@kennytm
Copy link
Member

kennytm commented Dec 7, 2018

@jimblandy The ambiguity happens only because you haven't enabled the relevant features. Assuming both ExactSizeIterator::is_empty and Range::is_empty are stabilized, using (0..0).is_empty() will always pick the latter since an inherent method has higher priority than a trait method.

#![feature(exact_size_is_empty, range_is_empty)]

use std::iter::ExactSizeIterator;

fn main() {
    println!("{:?}", (0..0).is_empty());
}

@scottmcm
Copy link
Member Author

scottmcm commented Dec 7, 2018

I believe that's an artifact of both being unstable, @jimblandy. With either or both feature gates enabled, it compiles fine (with a future warning if only the ESI gate is active):

#![feature(range_is_empty)]
#![feature(exact_size_is_empty)]
fn main() {
    println!("{:?}", (0..0).is_empty());
}

Edit: doh, 20 seconds late 😆

@jimblandy
Copy link
Contributor

Okay, thanks for the explanation.

I guess I'm still surprised that the error message is about the ambiguity, not the use of unstable features; is it that rustc can't decide which unstable feature's use to complain about? ^^;;

@raindev
Copy link
Contributor

raindev commented May 28, 2019

Are there any outstanding concerns to be resolved before stabilization or is the issue just waiting for someone to send a pull request?

@jonas-schievink jonas-schievink added the B-unstable Blocker: Implemented in the nightly compiler and unstable. label Nov 26, 2019
@yankana
Copy link

yankana commented Apr 3, 2020

I have no idea why a method like this would be unstable, and the reason provided is kind of old, contradicting itself.

Also, why is it coded like this?
!(self.start < self.end)

Isn't it better to just have this?
self.start >= self.end

@scottmcm
Copy link
Member Author

scottmcm commented Apr 3, 2020

@raindev I know of no outstanding concerns, though of course such a stabilization PR would need a T-libs FCP. There was a question about whether it should use PartialOrd or Ord, but now that contains is stable using PartialOrd, I think that using PartialOrd here is the obvious answer.

Also, why is it coded like this?

@yankana because those two are only equivalent for Ord, but the function allows PartialOrd. See the second set of examples in the documentation.

bors added a commit to rust-lang/rust-clippy that referenced this issue May 28, 2020
len_zero: skip ranges if feature `range_is_empty` is not enabled

If the feature is not enabled, calling `is_empty()` on a range is ambiguous. Moreover, the two possible resolutions are unstable methods, one inherent to the range and the other being part of the `ExactSizeIterator` trait.

Since `len_zero` only checks for existing `is_empty()` inherent methods, we only take into account the `range_is_empty` feature.

Related: rust-lang/rust#48111 (comment)

changelog: len_zero: avoid linting ranges without #![feature(range_is_empty)]

Fixes: #3807
flip1995 added a commit to flip1995/rust-clippy that referenced this issue May 31, 2020
len_zero: skip ranges if feature `range_is_empty` is not enabled

If the feature is not enabled, calling `is_empty()` on a range is ambiguous. Moreover, the two possible resolutions are unstable methods, one inherent to the range and the other being part of the `ExactSizeIterator` trait.

Since `len_zero` only checks for existing `is_empty()` inherent methods, we only take into account the `range_is_empty` feature.

Related: rust-lang/rust#48111 (comment)

changelog: len_zero: avoid linting ranges without #![feature(range_is_empty)]

Fixes: rust-lang#3807
@KodrAus KodrAus added I-nominated Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. labels Jul 29, 2020
@scottmcm
Copy link
Member Author

scottmcm commented Aug 4, 2020

To anyone following this tracking issue, I've made a PR to request stabilization of these methods: #75132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants