Skip to content

Tracking issue for associated_consts feature #29646

Closed
@aturon

Description

The feature is currently quite buggy, but this issue tracks its eventual stabilization.

Shortcomings of the current implementation

Associated consts in const expressions must be concrete

Associated consts can only be used in const contexts if their input types are fully concrete. That is, this works:

trait Foo {
     const X: usize;
}

impl Foo for () {
     const X = 0;
}

let y: [i32; <() as Foo>::X];

But this does not work:

fn foo<T: Foo>() {
    let y: [i32; <T as Foo>::X];
}

Associated consts are never object safe

Associated consts make a trait non-object-safe. There is no way to bound the const where Self: Sized, nor to specify the const when creating the object.

trait Foo {
    const FOO: i32;
}

// Not allowed:
Box<Foo<FOO = 0>>

trait Bar {
    // Not allowed
    const BAR: i32 where Self: Sized;
}

Associated consts referencing other associated consts have spurious destructor warnings

This works:

trait Foo {
    const FOO: Option<i32>;
}

trait Bar: Foo + 'static {
    const BAR: Option<i32> = <Self as Foo>::FOO;
}

But this does not, because we (unnecessarily) assume the const could be a variant that runs a destructor:

trait Foo {
    const FOO: Option<String>;
}

trait Bar: Foo + 'static {
    const BAR: Option<String> = <Self as Foo>::FOO;
}

Activity

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

Metadata

Assignees

Labels

A-associated-itemsArea: Associated items (types, constants & functions)B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions