-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Closed
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamRelevant to the language teamfinal-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.In the final comment period and will be merged soon unless new substantive objections are raised.
Description
The feature is currently quite buggy, but this issue tracks its eventual stabilization.
-
Blocked onconst_fn
. -
Type params in const expressions (associated-constants can not be used in constant expressions #34344 /cannot use an outer type parameter in this context
#39211) - Associated consts and object safety (Associated constants should not be object safe #26847)
- Do not always require
T: 'static
forT::CONST
- Associated constants not fully checked between
trait
andimpl
(rustc_typeck::check::compare_method::compare_const_impl doesn't drain its fullfillment context nor invoke regionck. #41323) - Docs
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;
}
alex, pyfisch, raphaelcohn, Gedweb, hauleth and 12 more
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamRelevant to the language teamfinal-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.In the final comment period and will be merged soon unless new substantive objections are raised.