Skip to content

Adding generic trait impl with const_evaluatable_checked bound can lead to compilation error in code using the trait. #82279

Closed
@steffahn

Description

@steffahn
#![feature(const_generics)]
#![feature(const_evaluatable_checked)]
#![allow(incomplete_features)]

trait Foo {}

fn f<T: Foo>() {}

impl<T> Foo for T
where
    [(); std::mem::size_of::<T>()]:
{}

Note that the above code compiles without the function f, so the trait impl itself is valid. Also, obviously, it compiles without
the impl. Adding an impl should not be a breaking change like that. While adding a blanket impl is a breaking change, AFAIR it isn’t for sealed traits; and anyways, a similar error occurs when T is replaced by Struct<T>, like this.

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: unconstrained generic constant
  --> src/lib.rs:7:9
   |
7  | fn f<T: Foo>() {}
   |         ^^^
   |
help: try adding a `where` bound using this expression: `where [u8; std::mem::size_of::<T>()]: Sized`
  --> src/lib.rs:11:10
   |
11 |     [(); std::mem::size_of::<T>()]:
   |          ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

@rustbot modify labels: F-const_evaluatable_checked, T-compiler, C-bug, requires-nightly, F-const_generics

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`F-generic_const_exprs`#![feature(generic_const_exprs)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions