Skip to content

Call to const trait methods in const functions no longer allowed #114808

Closed as not planned
@matthieu-m

Description

@matthieu-m

Code

I tried this code:

#![feature(const_trait_impl)]

#[const_trait]
trait Foo {
    fn foo(&self) -> i32;
}

struct X;

impl const Foo for X {
    fn foo(&self) -> i32 { 4 }
}

const fn call_foo<F>(x: &F) -> i32
where
    F: ~const Foo,
{
    x.foo()
}

Version it worked on

It compiled successfully on nightly 2023-07-31, it may succeed on later versions.

Version with regression

It failed to compile on nightly 2023-08-13 with:

error[[E0015]](https://doc.rust-lang.org/nightly/error_codes/E0015.html): cannot call non-const fn `<F as Foo>::foo` in constant functions
  --> src/lib.rs:18:7
   |
18 |     x.foo()
   |       ^^^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-const_trait_impl`#![feature(const_trait_impl)]`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