Type_name returns different value for local function for const and non-const variable

Hi,

Recently I've decided to migrate function_name!() to const and I've discovered that depending on if variable is const or let I get different results from type_name

#![feature(const_type_name)]

const fn type_name_of<T>(_: &T) -> &'static str {
    std::any::type_name::<T>()
}

fn main() {
    const A: &str = {
        fn f() {}
        type_name_of(&f)
    };
    let b: &str = {
        fn f() {}
        type_name_of(&f)
    };
    
    println!("A: {}. B: {}", A, b)
}
A: playground::main::A::f. B: playground::main::f

Is it something expected or should I create a bug for it?

  1. Those are different functions, not the same function
  2. Your type_name_of is the same as type_name_of_val
  3. It says

    Like type_name, this is intended for diagnostic use and the exact output is not guaranteed. [...] In short: use this for debugging, avoid using the output to affect program behavior.

You could make a case for consistency, but I wouldn't call it a bug.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.