Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Build fails with error[E0310]: the parameter type T may not live long enough. #274

Closed
dreirund opened this issue Jul 8, 2024 · 2 comments

Comments

@dreirund
Copy link

dreirund commented Jul 8, 2024

With rust version 1.79.0, build of this package (latest release version 0.11.4) fails with error[E0310]: the parameter type `T` may not live long enough:

    Updating git repository `https://github.com/H-M-H/autopilot-rs.git`
    Updating crates.io index
  Downloaded block-padding v0.1.5
[...]
   Compiling proc-macro2 v1.0.29
   Compiling unicode-xid v0.2.2
[...]
   Compiling log v0.3.9
error[E0310]: the parameter type `T` may not live long enough
    --> /var/cache/makepkg/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustc-serialize-0.3.24/src/serialize.rs:1155:5
     |
1155 |     fn decode<D: Decoder>(d: &mut D) -> Result<Cow<'static, T>, D::Error> {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |     |
     |     the parameter type `T` must be valid for the static lifetime...
     |     ...so that the type `T` will meet its required lifetime bounds...
     |
note: ...that is required by this bound
    --> /build/rust/src/rustc-1.79.0-src/library/alloc/src/borrow.rs:180:30
help: consider adding an explicit lifetime bound
     |
1151 | impl<'a, T: ?Sized + 'static> Decodable for Cow<'a, T>
     |                    +++++++++

   Compiling pin-utils v0.1.0
[...]
   Compiling matches v0.1.9
For more information about this error, try `rustc --explain E0310`.
error: could not compile `rustc-serialize` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

I am building according to this recipe.

rustc --explain E0310 shows:

A parameter type is missing a lifetime constraint or has a lifetime that does not live long enough.

Erroneous code example:

// This won't compile because T is not constrained to the static lifetime
// the reference needs
struct Foo<T> {
    foo: &'static T
}

Type parameters in type definitions have lifetimes associated with them that represent how long the data stored within them is guaranteed
to live. This lifetime must be as long as the data needs to be alive, and missing the constraint that denotes this will cause this error.

This will compile, because it has the constraint on the type parameter:

struct Foo<T: 'static> {
    foo: &'static T
}

Regards!

@beroal
Copy link

beroal commented Sep 29, 2024

For the time being, you can fix this error by inserting

rm Cargo.lock

before

cargo build ...

in PKGBUILD.

However, I ran into another error.

@H-M-H
Copy link
Owner

H-M-H commented Oct 17, 2024

Thanks for the report, should be fixed now.

@H-M-H H-M-H closed this as completed Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants