Skip to content

trait method calls on ~const bounded types should not be allowed in generic const exprs #102498

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

Closed
fee1-dead opened this issue Sep 30, 2022 · 3 comments
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. requires-nightly This issue requires a nightly compiler in some way.

Comments

@fee1-dead
Copy link
Member

fee1-dead commented Sep 30, 2022

playground

#![feature(const_trait_impl, generic_const_exprs)]

#[const_trait]
pub trait Tr {
    fn a() -> usize;
}

impl Tr for () {
    fn a() -> usize {
        1
    }
}

const fn foo<T: ~const Tr>() -> [u8; T::a()] {
    [0; T::a()]
}

fn huh() {
    foo::<()>();
}

If we remove the huh definition, it compiles. This fix would involve treating caller bounds differently and would probably come with the addition of "always const" bounds.

This is an interaction with generic_const_exprs and const_trait_impl, but does not block the stabilization of generic_const_exprs as this is only allowed if someone enables the feature const_trait_impl.

@fee1-dead fee1-dead added C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Sep 30, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Sep 30, 2022

I think this issue is a good use case for a // known-bug issue until we fix it (may be automatically fixed by keyword generics)

compiler-errors added a commit to compiler-errors/rust that referenced this issue Oct 2, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 2, 2022
…k-Simulacrum

Add a known-bug test for rust-lang#102498

Self-explanatory
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 2, 2022
…k-Simulacrum

Add a known-bug test for rust-lang#102498

Self-explanatory
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 2, 2022
…k-Simulacrum

Add a known-bug test for rust-lang#102498

Self-explanatory
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 2, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#100451 (Do not panic when a test function returns Result::Err.)
 - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade)
 - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.)
 - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`)
 - rust-lang#102566 (Add a known-bug test for rust-lang#102498)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
Rollup of 5 pull requests

Successful merges:

 - rust-lang#100451 (Do not panic when a test function returns Result::Err.)
 - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade)
 - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.)
 - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`)
 - rust-lang#102566 (Add a known-bug test for rust-lang#102498)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@workingjubilee workingjubilee added requires-nightly This issue requires a nightly compiler in some way. requires-incomplete-features This issue requires the use of incomplete features. labels Mar 4, 2023
@juntyr
Copy link
Contributor

juntyr commented Dec 8, 2023

I stumbled across this case today - is there a way yet in the const-trait-rewrite to use const-if-const trait methods inside constants? Is one planned (before the rewrite started, this case was supported) or will this require new always-const bounds?

@fmease
Copy link
Member

fmease commented Feb 12, 2025

Under the current implementation this gets rejected with and without huh existing1, so this is … fixed if I understand correctly? I don't believe this needs a regression test since I'm pretty sure we already have some that cover ~this.

Like, foo is only legal if you swap the conditionally-const with an always-const bound.

Footnotes

  1. Obviously after having added #[const_trait] to Tr.

@fmease fmease closed this as completed Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests

5 participants