Skip to content

Foo<T>: Trait bounds (T is a type param) are allowed in const fns #83452

Closed
@rodrimati1992

Description

@rodrimati1992

I tried this code:

pub trait Const {
    const C: u8;
}

impl Const for u8 {
    const C: u8 = 3;
}
pub struct Wrapper<T>(T);
impl<T: Const> Const for Wrapper<T> {
    const C: u8 = T::C;
}

const fn foo<T>() -> u8 
where
    Wrapper<T>: Const,
{
    <Wrapper<T>>::C
}

fn main(){
    const D: u8 = foo::<u8>();
    dbg!(D);
}

I expected to see this code not compile, because it bounds types by non-auto traits in const fns.

Instead, this happened: The code compiles without errors, and prints 3
This does error with T: Const bounds as expected.

Meta

rustc --version --verbose:

rustc 1.53.0-nightly (673d0db5e 2021-03-23)
binary: rustc
commit-hash: 673d0db5e393e9c64897005b470bfeb6d5aec61b
commit-date: 2021-03-23
host: i686-unknown-linux-gnu
release: 1.53.0-nightly
LLVM version: 12.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-stabilityArea: `#[stable]`, `#[unstable]` etc.C-bugCategory: This is a bug.F-const_trait_impl`#![feature(const_trait_impl)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions