Skip to content

RangeInclusive cannot be used in const generics. #70155

Closed
@CDirkx

Description

@CDirkx
Contributor

All range types except RangeInclusive can be used in const generics:

#![feature(const_generics)]

struct A<const R: Range<usize>>;
struct B<const R: RangeTo<usize>>;
struct C<const R: RangeFrom<usize>>;
struct D<const R: RangeToInclusive<usize>>;
struct E<const R: RangeFull<usize>>;

This compiles (on nightly), while the following does not:

#![feature(const_generics)]

struct S<const R: RangeInclusive<usize>>;

This is because RangeInclusive does not implement StructuralPartialEq and StructuralEq (#63438). RangeInclusive has fields start and end similar to the other range types, but also has an extra field: exhausted: bool (#68835). This was recently changed from a field is_empty: Option<bool>. This extra field tracks state for iteration and was added to address performance issues (#45222).

The old implementation with is_empty had semantic equality, however the new implementation with exhausted has structural equality:

impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.start == other.start && self.end == other.end && self.exhausted == other.exhausted
    }
} 

from range.rs

I believe this means we can either manually add implementations for StructuralPartialEq and StructuralEq, or just derive PartialEq and Eq, which will allow RangeInclusive to also be used in const generics and remove the inconsistency with other range types.

Activity

added
A-const-genericsArea: const generics (parameters and arguments)
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Mar 19, 2020
added 2 commits that reference this issue on Mar 20, 2020

Rollup merge of rust-lang#70166 - CDirkx:range-inclusive-derives, r=c…

cb57aeb

Rollup merge of rust-lang#70166 - CDirkx:range-inclusive-derives, r=c…

be6e983
added 2 commits that reference this issue on Mar 21, 2020

Rollup merge of rust-lang#70166 - CDirkx:range-inclusive-derives, r=c…

83c29ce

Rollup merge of rust-lang#70166 - CDirkx:range-inclusive-derives, r=c…

7b420ae
added a commit that references this issue on Mar 22, 2020
added a commit that references this issue on Mar 23, 2020

Rollup merge of rust-lang#70283 - CDirkx:regression-test-70155, r=oli…

7cdab7f
added a commit that references this issue on Mar 24, 2020

Auto merge of rust-lang#70343 - Centril:rollup-94egfvs, r=Centril

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.F-const_generics`#![feature(const_generics)]`T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonas-schievink@CDirkx

        Issue actions

          RangeInclusive cannot be used in const generics. · Issue #70155 · rust-lang/rust