Skip to content

ergonomics: the error message when trying to instrument const fn is hard to read #2414

Closed
@Michcioperz

Description

@Michcioperz

Bug Report

I tried using #[instrument] on a const fn in a rush in a larger codebase which didn't use tracing before, and got a slightly convoluted compile error message. Once I stopped being angry at my code, it clicked to me why logging in const context would be kind of difficult, but the message doesn't make it super easy.

I think it would be nice if the attribute macro outright rejected instrumenting const functions?

I looked for similar issues, but I suppose I'm first.

Version

michcioperz@Hiyori> cargo tree | grep tracing
└── tracing v0.1.37
    ├── tracing-attributes v0.1.23 (proc-macro)
    └── tracing-core v0.1.30

Platform

michcioperz@Hiyori> uname -a
Darwin Hiyori.local 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct  9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112 arm64
michcioperz@Hiyori> rustc --version
rustc 1.65.0 (897e37553 2022-11-02)

Crates

tracing-attributes, I think

Description

I got a minimal reproduction like this, cargo new --lib a, cargo add tracing,

// src/lib.rs
#[tracing::instrument]
const fn a() {
}

Then if you try to compile it:

michcioperz@Hiyori> cargo build
   Compiling a v0.1.0 (/private/var/folders/_0/cbkgpmq97b1bcm8xkcssk_hh0000gn/T/tmp.jEjglWQ0/a)
error[E0277]: can't compare `Level` with `_` in const contexts
 --> src/lib.rs:1:1
  |
1 | #[tracing::instrument]
  | ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `Level < _` and `Level > _`
  |
  = help: the trait `~const PartialOrd<_>` is not implemented for `Level`
note: the trait `PartialOrd<_>` is implemented for `Level`, but that implementation is not `const`
 --> src/lib.rs:1:1
  |
1 | #[tracing::instrument]
  | ^^^^^^^^^^^^^^^^^^^^^^
  = note: this error originates in the macro `tracing::level_enabled` which comes from the expansion of the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: can't compare `Level` with `_` in const contexts
 --> src/lib.rs:1:1
  |
1 | #[tracing::instrument]
  | ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `Level < _` and `Level > _`
  |
  = help: the trait `~const PartialOrd<_>` is not implemented for `Level`
note: the trait `PartialOrd<_>` is implemented for `Level`, but that implementation is not `const`
 --> src/lib.rs:1:1
  |
1 | #[tracing::instrument]
  | ^^^^^^^^^^^^^^^^^^^^^^
  = note: this error originates in the macro `$crate::level_enabled` which comes from the expansion of the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `a` due to 4 previous errors

Thanks for the crate and your time

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/attributesRelated to the `tracing-attributes` crategood first issueGood for newcomerskind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions