Skip to content

Incorrect constant evaluation warning on correct code #46553

Closed
@mooman219

Description

@mooman219

It looks like I'm incorrectly being warned of a constant evaluation error on valid code. The code runs just fine and is not violating the constant function requirements.

Offending code:

#![feature(const_fn)]

pub struct Data<T> {
    function: fn() -> T,
}

impl<T> Data<T> {
    pub const fn new(function: fn() -> T) -> Data<T> {
        Data {
            function: function,
        }
    }
}

pub static DATA: Data<i32> = Data::new(|| {
    413i32
});

fn main() {
    print!("{:?}", (DATA.function)());
}

Incorrect error:

warning: constant evaluation error: unsupported constant expr
  --> src/main.rs:15:30
   |
15 |   pub static DATA: Data<i32> = Data::new(|| {
   |  ______________________________^
16 | |     413i32
17 | | });
   | |__^
   |
   = note: #[warn(const_err)] on by default

Example of the issue on the playground in nightly.

Ping #24111

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions