Skip to content

const_trait_impl does not work for a dependent crate #84846

Closed
@qinsoon

Description

@qinsoon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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