Skip to content

Function used in anonymous constant is not considered used #104084

New issue

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

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

Already on GitHub? # to your account

Closed
joshlf opened this issue Nov 7, 2022 · 4 comments
Closed

Function used in anonymous constant is not considered used #104084

joshlf opened this issue Nov 7, 2022 · 4 comments

Comments

@joshlf
Copy link
Contributor

joshlf commented Nov 7, 2022

I have the following code:

fn foo() {}
const fn bar() {}

fn main() {
    let _ = foo();
    const _: () = bar();
}

Even though both foo and bar are used, I get an unused warning for bar:

warning: function `bar` is never used
 --> src/main.rs:2:10
  |
2 | const fn bar() {}
  |          ^^^
  |
  = note: `#[warn(dead_code)]` on by default

Version

Rust 1.65.0

@compiler-errors
Copy link
Member

compiler-errors commented Nov 7, 2022

So unlike foo, which has (possibly) a side-effect even its return type is not assigned, const _: () is unused in the program, so anything that it uniquely references is considered unused as well.

Giving the constant a name and using the constant suppresses the warning:

fn foo() {}
const fn bar() {}

fn main() {
    let _ = foo();
    const A: () = bar();
    let _ = A;
}

Do you have a more detailed explanation if you hit this in production and was confused by the warning?

@joshlf
Copy link
Contributor Author

joshlf commented Nov 7, 2022

Do you have a more detailed explanation if you hit this in production and was confused by the warning?

Sure: I'm using it in a test that runs at compile time. The code doesn't need to actually produce anything (in other words, there's no const value that's used by another part of the program), it just needs to compile successfully.

The code is here if you're curious.

We also (in our zerocopy-derive crate) emit code of the form const _: () = { ... } inside of derive-generated impl blocks in order to make sure that safety invariants are satisfied - the emitted code fails to compile if the invariants are not satisfied (source code here). We haven't run into this particular issue with zerocopy-derive because all of the items we use are either defined inside of the { ... } or in another crate; I mention this just to illustrate that there is a class of use cases where you want to consider something "used" even if it's just inside of an anonymous constant.

@jruderman
Copy link
Contributor

FWIW, you can suppress the warning by putting #[allow(dead_code)] on the const item which you want to consider a "use":

const fn bar() {}

fn main() {
    #[allow(dead_code)]
    const _: () = bar();
}

@Dylan-DPC
Copy link
Member

Closing this as it is intended behaviour for dead code

@Dylan-DPC Dylan-DPC closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants