Search code examples
rustrust-cargo

Encountered [E0195] lifetime parameters or bounds on type `Target` do not match the trait declaration when using lazy_static


While doing cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0', I was hit with the following:

error[E0195]: lifetime parameters or bounds on type `Target` do not match the trait declaration
  --> C:\Users\USER\.cargo\registry\src\index.crates.io-6f17d22bba15001f\console-0.15.7\src\utils.rs:25:1
   |
25 | / lazy_static! {
26 | |     static ref STDOUT_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stdout()));
27 | |     static ref STDERR_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stderr()));
28 | | }
   | |_^ lifetimes do not match type in trait
   |
   = note: this error originates in the macro `__lazy_static_internal` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info)

As this wasn't the first machine I installed flutter_rust_bridge_codegen, I am pretty sure this is not the problem of the release.

I suspect there is an issue with the cached version of the lazy_static crate.


Solution

  • I dug into the cache %USERPROFILE%\.cargo\registry\src\index.crates.io-6f17d22bba15001f\lazy_static-1.4.0\src\lib.rs and found the line that defines type Target

    The cached version I had was type Target<'a> = $T; I revised it to type Target = $T;

    Then I rerun cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0'

    Problem solved.

    Further note: I had encountered similar problem with my own development and had circumvent using lazy_static altogether because I thought my understanding of lazy_static usage was wrong.

    This time around, I realised the problem didn't just happened to my code.

    My "fix" may not be totally correct. But to me, this <'a> seem unnecessary and is really painful to handle. Even though imperfect, the fix at least gets me to move on to focus on the things I cared more for.

    I cannot find a similar case in SO. So my assumption is this lazy_static issue may be caused by my own ignorance. I am keen to find out what I could have done better.