Skip to content

Deprecated tracking issue for closed RFC 2632, impl const Trait for Ty and ~const (tilde const) syntax (const_trait_impl) #67792

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

Open
5 of 13 tasks
Tracked by #1568 ...
ecstatic-morse opened this issue Jan 2, 2020 · 62 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-trait-system Area: Trait system C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC F-const_trait_impl `#![feature(const_trait_impl)]` S-tracking-perma-unstable Status: The feature will stay unstable indefinitely. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Jan 2, 2020

NOTE: See #110395, which tracks a planned rewrite of this feature's implementation

This is the primary tracking issue for rust-lang/rfcs#2632.

The current RFC text can be found at https://internals.rust-lang.org/t/pre-rfc-revamped-const-trait-impl-aka-rfc-2632/15192

This RFC has not yet been accepted. It is being implemented on a provisional basis to evaluate the potential fallout.

cc #57563

The feature gate is const_trait_impl.

Components

  • #[const_trait] attribute
  • impl const Trait
  • T: ~const Trait
  • append_const_msg on rustc_on_unimplemented
  • #[derive_const]
  • trait Destruct

Open issues

  • const trait checks allow calling non-const methods #88155
  • this test shows we can currently still call a const fn with a Trait bound even if the concrete type does not implement const Trait, but just Trait. This will fail later during evaluation. Some related discussion can be found in Allow using generic trait methods in const fn #79287 (comment)
  • There are no tests for using a const impl without defining one without the feature gate enabled. This should be added before any impl const is added to the standard library.
  • We need some tests and probably code around #[rustc_const_stable] and #[rustc_const_unstable] so we can properly stabilize (or not) the constness of impls in the standard library
  • impl const wrongly accepts impl with non-const provided methods #79450 shows that with default function bodies in the trait declaration, we can cause non-const functions to exist in impl const Trait impls by leaving out these default functions
  • We need to determine the final syntax for ~const. (In the interim, we may want to switch this to use a k#provisional_keyword or similar.)
  • If we need #[default_method_body_is_const], determine the syntax for it instead of existing as an attribute
  • #[derive_const] for custom derives (proc macros) Provide a way for derives to know if they were invoked with #[derive_const] #118304
  • grep for 110395 and figure out what to do everywhere that issue is mentioned (this is old stuff left over from when the previous const trait impl was removed)
  • add a perf stress test for const trait impls and complex bounds that need solving

When stabilizing: compiler changes are required:

  • Error against putting rustc_const_unstable attribute on const impls as they are now insta-stable.
  • Treat default_method_body_is_const bodies and const impl bodies as stable const fn bodies. We need to prevent accidentally stabilizing an implementation that uses unstable lang/lib const fn features.
  • Change Rustdoc to display ~const bounds or what syntax we decided it to be.
@ecstatic-morse ecstatic-morse changed the title Tracking issue for RFC 2362, impl const Trait for Ty syntax Tracking issue for RFC 2362, impl const Trait for Ty Jan 2, 2020
@ecstatic-morse ecstatic-morse added A-const-fn C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Jan 2, 2020
@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 2, 2020
@jonas-schievink jonas-schievink added B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. and removed B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. labels Jan 2, 2020
@ecstatic-morse ecstatic-morse changed the title Tracking issue for RFC 2362, impl const Trait for Ty Tracking issue for RFC 2632, impl const Trait for Ty Jan 3, 2020
@oli-obk oli-obk added the F-const_trait_impl `#![feature(const_trait_impl)]` label Jan 5, 2020
Centril added a commit to Centril/rust that referenced this issue Jan 7, 2020
Parse the syntax described in RFC 2632

This adds support for both `impl const Trait for Ty` and `?const Trait` bound syntax from rust-lang/rfcs#2632 to the parser. For now, both modifiers end up in a newly-added `constness` field on `ast::TraitRef`, although this may change once the implementation is fleshed out.

I was planning on using `delay_span_bug` when this syntax is encountered during lowering, but I can't write `should-ice` UI tests. I emit a normal error instead, which causes duplicates when the feature gate is not enabled (see the `.stderr` files for the feature gate tests). Not sure what the desired approach is; Maybe just do nothing when the syntax is encountered with the feature gate is enabled?

@oli-obk I went with `const_trait_impl` and `const_trait_bound_opt_out` for the names of these features. Are these to your liking?

cc rust-lang#67792 rust-lang#67794

r? @Centril
Centril added a commit to Centril/rust that referenced this issue Jan 10, 2020
Parse the syntax described in RFC 2632

This adds support for both `impl const Trait for Ty` and `?const Trait` bound syntax from rust-lang/rfcs#2632 to the parser. For now, both modifiers end up in a newly-added `constness` field on `ast::TraitRef`, although this may change once the implementation is fleshed out.

I was planning on using `delay_span_bug` when this syntax is encountered during lowering, but I can't write `should-ice` UI tests. I emit a normal error instead, which causes duplicates when the feature gate is not enabled (see the `.stderr` files for the feature gate tests). Not sure what the desired approach is; Maybe just do nothing when the syntax is encountered with the feature gate is enabled?

@oli-obk I went with `const_trait_impl` and `const_trait_bound_opt_out` for the names of these features. Are these to your liking?

cc rust-lang#67792 rust-lang#67794

r? @Centril
Centril added a commit to Centril/rust that referenced this issue Jan 10, 2020
Parse the syntax described in RFC 2632

This adds support for both `impl const Trait for Ty` and `?const Trait` bound syntax from rust-lang/rfcs#2632 to the parser. For now, both modifiers end up in a newly-added `constness` field on `ast::TraitRef`, although this may change once the implementation is fleshed out.

I was planning on using `delay_span_bug` when this syntax is encountered during lowering, but I can't write `should-ice` UI tests. I emit a normal error instead, which causes duplicates when the feature gate is not enabled (see the `.stderr` files for the feature gate tests). Not sure what the desired approach is; Maybe just do nothing when the syntax is encountered with the feature gate is enabled?

@oli-obk I went with `const_trait_impl` and `const_trait_bound_opt_out` for the names of these features. Are these to your liking?

cc rust-lang#67792 rust-lang#67794

r? @Centril
bors added a commit that referenced this issue Feb 20, 2020
Allow trait methods to be called on concrete types in a const context

This partially implements [RFC 2632](rust-lang/rfcs#2632) by const-checking methods inside an `impl const` block and allowing those methods to be called on concrete types. Calling trait methods on type parameters in a const context is not yet allowed. Implementing this will require much more work. Since we are only concerned with methods on concrete types, we are able to take advantage of the machinery in `Instance::resolve`, which is doing most of the work.

This also propagates `#[rustc_const_unstable]` from parent items to child items, making that attribute behave like `#[stable]` and `#[unstable]` do. This allows trait methods to be marked as unstably const.

cc #67792 #57563
cc @rust-lang/wg-const-eval
r? @oli-obk
@jonas-schievink jonas-schievink added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Mar 28, 2020
github-actions bot pushed a commit to rust-lang/glacier that referenced this issue Nov 20, 2020
=== stdout ===
=== stderr ===
warning: the feature `const_trait_impl` is incomplete and may not be safe to use and/or cause compiler crashes
 --> /home/runner/work/glacier/glacier/ices/69487.rs:1:12
  |
1 | #![feature(const_trait_impl)]
  |            ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #67792 <rust-lang/rust#67792> for more information

error: A function with the "rust-call" ABI must take a single non-self argument that is a tuple
  --> /home/runner/work/glacier/glacier/ices/69487.rs:10:5
   |
10 |     extern "rust-call" fn call_once(self, arg: &usize) -> Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

==============
@RalfJung RalfJung changed the title Tracking issue for RFC 2632, impl const Trait for Ty and ~const (tilde const) syntax Tracking issue for RFC 2632, impl const Trait for Ty and ~const (tilde const) syntax (const_trait_impl) Jan 16, 2025
@programmerjake

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Jan 20, 2025 via email

@loynoir
Copy link

loynoir commented Mar 30, 2025

What about HRTB?

Fail to compile when HRTB + const trait

pub mod reproduce {
    #[const_trait]
    pub trait ConstFrom<T>: Sized {
        #[must_use]
        fn const_from(value: T) -> Self;
    }

    #[const_trait]
    pub trait ConstAddAssign<Rhs = Self> {
        fn const_add_assign(&mut self, rhs: Rhs);
    }

    pub fn incr42<T>(mut x: T)
    where
        T: const ConstFrom<u8> + for<'a> ConstAddAssign<&'a T>,
    {
        let delta = T::const_from(42u8);

        x.const_add_assign(&delta)
    }

    pub const fn const_incr42<T>(mut x: T)
    where
        T: const ConstFrom<u8> + for<'a> const ConstAddAssign<&'a T>,
    {
        let delta = T::const_from(42u8);

        x.const_add_assign(&delta)
    }
}

@quratoop
Copy link

The Bound must be T: ~const not T: const

@loynoir
Copy link

loynoir commented Mar 30, 2025

@quratoop

The Bound must be T: ~const not T: const

Still not able to compile.

    pub const fn const_incr42<T>(mut x: T)
    where
        T: ~const ConstFrom<u8> + for<'a> ~const ConstAddAssign<&'a T>,
    {
        let delta = T::const_from(42u8);

        x.const_add_assign(&delta)
    }

@quratoop
Copy link

Hmm, I can't find a quick solution. I spent some time looking into the const_trait_impl feature, but never really in combination with HRTBs. I imagine it's not currently supported, which is trivial since you can declare the traits.

@fmease
Copy link
Member

fmease commented Mar 30, 2025

@quratoop You need to also add the bound ~const std::marker::Destruct to T (additionally requires feature const_destruct).


This is a tracking issue, for the record. If you have a bug with a specific feature, please open a new issue! If you want to discuss the implementation, please also open a new issue and it can be tagged C-discussion. Tracking issues have a linear history so they are not conducive to any kind of discussion.


CC'ing the new RFC, RFC 3762, again, for which we would open a new tracking issue on approval as Ralf wrote.
Also CC other channels of communication: #t-compiler/project-const-traits (implementation) and #t-lang/effects (overall design).

@rust-lang rust-lang locked as off-topic and limited conversation to collaborators Mar 30, 2025
@fmease fmease changed the title Tracking issue for RFC 2632, impl const Trait for Ty and ~const (tilde const) syntax (const_trait_impl) Deprecated tracking issue for closed RFC 2632, impl const Trait for Ty and ~const (tilde const) syntax (const_trait_impl) Mar 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-trait-system Area: Trait system C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC F-const_trait_impl `#![feature(const_trait_impl)]` S-tracking-perma-unstable Status: The feature will stay unstable indefinitely. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests