Closed
Description
I tried this minimal example.
One crate int
that declares an Add
trait with const_trait_impl
. It works fine and we can declare a constant using the add trait:
#![allow(incomplete_features)]
#![feature(const_trait_impl)]
use std::ops::Add;
pub struct Integer(pub i32);
impl const Add<i32> for Integer {
type Output = Integer;
fn add(self, a: i32) -> Integer {
Integer(self.0 + a)
}
}
pub const A: Integer = Integer(0);
// This works
pub const B: Integer = A + 1;
A dependent crate that uses the above crate. I can use the add trait in the dependent crate, but I cannot declare a constant using the trait:
use int::Integer;
pub const A: Integer = Integer(0);
// This fails
pub const B: Integer = A + 1;
// This also fails
pub const C: Integer = int::B;
pub fn add(x: Integer, i: i32) -> Integer {
// This works
x + i
}
This error was given:
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
--> src/lib.rs:5:24
|
5 | pub const B: Integer = A + 1;
| ^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0015`.
Meta
rustc --version --verbose
:
rustc 1.53.0-nightly (42816d61e 2021-04-24)
binary: rustc
commit-hash: 42816d61ead7e46d462df997958ccfd514f8c21c
commit-date: 2021-04-24
host: x86_64-apple-darwin
release: 1.53.0-nightly
LLVM version: 12.0.0