Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const_trait_impl does not work for a dependent crate #84846

Closed
qinsoon opened this issue May 3, 2021 · 1 comment
Closed

const_trait_impl does not work for a dependent crate #84846

qinsoon opened this issue May 3, 2021 · 1 comment
Labels
A-metadata Area: Crate metadata C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@qinsoon
Copy link

qinsoon commented May 3, 2021

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
@qinsoon qinsoon added the C-bug Category: This is a bug. label May 3, 2021
@jonas-schievink jonas-schievink added A-metadata Area: Crate metadata F-const_trait_impl `#![feature(const_trait_impl)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 3, 2021
@fee1-dead
Copy link
Member

Fixed by #86750

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-metadata Area: Crate metadata C-bug Category: This is a bug. F-const_trait_impl `#![feature(const_trait_impl)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants