Skip to content

Generic bounds don't seem to be used in consteval #50308

Closed
@glandium

Description

@glandium

The following code fails to compile

struct Bar<T> (
    std::marker::PhantomData<(T, [(); 0 - !(std::mem::align_of::<T>() > 3) as usize])>,
);

The error is:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
 --> src/main.rs:2:45
  |
2 |     std::marker::PhantomData<(T, [(); 0 - !(std::mem::align_of::<T>() > 3) as usize])>,
  |                                             ^^^^^^^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required by `std::mem::align_of`

which makes no sense since there is an implicit T: Sized bound from the struct definition. Even with an explicit bound, the error occurs.

Combined with associated consts, this leads to weird error messages:

trait Foo: Sized {
    const align: usize = std::mem::align_of::<Self>();
}

struct Bar<T: Foo> (
    std::marker::PhantomData<(T, [(); 0 - !(T::align > 3) as usize])>,
);
error[E0599]: no function or associated item named `align` found for type `T` in the current scope
 --> src/main.rs:6:45
  |
6 |     std::marker::PhantomData<(T, [(); 0 - !(T::align > 3) as usize])>,
  |                                             ^^^^^^^^ function or associated item not found in `T`
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `align`, perhaps you need to implement it:
          candidate #1: `Foo`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions