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

Where bounds are ignored as part of trait type parameters on an impl #60374

Open
richardwhiuk opened this issue Apr 29, 2019 · 1 comment
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@richardwhiuk
Copy link

richardwhiuk commented Apr 29, 2019

Where bounds are ignored, but suggested by the compiler, when implementing trait objects with second order types.

A full example is available at: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d61ff8acea97a202e00ea6fc32def211

The bound on line 21 is ignored:

    <T as hyper::service::MakeService<&'a SC>>::Service: 'static,

but suggested (with inaccurate markers):

error[E0310]: the associated type `<T as hyper::service::make_service::MakeService<&SC>>::Service` may not live long enough
  --> src/lib.rs:10:32
   |
10 | impl<'a, T, SC, RC, F, OB, ME> hyper::service::MakeService<&'a SC>
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider adding an explicit lifetime bound `<T as hyper::service::make_service::MakeService<&SC>>::Service: 'static`...
note: ...so that the type `<T as hyper::service::make_service::MakeService<&SC>>::Service` will meet its required lifetime bounds
  --> src/lib.rs:10:32
   |
10 | impl<'a, T, SC, RC, F, OB, ME> hyper::service::MakeService<&'a SC>
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In fact, in the example given, the error is repeated four times for good measure, in a standard cargo build.

The workaround is to add another type parameter to the function declaration - i.e. changing:

impl<'a, T, S, SC, RC, F, OB, ME> hyper::service::MakeService<&'a SC>
    for MakeService<T, RC>
where
    T: hyper::service::MakeService<
        &'a SC,
        ReqBody = hyper::Body,
        ResBody = OB,
        Error = hyper::Error,
        MakeError = ME,
        Future = F,
    >,
    <T as hyper::service::MakeService<&'a SC>>::Service: 'static,

to

impl<'a, T, SC, RC, F, OB, ME> hyper::service::MakeService<&'a SC>
    for MakeService<T, RC>
where
    T: hyper::service::MakeService<
        &'a SC,
        ReqBody = hyper::Body,
        ResBody = OB,
        Error = hyper::Error,
        Service = S,
        MakeError = ME,
        Future = F,
    >,
    S: 'static,

which additionally requires duplicating any bounds on the inner type (i.e,

    type Service: Service<
        ReqBody=Self::ReqBody,
        ResBody=Self::ResBody,
        Error=Self::Error,
    >;

specified at https://docs.rs/hyper/0.12.25/src/hyper/service/make_service.rs.html#10-44).

This results in function declarations with large numbers of type parameters, and more complex bounds than is strictly necessary.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2019
@daboross
Copy link
Contributor

daboross commented Sep 23, 2019

This seems related to #20671?

@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants