Skip to content

Const generics and structural match. #71415

Closed
@crlf0710

Description

@crlf0710
Member

Not sure whether this has been reported before.
I tried this code:

#![feature(const_generics)]
#![feature(structural_match)]

use std::marker::{StructuralPartialEq, StructuralEq};

struct Container<T: StructuralPartialEq + StructuralEq, const V: T>;

fn main() {
    let container1: Container<i32, {42}> = Container;
    let container2: Container<&'static str, "Hello world"> = Container;
}

I expected to see this happen: The compile passes.

Instead, this happened:

error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
 --> src/main.rs:6:66
  |
6 | struct Container<T: StructuralPartialEq + StructuralEq, const V: T>;
  |                                                                  ^ `T` doesn't derive both `PartialEq` and `Eq`

Meta

rustc --version --verbose:

Nightly channel
Build using the Nightly version: 1.44.0-nightly

(2020-04-21 45d050cde277b22a7558)
Backtrace

<backtrace>

@rustbot modify labels to +A-const generics F-const generics T-compiler requires-nightly

Activity

rustbot

rustbot commented on Apr 22, 2020

@rustbot
Collaborator

Error: Label generics can only be set by Rust team members

Please let @rust-lang/release know if you're having trouble with this bot.

crlf0710

crlf0710 commented on Apr 22, 2020

@crlf0710
MemberAuthor

@rustbot modify labels to +A-const-generics F-const_generics T-compiler requires-nightly

added
A-const-genericsArea: const generics (parameters and arguments)
requires-nightlyThis issue requires a nightly compiler in some way.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 22, 2020
crlf0710

crlf0710 commented on Apr 22, 2020

@crlf0710
MemberAuthor
#![feature(const_generics)]
#![feature(structural_match)]

use std::marker::{StructuralPartialEq, StructuralEq};

struct Container<T: StructuralPartialEq + StructuralEq + PartialEq + Eq, const V: T>;

fn main() {
    let container1: Container<i32, {42}> = Container;
    let container2: Container<&'static str, "Hello world"> = Container;
}

This also doesn't work.

varkor

varkor commented on Apr 22, 2020

@varkor
Member

I'm pretty sure StructuralPartialEq and StructuralEq are not supposed to be public like this… it's not valid to claim that an arbitrary type is structural_match. I don't expect this to work.

cc @pnkfelix @eddyb

lcnr

lcnr commented on Apr 22, 2020

@lcnr
Contributor

We don't allow const paramters with generic types at all afaik. So while the error message is misleading, I don't think that this will work soon.

varkor

varkor commented on Apr 22, 2020

@varkor
Member

We don't allow const paramters with generic types at all afaik. So while the error message is misleading, I don't think that this will work soon.

Right. This will actually be improved by #70845, which makes this explicit.

crlf0710

crlf0710 commented on Apr 23, 2020

@crlf0710
MemberAuthor

Thanks. Is this a language limitation or implementation limitation? Will lazy normalization allow this to be written?

varkor

varkor commented on Apr 23, 2020

@varkor
Member

It won't be supported after lazy normalisation specifically, but we would like to be able to use const parameters with generic types eventually.

vmarkushin

vmarkushin commented on Dec 25, 2020

@vmarkushin

Also consider this example

#![feature(const_generics)]

#[derive(PartialEq, Eq)]
enum Nat {
    Z,
    S(Box<Nat>)
}

fn foo<const N: Nat>() {}
error[E0741]: `Nat` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
 --> src/lib.rs:9:17
  |
9 | fn foo<const N: Nat>() {}
  |                 ^^^ `Nat` doesn't derive both `PartialEq` and `Eq`
lcnr

lcnr commented on Dec 25, 2020

@lcnr
Contributor

@vmarkushin can you open a separate issue for this as your example seems distinct enough to be tracked separately?

4 remaining items

Loading
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-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @crlf0710@vmarkushin@varkor@lcnr@rustbot

        Issue actions

          Const generics and structural match. · Issue #71415 · rust-lang/rust